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

:nodoc:



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

def initialize(id_or_attributes) #:nodoc:
  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



95
96
97
98
# File 'lib/fetchapi/base.rb', line 95

def method_missing(method) #:nodoc:
  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 to the API



37
38
39
40
41
# File 'lib/fetchapi/base.rb', line 37

def self.basic_auth(url, key, token)
  @key = key # Save this in case they generate a new token later
  Connector.base_uri(url)
  Connector.basic_auth(key, token)
end

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

:nodoc:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fetchapi/base.rb', line 16

def self.find(selector, params={}) #:nodoc:
  case selector
  when :all
    objects = execute(:get, "/#{pluralized_class_name}")

    if objects[pluralized_class_name].blank?
      return []
    else
      objects[pluralized_class_name].map { |data| new(data) }
    end
  when Integer, String
    new(selector)
  end
end

.keyObject

:nodoc:



43
44
45
# File 'lib/fetchapi/base.rb', line 43

def self.key #:nodoc:
  return @key
end