Class: Aptible::Resource::Base

Inherits:
HyperResource show all
Defined in:
lib/aptible/resource/base.rb

Overview

rubocop:disable ClassLength rubocop:disable DuplicateMethods

Constant Summary

Constants inherited from HyperResource

HyperResource::VERSION, HyperResource::VERSION_DATE

Constants included from HyperResource::Modules::HTTP

HyperResource::Modules::HTTP::CONTENT_TYPE_HEADERS, HyperResource::Modules::HTTP::MAX_COORDINATOR_RETRIES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HyperResource

#[], #_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

Methods included from HyperResource::Modules::InternalAttributes

included

Methods included from HyperResource::Modules::HTTP

#create, #get, initialize_http_client!, #patch, #post, #put

Constructor Details

#initialize(options = {}) ⇒ Base

rubocop:disable ReturnInVoidContext



232
233
234
235
236
237
238
# File 'lib/aptible/resource/base.rb', line 232

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

#errorsObject

Returns the value of attribute errors.



20
21
22
# File 'lib/aptible/resource/base.rb', line 20

def errors
  @errors
end

#tokenObject

Returns the value of attribute token.



21
22
23
# File 'lib/aptible/resource/base.rb', line 21

def token
  @token
end

Class Method Details

.adapterObject



29
30
31
# File 'lib/aptible/resource/base.rb', line 29

def self.adapter
  Aptible::Resource::Adapter
end

.all(options = {}) ⇒ Object



60
61
62
63
64
# File 'lib/aptible/resource/base.rb', line 60

def self.all(options = {})
  out = []
  each_page(options) { |page| out.concat page }
  out
end

.basenameObject



37
38
39
# File 'lib/aptible/resource/base.rb', line 37

def self.basename
  name.split('::').last.underscore.pluralize
end

.belongs_to(relation) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/aptible/resource/base.rb', line 121

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



221
222
223
224
225
226
227
228
229
# File 'lib/aptible/resource/base.rb', line 221

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_hrefObject



33
34
35
# File 'lib/aptible/resource/base.rb', line 33

def self.collection_href
  "/#{basename}"
end

.create(params = {}) ⇒ Object



94
95
96
97
98
# File 'lib/aptible/resource/base.rb', line 94

def self.create(params = {})
  create!(params)
rescue HyperResource::ResponseError => e
  new.tap { |resource| resource.errors = Errors.from_exception(e) }
end

.create!(params = {}) ⇒ Object



88
89
90
91
92
# File 'lib/aptible/resource/base.rb', line 88

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/aptible/resource/base.rb', line 178

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/aptible/resource/base.rb', line 141

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: 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: headers
    ) do |page|
      page.each { |entry| block.call entry }
    end
  end
end

.define_has_many_setter(relation) ⇒ Object

rubocop:disable MethodLength rubocop:disable AbcSize



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/aptible/resource/base.rb', line 195

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aptible/resource/base.rb', line 41

def self.each_page(options = {})
  return enum_for(:each_page, options) unless block_given?

  href = options[:href] || collection_href
  while href
    # TODO: Breaking here is consistent with the existing behavior of
    # .all, but it essentially swallows an error if the page you're
    # hitting happens to 404. This should probably be addressed in an
    # effort to make this API client more strict.
    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



107
108
109
110
# File 'lib/aptible/resource/base.rb', line 107

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



171
172
173
174
175
176
# File 'lib/aptible/resource/base.rb', line 171

def self.embeds_one(relation)
  define_method relation do
    get unless loaded
    objects[relation]
  end
end

.field(name, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/aptible/resource/base.rb', line 112

def self.field(name, options = {})
  define_method name do
    self.class.cast_field(attributes[name], options[:type])
  end

  # Define ? accessor for Boolean attributes
  define_method("#{name}?") { !!send(name) } if options[:type] == Boolean
end

.find(id, options = {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/aptible/resource/base.rb', line 72

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



79
80
81
82
83
84
85
86
# File 'lib/aptible/resource/base.rb', line 79

def self.find_by_url(url, options = {})
  # REVIEW: Should exception be raised if return type mismatch?
  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



23
24
25
26
27
# File 'lib/aptible/resource/base.rb', line 23

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



101
102
103
104
# File 'lib/aptible/resource/base.rb', line 101

def self.has_many(relation)
  define_has_many_getters(relation)
  define_has_many_setter(relation)
end

.has_one(relation) ⇒ Object

rubocop:disable PredicateName



133
134
135
136
# File 'lib/aptible/resource/base.rb', line 133

def self.has_one(relation)
  # Better than class << self + alias_method?
  belongs_to(relation)
end

.normalize_params(params = {}) ⇒ Object

rubocop: enable AbcSize rubocop:enable MethodLength



214
215
216
217
218
219
# File 'lib/aptible/resource/base.rb', line 214

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



66
67
68
69
70
# File 'lib/aptible/resource/base.rb', line 66

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_updateObject

rubocop:disable Style/Alias



280
# File 'lib/aptible/resource/base.rb', line 280

alias_method :_hyperresource_update, :update

#adapterObject



253
254
255
# File 'lib/aptible/resource/base.rb', line 253

def adapter
  self.class.adapter
end

#bearer_tokenObject



271
272
273
274
275
276
277
# File 'lib/aptible/resource/base.rb', line 271

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

#deleteObject Also known as: destroy



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/aptible/resource/base.rb', line 296

def delete
  super
rescue HyperResource::ServerError
  raise
rescue HyperResource::ClientError => e
  # Already deleted
  raise unless e.response.status == 404
rescue HyperResource::ResponseError
  # HyperResource chokes on empty response bodies
  nil
end

#error_htmlObject



321
322
323
# File 'lib/aptible/resource/base.rb', line 321

def error_html
  errors.full_messages.join('<br />')
end

#find_by_url(url_or_href) ⇒ Object



265
266
267
268
269
# File 'lib/aptible/resource/base.rb', line 265

def find_by_url(url_or_href)
  resource = dup
  resource.href = url_or_href.gsub(/^#{root}/, '')
  resource.get
end

#namespaceObject



257
258
259
# File 'lib/aptible/resource/base.rb', line 257

def namespace
  raise 'Resource server namespace must be defined by subclass'
end

#populate_default_options!(options) ⇒ Object

rubocop:enable ReturnInVoidContext



241
242
243
244
245
246
# File 'lib/aptible/resource/base.rb', line 241

def populate_default_options!(options)
  options[:root] ||= root_url
  options[:namespace] ||= namespace
  options[:headers] ||= {}
  options[:headers]['Content-Type'] = 'application/json'
end

#reloadObject

NOTE: The following does not update the object in-place



313
314
315
# File 'lib/aptible/resource/base.rb', line 313

def reload
  self.class.find_by_url(href, token: token, headers: headers)
end

#root_urlObject



261
262
263
# File 'lib/aptible/resource/base.rb', line 261

def root_url
  raise 'Resource server root URL must be defined by subclass'
end

#update(params) ⇒ Object



290
291
292
293
294
# File 'lib/aptible/resource/base.rb', line 290

def update(params)
  update!(params)
rescue HyperResource::ResponseError
  false
end

#update!(params) ⇒ Object

rubocop:enable Style/Alias



283
284
285
286
287
288
# File 'lib/aptible/resource/base.rb', line 283

def update!(params)
  _hyperresource_update(self.class.normalize_params(params))
rescue HyperResource::ResponseError => e
  self.errors = Errors.from_exception(e)
  raise e
end