Class: FetchAPI::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fetchapi/base.rb

Direct Known Subclasses

Account, Download, Item, Order

Defined Under Namespace

Classes: Connector

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_or_attributes) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/fetchapi/base.rb', line 4

def initialize(id_or_attributes)  
  case id_or_attributes
  when Integer, String
    @attributes = get("/#{self.class.pluralized_class_name}/#{id_or_attributes.to_s}")
@attributes = @attributes[self.class.singularized_class_name]
@id = @attributes['id']
  when Hash
    @attributes = id_or_attributes
@id = id_or_attributes['id']
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object (protected)

Access attributes as class methods of the Item object



73
74
75
76
# File 'lib/fetchapi/base.rb', line 73

def method_missing(method)
  return super unless attributes.has_key?(method.to_s)
  attributes[method.to_s]
end

Class Method Details

.basic_auth(url, key, token) ⇒ Object

Initializes the connection



31
32
33
34
# File 'lib/fetchapi/base.rb', line 31

def self.basic_auth(url, key, token)
  Connector.base_uri(url)
  Connector.basic_auth(key, token)
end

.find(selector, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/fetchapi/base.rb', line 16

def self.find(selector, params={})
  case selector
  when :all
    execute(:get, "/#{pluralized_class_name}")[pluralized_class_name].map { |data| new(data) }
  when Integer, String
    new(selector)
  end
end