Class: ApiResource::Resource
- Inherits:
-
Object
- Object
- ApiResource::Resource
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.
31
32
33
|
# File 'lib/api-resource/resource.rb', line 31
def initialize(hash=nil)
self.attributes = hash if hash
end
|
Class Method Details
.all ⇒ Object
57
58
59
60
|
# File 'lib/api-resource/resource.rb', line 57
def self.all
result = client(:get, {}, resource_path)
create_resource_collection(result)
end
|
.all_pages(options = {}) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/api-resource/resource.rb', line 62
def self.all_pages(options={})
options = { page: 1, per_page: 500 }.merge(options)
per_page = options[:per_page].to_i
page = options[:page].to_i
begin
options[:page] = page
result = self.where(options)
total_count = result.try :total_count
arr = result.to_a
count = arr.length
break if yield(arr, page, per_page, total_count) || (page - 1) * per_page + count >= total_count
page += 1
end while true
end
|
.client(verb, params, *paths) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/api-resource/resource.rb', line 190
def self.client(verb, params, *paths)
raise RuntimeError, 'Empty base_url' if base_url.blank?
url = base_url
url += '/' unless url.end_with?('/')
url += paths.join('/')
= { accept: :json }
req_params = { url: url, method: verb, headers: }
if verb == :get
req_params[:url] = "#{url}?#{params.to_query}" unless params.to_query.blank?
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
180
181
182
183
|
# File 'lib/api-resource/resource.rb', line 180
def self.create_resource_collection(result)
json = JSON.parse(result)
ResourceCollection.new(json['data'], json['meta'], self)
end
|
.find(id) ⇒ Object
51
52
53
54
55
|
# File 'lib/api-resource/resource.rb', line 51
def self.find(id)
result = client(:get, {}, resource_path, id)
json = JSON.parse(result)
self.new(json['data'] || json)
end
|
.load_class(tableized_class) ⇒ Object
185
186
187
188
|
# File 'lib/api-resource/resource.rb', line 185
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
165
166
167
168
169
170
171
172
173
|
# File 'lib/api-resource/resource.rb', line 165
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_name ⇒ Object
43
44
45
|
# File 'lib/api-resource/resource.rb', line 43
def self.resource_name
name.to_s.tableize
end
|
.resource_path ⇒ Object
35
36
37
|
# File 'lib/api-resource/resource.rb', line 35
def self.resource_path
@resource_path || resource_name
end
|
.resource_path=(path) ⇒ Object
39
40
41
|
# File 'lib/api-resource/resource.rb', line 39
def self.resource_path=(path)
@resource_path = path
end
|
.where(options, verb = :get) ⇒ Object
77
78
79
80
|
# File 'lib/api-resource/resource.rb', line 77
def self.where(options, verb=:get)
result = client(verb, options, resource_path)
create_resource_collection(result)
end
|
.with_hmac(api_id, api_secret, options = {}) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/api-resource/resource.rb', line 24
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
175
176
177
178
|
# File 'lib/api-resource/resource.rb', line 175
def self.with_parent_resource(parent_resource_id, parent_resource_name)
result = client(:get, {}, parent_resource_name.pluralize, parent_resource_id, resource_path)
create_resource_collection(result)
end
|
Instance Method Details
#attributes ⇒ Object
106
107
108
|
# File 'lib/api-resource/resource.rb', line 106
def attributes
instance_values.with_indifferent_access
end
|
#destroy! ⇒ Object
101
102
103
104
|
# File 'lib/api-resource/resource.rb', line 101
def destroy!
self.class.client(:delete, id, resource_path) if self.id
self
end
|
#save! ⇒ Object
82
83
84
85
|
# File 'lib/api-resource/resource.rb', line 82
def save!
submit_resource(resource_path, true)
self
end
|
#submit!(options = {}) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/api-resource/resource.rb', line 87
def submit!(options={})
path = options.fetch(:path, resource_path)
type = options[:type]
json = submit_resource(path, false)
meta = json['meta']
type ||= meta && meta['type']
if type
returned_type = self.class.load_class(type)
returned_type.new(json['data'] || json)
else
self
end
end
|