Class: ApiResource::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/api-resource/resource.rb

Defined Under Namespace

Classes: ResourceCollection

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ Resource

Returns a new instance of Resource.



27
28
29
# File 'lib/api-resource/resource.rb', line 27

def initialize(hash=nil)
  self.attributes = hash if hash
end

Class Method Details

.allObject



41
42
43
44
# File 'lib/api-resource/resource.rb', line 41

def self.all
  result = client(:get, {}, resource_name)
  create_resource_collection(result)
end

.client(verb, params, *paths) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/api-resource/resource.rb', line 111

def self.client(verb, params, *paths)
  url        = base_url
  url        += '/' unless url.end_with?('/')
  url        += paths.join('/')
  headers    = { accept: :json }
  req_params = { url: url, method: verb, headers: headers }
  if verb == :get
    headers[:params] = params
  else
    req_params[:payload] = params
  end
  req = RestClient::Request.new(req_params)
  if hmac
    req.sign!(api_id, api_secret, hmac_options)
  end
  req.execute
end

.create_resource_collection(result) ⇒ Object



101
102
103
104
# File 'lib/api-resource/resource.rb', line 101

def self.create_resource_collection(result)
  json = JSON.parse(result)
  ResourceCollection.new(json['data'], json['meta'], self)
end

.find(id) ⇒ Object



35
36
37
38
39
# File 'lib/api-resource/resource.rb', line 35

def self.find(id)
  result = client(:get, {}, resource_name, id)
  json   = JSON.parse(result)
  self.new(json['data'] || json)
end

.load_class(tableized_class) ⇒ Object



106
107
108
109
# File 'lib/api-resource/resource.rb', line 106

def self.load_class(tableized_class)
  klass = tableized_class && tableized_class.to_s.singularize.classify.constantize rescue nil
  klass if klass < ApiResource::Resource
end

.method_missing(m, *args, &_) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/api-resource/resource.rb', line 86

def self.method_missing(m, *args, &_)
  unless args.empty?
    by_method_match = /^by_(.+)/.match(m)
    if by_method_match
      parent_resource_name = by_method_match[1]
      with_parent_resource(args[0], parent_resource_name)
    end
  end
end

.resource_nameObject



31
32
33
# File 'lib/api-resource/resource.rb', line 31

def self.resource_name
  self.to_s.tableize
end

.where(options, verb = :get) ⇒ Object



47
48
49
50
# File 'lib/api-resource/resource.rb', line 47

def self.where(options, verb=:get)
  result = client(verb, *(verb==:get ? [{}, "#{resource_name}?#{options.to_param}"] : [options, resource_name]))
  create_resource_collection(result)
end

.with_hmac(api_id, api_secret, options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/api-resource/resource.rb', line 20

def self.with_hmac(api_id, api_secret, options={})
  self.hmac         = true
  self.api_id       = api_id
  self.api_secret   = api_secret
  self.hmac_options = options
end

.with_parent_resource(parent_resource_id, parent_resource_name) ⇒ Object



96
97
98
99
# File 'lib/api-resource/resource.rb', line 96

def self.with_parent_resource(parent_resource_id, parent_resource_name)
  result = client(:get, {}, parent_resource_name.pluralize, parent_resource_id, self.resource_name)
  create_resource_collection(result)
end

Instance Method Details

#attributesObject



52
53
54
# File 'lib/api-resource/resource.rb', line 52

def attributes
  instance_values.with_indifferent_access
end