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.
26
27
28
|
# File 'lib/api-resource/resource.rb', line 26
def initialize(hash=nil)
self.attributes = hash if hash
end
|
Class Method Details
.all ⇒ Object
40
41
42
43
|
# File 'lib/api-resource/resource.rb', line 40
def self.all
result = client(:get, {}, resource_name)
create_resource_collection(result)
end
|
.client(verb, params, *paths) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/api-resource/resource.rb', line 110
def self.client(verb, params, *paths)
url = base_url
url += '/' unless url.end_with?('/')
url += paths.join('/')
= { accept: :json }
req_params = { url: url, method: verb, headers: }
if verb == :get
[: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
100
101
102
103
|
# File 'lib/api-resource/resource.rb', line 100
def self.create_resource_collection(result)
json = JSON.parse(result)
ResourceCollection.new(json['data'], json['meta'], self)
end
|
.find(id) ⇒ Object
34
35
36
37
38
|
# File 'lib/api-resource/resource.rb', line 34
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
105
106
107
108
|
# File 'lib/api-resource/resource.rb', line 105
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
85
86
87
88
89
90
91
92
93
|
# File 'lib/api-resource/resource.rb', line 85
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
30
31
32
|
# File 'lib/api-resource/resource.rb', line 30
def self.resource_name
self.to_s.tableize
end
|
.where(options, verb = :get) ⇒ Object
46
47
48
49
|
# File 'lib/api-resource/resource.rb', line 46
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
19
20
21
22
23
24
|
# File 'lib/api-resource/resource.rb', line 19
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
95
96
97
98
|
# File 'lib/api-resource/resource.rb', line 95
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
#attributes ⇒ Object
51
52
53
|
# File 'lib/api-resource/resource.rb', line 51
def attributes
instance_values.with_indifferent_access
end
|