Class: Aptible::Api::Resource
- Inherits:
-
Api
- Object
- Api
- Aptible::Api::Resource
- Defined in:
- lib/aptible/api/resource.rb
Class Method Summary collapse
- .all(options = {}) ⇒ Object
- .basename ⇒ Object
- .collection_url ⇒ Object
- .create(options) ⇒ Object
- .define_has_many_getter(relation) ⇒ Object
- .define_has_many_setter(relation) ⇒ Object
- .find(id) ⇒ Object
- .find_by_url(url) ⇒ Object
-
.has_many(relation) ⇒ Object
rubocop:disable PredicateName.
Class Method Details
.all(options = {}) ⇒ Object
14 15 16 17 |
# File 'lib/aptible/api/resource.rb', line 14 def self.all( = {}) resource = new().find_by_url(collection_url) resource.send(basename).entries end |
.basename ⇒ Object
5 6 7 |
# File 'lib/aptible/api/resource.rb', line 5 def self.basename name.split('::').last.downcase.pluralize end |
.collection_url ⇒ Object
9 10 11 12 |
# File 'lib/aptible/api/resource.rb', line 9 def self.collection_url config = Aptible::Api.configuration config.root_url.chomp('/') + "/#{basename}" end |
.create(options) ⇒ Object
30 31 32 33 34 |
# File 'lib/aptible/api/resource.rb', line 30 def self.create() token = .delete(:token) auth = Api.new(token: token) auth.send(basename).create() end |
.define_has_many_getter(relation) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/aptible/api/resource.rb', line 45 def self.define_has_many_getter(relation) define_method relation do get unless loaded if (memoized = instance_variable_get("@#{relation}")) memoized elsif links[relation] instance_variable_set("@#{relation}", links[relation].entries) end end end |
.define_has_many_setter(relation) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/aptible/api/resource.rb', line 56 def self.define_has_many_setter(relation) define_method "create_#{relation.to_s.singularize}" do | = {}| get unless loaded links[relation].create() end end |
.find(id) ⇒ Object
19 20 21 |
# File 'lib/aptible/api/resource.rb', line 19 def self.find(id) find_by_url("#{collection_url}/#{id}") end |
.find_by_url(url) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/aptible/api/resource.rb', line 23 def self.find_by_url(url) # REVIEW: Should exception be raised if return type mismatch? new.find_by_url(url) rescue nil end |
.has_many(relation) ⇒ Object
rubocop:disable PredicateName
37 38 39 40 |
# File 'lib/aptible/api/resource.rb', line 37 def self.has_many(relation) define_has_many_getter(relation) define_has_many_setter(relation) end |