Class: TheOneApi::ResourceWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/the_one_api/resource_wrapper.rb

Overview

A client for The One Api

Instance Method Summary collapse

Constructor Details

#initialize(wrapped_resource, api_key) ⇒ ResourceWrapper

Returns a new instance of ResourceWrapper.



6
7
8
9
# File 'lib/the_one_api/resource_wrapper.rb', line 6

def initialize(wrapped_resource, api_key)
  @wrapped_resource = wrapped_resource
  @api_key = api_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/the_one_api/resource_wrapper.rb', line 11

def method_missing(method, *args, &block)
  super unless @wrapped_resource._calls.key?(method)

  # merge the api key in with the supplied
  # arguments so that the request can be authenticated
  args_with_api_key = if args[0].is_a?(Hash)
                        (args[0]).merge({ api_key: @api_key })
                      elsif args[0]
                        { id: args[0] }.merge({ api_key: @api_key })
                      else
                        { api_key: @api_key }
                      end

  @wrapped_resource.send(method, args_with_api_key, &block)
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/the_one_api/resource_wrapper.rb', line 27

def respond_to_missing?(method, include_private = false)
  @wrapped_resource._calls.key?(method) || super
end