Class: FetchAppAPI::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fetchapp-api-ruby/base.rb

Direct Known Subclasses

Account, Download, File, Order, OrderItem, Product, Upload

Defined Under Namespace

Classes: Connector

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_or_attributes) ⇒ Base

:nodoc:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fetchapp-api-ruby/base.rb', line 13

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 object



125
126
127
128
# File 'lib/fetchapp-api-ruby/base.rb', line 125

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

Class Method Details

.basic_auth(params = {}) ⇒ Object

Initializes the connection to the API



63
64
65
66
67
68
69
# File 'lib/fetchapp-api-ruby/base.rb', line 63

def self.basic_auth(params={})
  return false if params[:key].blank? || params[:token].blank?

  @key = params[:key] # Save this in case they generate a new token later
  Connector.base_uri((params[:url] || 'app.fetchapp.com')+"/api/v2")
  Connector.basic_auth(params[:key], params[:token])
end

.can_connect?Boolean

Determines whether or not we can connect to the API by calling the time method

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/fetchapp-api-ruby/base.rb', line 42

def self.can_connect?
  begin
    result = execute(:get, "/time")["time"]
    return !(result.empty? || result.nil?)
  rescue
    return false
  end

end

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

:nodoc:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fetchapp-api-ruby/base.rb', line 25

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

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

.keyObject

:nodoc:



71
72
73
# File 'lib/fetchapp-api-ruby/base.rb', line 71

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

.timeObject

Returns the server time



53
54
55
# File 'lib/fetchapp-api-ruby/base.rb', line 53

def self.time
  return execute(:get, "/time")["time"]
end