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.
27
28
29
|
# File 'lib/api-resource/resource.rb', line 27
def initialize(hash=nil)
self.attributes = hash if hash
end
|
Class Method Details
.all ⇒ Object
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
|
.all_pages(options = {}) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/api-resource/resource.rb', line 46
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
|
.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
|
.resource_name ⇒ Object
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
61
62
63
64
|
# File 'lib/api-resource/resource.rb', line 61
def self.where(options, verb=:get)
result = client(verb, 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
|
Instance Method Details
#attributes ⇒ Object
66
67
68
|
# File 'lib/api-resource/resource.rb', line 66
def attributes
instance_values.with_indifferent_access
end
|