Class: Aptible::Resource::Base
Overview
rubocop:disable ClassLength
Constant Summary
HyperResource::VERSION, HyperResource::VERSION_DATE
HyperResource::Modules::HTTP::MAX_COORDINATOR_RETRIES
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #_hr_new_from_link, #_hr_response_class, #changed?, #deserialized_response, #each, #get_data_type_from_response, #incoming_body_filter, #inspect, #method_missing, namespaced_class, #outgoing_body_filter, #outgoing_uri_filter, #response_body, response_class, #response_object
included
#create, #faraday_connection, #get, #patch, #post, #put
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
237
238
239
240
241
242
243
|
# File 'lib/aptible/resource/base.rb', line 237
def initialize(options = {})
return super(options) unless options.is_a?(Hash)
populate_default_options!(options)
super(options)
self.token = options[:token] if options[:token]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class HyperResource
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
19
20
21
|
# File 'lib/aptible/resource/base.rb', line 19
def errors
@errors
end
|
#token ⇒ Object
Returns the value of attribute token.
20
21
22
|
# File 'lib/aptible/resource/base.rb', line 20
def token
@token
end
|
Class Method Details
.all(options = {}) ⇒ Object
58
59
60
61
62
|
# File 'lib/aptible/resource/base.rb', line 58
def self.all(options = {})
out = []
each_page(options) { |page| out.concat page }
out
end
|
.basename ⇒ Object
35
36
37
|
# File 'lib/aptible/resource/base.rb', line 35
def self.basename
name.split('::').last.underscore.pluralize
end
|
.belongs_to(relation) ⇒ Object
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/aptible/resource/base.rb', line 118
def self.belongs_to(relation)
define_method relation do
get unless loaded
if (memoized = instance_variable_get("@#{relation}"))
memoized
elsif links[relation]
instance_variable_set("@#{relation}", links[relation].get)
end
end
end
|
.cast_field(value, type) ⇒ Object
217
218
219
220
221
222
223
224
225
|
# File 'lib/aptible/resource/base.rb', line 217
def self.cast_field(value, type)
if type == Time
Time.parse(value) if value
elsif type == DateTime
DateTime.parse(value) if value
else
value
end
end
|
.collection_href ⇒ Object
31
32
33
|
# File 'lib/aptible/resource/base.rb', line 31
def self.collection_href
"/#{basename}"
end
|
.create(params = {}) ⇒ Object
.create!(params = {}) ⇒ Object
85
86
87
88
89
|
# File 'lib/aptible/resource/base.rb', line 85
def self.create!(params = {})
token = params.delete(:token)
resource = new(token: token)
resource.send(basename).create(normalize_params(params))
end
|
.define_embeds_many_getters(relation) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/aptible/resource/base.rb', line 175
def self.define_embeds_many_getters(relation)
define_method relation do
get unless loaded
objects[relation].entries
end
iterator_method = "each_#{relation.to_s.singularize}".to_sym
define_method iterator_method do |&block|
next enum_for(iterator_method) if block.nil?
send(relation).each(&block)
end
end
|
.define_has_many_getters(relation) ⇒ Object
rubocop:disable MethodLength rubocop:disable AbcSize
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/aptible/resource/base.rb', line 138
def self.define_has_many_getters(relation)
define_method relation do
get unless loaded
if (memoized = instance_variable_get("@#{relation}"))
memoized
elsif links[relation]
depaginated = self.class.all(href: links[relation].base_href,
token: token,
headers: )
instance_variable_set("@#{relation}", depaginated)
end
end
iterator_method = "each_#{relation.to_s.singularize}".to_sym
define_method iterator_method do |&block|
next enum_for(iterator_method) if block.nil?
self.class.each_page(
href: links[relation].base_href,
token: token,
headers:
) do |page|
page.each { |entry| block.call entry }
end
end
end
|
.define_has_many_setter(relation) ⇒ Object
rubocop:disable MethodLength rubocop:disable AbcSize
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/aptible/resource/base.rb', line 191
def self.define_has_many_setter(relation)
define_method "create_#{relation.to_s.singularize}!" do |params = {}|
get unless loaded
links[relation].create(self.class.normalize_params(params))
end
define_method "create_#{relation.to_s.singularize}" do |params = {}|
begin
send "create_#{relation.to_s.singularize}!", params
rescue HyperResource::ResponseError => e
Base.new(root: root_url, namespace: namespace).tap do |base|
base.errors = Errors.from_exception(e)
end
end
end
end
|
.each_page(options = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/aptible/resource/base.rb', line 39
def self.each_page(options = {})
return enum_for(:each_page, options) unless block_given?
href = options[:href] || collection_href
while href
resource = find_by_url(href, options)
break if resource.nil?
yield resource.entries
next_link = resource.links['next']
href = next_link ? next_link.href : nil
end
end
|
.embeds_many(relation) ⇒ Object
rubocop:enable PredicateName
104
105
106
107
|
# File 'lib/aptible/resource/base.rb', line 104
def self.embeds_many(relation)
define_embeds_many_getters(relation)
define_has_many_setter(relation)
end
|
.embeds_one(relation) ⇒ Object
rubocop:enable AbcSize rubocop:enable MethodLength
168
169
170
171
172
173
|
# File 'lib/aptible/resource/base.rb', line 168
def self.embeds_one(relation)
define_method relation do
get unless loaded
objects[relation]
end
end
|
.faraday_options ⇒ Object
227
228
229
230
231
232
233
234
235
|
# File 'lib/aptible/resource/base.rb', line 227
def self.faraday_options
{
request: {
open_timeout: 10
}
}
end
|
.field(name, options = {}) ⇒ Object
109
110
111
112
113
114
115
116
|
# File 'lib/aptible/resource/base.rb', line 109
def self.field(name, options = {})
define_method name do
self.class.cast_field(attributes[name], options[:type])
end
define_method("#{name}?") { !!send(name) } if options[:type] == Boolean
end
|
.find(id, options = {}) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/aptible/resource/base.rb', line 70
def self.find(id, options = {})
params = options.except(:token, :root, :namespace, :headers)
params = normalize_params(params)
params = params.empty? ? '' : '?' + params.to_query
find_by_url("#{collection_href}/#{id}#{params}", options)
end
|
.find_by_url(url, options = {}) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/aptible/resource/base.rb', line 77
def self.find_by_url(url, options = {})
new(options).find_by_url(url)
rescue HyperResource::ClientError => e
return nil if e.response.status == 404
raise e
end
|
.get_data_type_from_response(response) ⇒ Object
22
23
24
25
|
# File 'lib/aptible/resource/base.rb', line 22
def self.get_data_type_from_response(response)
return nil unless response && response.body
adapter.get_data_type_from_object(adapter.deserialize(response.body))
end
|
.has_many(relation) ⇒ Object
rubocop:disable PredicateName
98
99
100
101
|
# File 'lib/aptible/resource/base.rb', line 98
def self.has_many(relation)
define_has_many_getters(relation)
define_has_many_setter(relation)
end
|
.has_one(relation) ⇒ Object
rubocop:disable PredicateName
130
131
132
133
|
# File 'lib/aptible/resource/base.rb', line 130
def self.has_one(relation)
belongs_to(relation)
end
|
.normalize_params(params = {}) ⇒ Object
rubocop: enable AbcSize rubocop:enable MethodLength
210
211
212
213
214
215
|
# File 'lib/aptible/resource/base.rb', line 210
def self.normalize_params(params = {})
params_array = params.map do |key, value|
value.is_a?(HyperResource) ? [key, value.href] : [key, value]
end
Hash[params_array]
end
|
.where(options = {}) ⇒ Object
64
65
66
67
68
|
# File 'lib/aptible/resource/base.rb', line 64
def self.where(options = {})
params = options.except(:token, :root, :namespace, :headers)
params = normalize_params(params)
find_by_url("#{collection_href}?#{params.to_query}", options).entries
end
|
Instance Method Details
#_hyperresource_update ⇒ Object
rubocop:disable Style/Alias
284
|
# File 'lib/aptible/resource/base.rb', line 284
alias_method :_hyperresource_update, :update
|
#adapter ⇒ Object
257
258
259
|
# File 'lib/aptible/resource/base.rb', line 257
def adapter
self.class.adapter
end
|
#bearer_token ⇒ Object
275
276
277
278
279
280
281
|
# File 'lib/aptible/resource/base.rb', line 275
def bearer_token
case token
when Aptible::Resource::Base then token.access_token
when Fridge::AccessToken then token.to_s
when String then token
end
end
|
#delete ⇒ Object
Also known as:
destroy
#error_html ⇒ Object
325
326
327
|
# File 'lib/aptible/resource/base.rb', line 325
def error_html
errors.full_messages.join('<br />')
end
|
#find_by_url(url_or_href) ⇒ Object
269
270
271
272
273
|
# File 'lib/aptible/resource/base.rb', line 269
def find_by_url(url_or_href)
resource = dup
resource.href = url_or_href.gsub(/^#{root}/, '')
resource.get
end
|
#namespace ⇒ Object
261
262
263
|
# File 'lib/aptible/resource/base.rb', line 261
def namespace
raise 'Resource server namespace must be defined by subclass'
end
|
#populate_default_options!(options) ⇒ Object
245
246
247
248
249
250
|
# File 'lib/aptible/resource/base.rb', line 245
def populate_default_options!(options)
options[:root] ||= root_url
options[:namespace] ||= namespace
options[:headers] ||= {}
options[:headers]['Content-Type'] = 'application/json'
end
|
#reload ⇒ Object
NOTE: The following does not update the object in-place
317
318
319
|
# File 'lib/aptible/resource/base.rb', line 317
def reload
self.class.find_by_url(href, token: token, headers: )
end
|
#root_url ⇒ Object
265
266
267
|
# File 'lib/aptible/resource/base.rb', line 265
def root_url
raise 'Resource server root URL must be defined by subclass'
end
|
#update(params) ⇒ Object
294
295
296
297
298
|
# File 'lib/aptible/resource/base.rb', line 294
def update(params)
update!(params)
rescue HyperResource::ResponseError
false
end
|
#update!(params) ⇒ Object
rubocop:enable Style/Alias
287
288
289
290
291
292
|
# File 'lib/aptible/resource/base.rb', line 287
def update!(params)
_hyperresource_update(self.class.normalize_params(params))
rescue HyperResource::ResponseError => e
self.errors = Errors.from_exception(e)
raise e
end
|