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
Returns a new instance of Resource.
48
49
50
|
# File 'lib/api-resource/resource.rb', line 48
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
189
190
191
192
193
194
195
196
197
|
# File 'lib/api-resource/resource.rb', line 189
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
74
75
76
|
# File 'lib/api-resource/resource.rb', line 74
def self.all
self.where({})
end
|
.all_pages(params = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/api-resource/resource.rb', line 78
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
|
.destroy!(id) ⇒ Object
98
99
100
101
|
# File 'lib/api-resource/resource.rb', line 98
def self.destroy!(id)
client(:delete, {}, resource_path, id)
self
end
|
.filter_submitted_attributes(&block) ⇒ Object
44
45
46
|
# File 'lib/api-resource/resource.rb', line 44
def self.filter_submitted_attributes(&block)
self.pre_submit_filter = block if block_given?
end
|
.find(id, params = {}) ⇒ Object
68
69
70
71
72
|
# File 'lib/api-resource/resource.rb', line 68
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
52
53
54
|
# File 'lib/api-resource/resource.rb', line 52
def self.resource_path
@resource_path || name.to_s.tableize.split('/').last
end
|
.resource_path=(path) ⇒ Object
56
57
58
|
# File 'lib/api-resource/resource.rb', line 56
def self.resource_path=(path)
@resource_path = path.gsub(%r{\A/+|/+\Z}, '')
end
|
.where(params) ⇒ Object
93
94
95
96
|
# File 'lib/api-resource/resource.rb', line 93
def self.where(params)
result = client(:get, params, resource_path)
create_resource_collection(result)
end
|
.with_hmac(api_id, api_secret, options = {}) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/api-resource/resource.rb', line 37
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
127
128
129
|
# File 'lib/api-resource/resource.rb', line 127
def attributes
instance_values.with_indifferent_access.except(:errors, :validation_context, :resource_path)
end
|
#destroy!(options = {}) ⇒ Object
122
123
124
125
|
# File 'lib/api-resource/resource.rb', line 122
def destroy!(options={})
submit_resource(resource_path, false, options[:id_attr], :delete)
self
end
|
#resource_path ⇒ Object
60
61
62
|
# File 'lib/api-resource/resource.rb', line 60
def resource_path
@resource_path ||= self.class.resource_path
end
|
#resource_path=(path) ⇒ Object
64
65
66
|
# File 'lib/api-resource/resource.rb', line 64
def resource_path=(path)
@resource_path = path
end
|
#save!(options = {}) ⇒ Object
103
104
105
106
|
# File 'lib/api-resource/resource.rb', line 103
def save!(options={})
submit_resource(resource_path, true, options[:id_attr])
self
end
|
#submit!(options = {}) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/api-resource/resource.rb', line 108
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
|