/api-interface

Create simple and clean software API for ruby libraries.

Primary LanguageRubyMIT LicenseMIT

api-interface

Create simple and clean software API for ruby libraries.

Setup

  1. Install api-interface gem

  2. Create a module for your api and extend ApiInterface

  3. You can start registering methods, modules and classes to your Api.

Note: Methods, Modules and Classes are only added if they do not exist in the Api already. (not overwritten)

Usage

Adding a method to your API

module MyApi
  extend ApiInterface
  register :method, Path::To::Method::Container, :method_name
end

Now you can call MyApi::method_name and Path::To::Method::Container::method_name will be called in the background.

Adding a module to your API

module MyApi
  extend ApiInterface
  register :module, Path::To::MyModule, :ModuleNameInAPI
end

Now you can use MyApi::ModuleNameInAPI to access all public/protected methods of Path::To::MyModule.

Adding a module to your API

module MyApi
  extend ApiInterface
  register :class, Path::To::Klass, :Class4Api
end

Now you can use MyApi::Class4Api to access all public/protected methods ofPath::To::Klass.

Examples

Your code base:

module Hello
  module World
    class Klass
      def foo
        puts "Hello Klass#foo"
      end
    end

    module B
      def self.bar
        puts "Hello self.bar"
      end
    end

    module C
      def foo
        puts "Hello foo"
      end
      def bar
        puts "Hello bar"
      end
    end
  end
end

Your API:

module Hello
  module World
    module Api
      extend ApiInterface

      register :method, Hello::World::B, :bar
      register :module, Hello::World::C, :Module4Api
      register :class, Hello::World::Klass, :Class4Api

    end
  end
end

Testing it words:

puts "---- testing register :method ----"
Hello::World::Api::bar
puts "---- testing register :module ----"
Hello::World::Api::Module4Api.foo
puts "---- testing register :class ----"
Hello::World::Api::Class4Api.new.foo

Result:

---- testing register :method ----
Hello self.bar
---- testing register :module ----
Hello foo
---- testing register :class ----
Hello Klass#foo

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Adrien (adrien[at]gn2fr) . See LICENSE for details.