Class: NameDrop::Resources::Base
- Inherits:
-
Object
- Object
- NameDrop::Resources::Base
- Defined in:
- lib/name_drop/resources/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
- .all(client, params = {}) ⇒ Object
- .build(client, attributes = {}) ⇒ Object
- .find(client, id) ⇒ Object
- .response_key ⇒ Object
Instance Method Summary collapse
- #destroy(params = {}) ⇒ Object
-
#initialize(client, attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #save ⇒ Object
Constructor Details
#initialize(client, attributes = {}) ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 |
# File 'lib/name_drop/resources/base.rb', line 11 def initialize(client, attributes = {}) @client = client @attributes = attributes @errors = [] end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/name_drop/resources/base.rb', line 8 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
9 10 11 |
# File 'lib/name_drop/resources/base.rb', line 9 def errors @errors end |
Class Method Details
.all(client, params = {}) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/name_drop/resources/base.rb', line 17 def self.all(client, params = {}) response = client.get(endpoint(params)) response[response_key.pluralize].map do |attributes| new(client, attributes) end end |
.build(client, attributes = {}) ⇒ Object
33 34 35 |
# File 'lib/name_drop/resources/base.rb', line 33 def self.build(client, attributes = {}) new(client, attributes) end |
.find(client, id) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/name_drop/resources/base.rb', line 24 def self.find(client, id) response = client.get("#{endpoint}/#{id}") if response[response_key].present? new(client, response[response_key]) else response end end |
.response_key ⇒ Object
53 54 55 |
# File 'lib/name_drop/resources/base.rb', line 53 def self.response_key name.demodulize.downcase end |
Instance Method Details
#destroy(params = {}) ⇒ Object
49 50 51 |
# File 'lib/name_drop/resources/base.rb', line 49 def destroy(params = {}) client.delete("#{endpoint(params)}/#{attributes['id']}") end |
#save ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/name_drop/resources/base.rb', line 37 def save path = new_record? ? endpoint : "#{endpoint}/#{attributes['id']}" response = client.send(persistence_action, path, attributes) if response[response_key].present? self.attributes = response[response_key] true else @errors = response false end end |