Module: ApiBee

Defined in:
lib/api_bee.rb,
lib/api_bee/node.rb,
lib/api_bee/proxy.rb,
lib/api_bee/version.rb,
lib/api_bee/adapters/hash.rb

Defined Under Namespace

Modules: Adapters Classes: Config, Node, Proxy

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.new_configObject

new config object with defaults



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/api_bee.rb', line 55

def new_config
  Config.new(
    # This field is expected in API responses
    # and should point to an individual resource with more data
    :uri_property_name            => :href,
    # Total number of entries
    # Used to paginate lists
    :total_entries_property_name  => :total_entries,
    # Name of array property
    # that contains cureent page's entries
    :entries_property_name        => :entries
  )
end

.setup(adapter_klass, *args) {|config| ... } ⇒ Object

Setup and instantiates a new API proxy (ApiBee::Proxy) by wrapping a bundled or custom API adapter and passing optional arguments

When a hash is passed as the adapter class, it will look for that class file in the bundled adapters directory:

ApiBee.setup(:hash, {})

Looks for ApiBee::Adapters::Hash in lib/api_bee/adapters

You can pass a custom adapter class

Example:

class MyAdapter
  def initialize(api_key)
   @url = 'http://myservice.com'
  end

  def get(path, options = {})
    # Fetch data from your service here
  end
end

api = ApiBee.setup(MyAdapter, 'MY_API_KEY')

That gives you an instance of ApiBee::Proxy wrapping an instance of your MyAdapter initialized with your key.

Yields:

  • (config)

Raises:

  • (NoMethodError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/api_bee.rb', line 34

def setup(adapter_klass, *args)
  
  adapter_klass = (
    require File.join('api_bee', 'adapters', adapter_klass.to_s)
    klass = adapter_klass.to_s.gsub(/(^.{1})/){$1.upcase}
    Adapters.const_get(klass)
  ) if adapter_klass.is_a?(Symbol)
  
  config = new_config
  # If adapter-wide config method
  adapter_klass.config_api_bee(config) if adapter_klass.respond_to?(:config_api_bee)
  # If config block passed per api instance
  yield config if block_given?
  
  adapter = adapter_klass.new(*args)
  raise NoMethodError, "Adapter must implement #get(path, *args) method" unless adapter.respond_to?(:get)
  
  Proxy.new adapter, config
end