Class: ApiResource::Resource
- Inherits:
-
Object
- Object
- ApiResource::Resource
show all
- Includes:
- ActiveModel::Model
- Defined in:
- lib/api-resource/resource.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = nil) ⇒ Resource
42
43
44
|
# File 'lib/api-resource/resource.rb', line 42
def initialize(hash=nil)
self.attributes = hash if hash
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &_) ⇒ Object
173
174
175
176
177
178
179
180
181
|
# File 'lib/api-resource/resource.rb', line 173
def method_missing(m, *args, &_)
case m
when /^by_(.+)/
self.resource_path = self.class.build_path($1.pluralize, args[0], resource_path)
self
else
super
end
end
|
Class Method Details
.all ⇒ Object
68
69
70
|
# File 'lib/api-resource/resource.rb', line 68
def self.all
self.where({})
end
|
.all_pages(params = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/api-resource/resource.rb', line 72
def self.all_pages(params={})
params = { page: 1, per_page: default_per_page }.merge(params)
per_page = params[:per_page].to_i
page = params[:page].to_i
begin
params[:page] = page
result = self.where(params)
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
|
.filter_submitted_attributes(&block) ⇒ Object
38
39
40
|
# File 'lib/api-resource/resource.rb', line 38
def self.filter_submitted_attributes(&block)
self.pre_submit_filter = block if block_given?
end
|
.find(id, params = {}) ⇒ Object
62
63
64
65
66
|
# File 'lib/api-resource/resource.rb', line 62
def self.find(id, params={})
result = client(:get, params, resource_path, id)
json = JSON.parse(result)
self.new(json['data'] || json)
end
|
.resource_path ⇒ Object
46
47
48
|
# File 'lib/api-resource/resource.rb', line 46
def self.resource_path
@resource_path || name.to_s.tableize.split('/').last
end
|
.resource_path=(path) ⇒ Object
50
51
52
|
# File 'lib/api-resource/resource.rb', line 50
def self.resource_path=(path)
@resource_path = path.gsub(%r{\A/+|/+\Z}, '')
end
|
.where(params) ⇒ Object
87
88
89
90
|
# File 'lib/api-resource/resource.rb', line 87
def self.where(params)
result = client(:get, params, resource_path)
create_resource_collection(result)
end
|
.with_hmac(api_id, api_secret, options = {}) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/api-resource/resource.rb', line 31
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
116
117
118
|
# File 'lib/api-resource/resource.rb', line 116
def attributes
instance_values.with_indifferent_access.except(:errors, :validation_context, :resource_path)
end
|
#destroy!(options = {}) ⇒ Object
111
112
113
114
|
# File 'lib/api-resource/resource.rb', line 111
def destroy!(options={})
submit_resource(resource_path, false, options[:id_attr], :delete)
self
end
|
#resource_path ⇒ Object
54
55
56
|
# File 'lib/api-resource/resource.rb', line 54
def resource_path
@resource_path ||= self.class.resource_path
end
|
#resource_path=(path) ⇒ Object
58
59
60
|
# File 'lib/api-resource/resource.rb', line 58
def resource_path=(path)
@resource_path = path
end
|
#save!(options = {}) ⇒ Object
92
93
94
95
|
# File 'lib/api-resource/resource.rb', line 92
def save!(options={})
submit_resource(resource_path, true, options[:id_attr])
self
end
|
#submit!(options = {}) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/api-resource/resource.rb', line 97
def submit!(options={})
path = options.fetch(:path, resource_path)
type = options[:type]
json = submit_resource(path, false, options[:id_attr])
meta = json && 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
|