Class: Mondido::BaseModel
- Inherits:
-
Object
- Object
- Mondido::BaseModel
- Defined in:
- lib/mondido/base_model.rb
Direct Known Subclasses
CreditCard::Refund, CreditCard::StoredCard, CreditCard::Transaction
Class Method Summary collapse
-
.all(filter = {}) ⇒ Array
Retrieves a list of objects from the API.
-
.create(attributes) ⇒ Mondido::*
Creates a transaction and posts it to the API.
- .delete(id) ⇒ Mondido::*
-
.get(id) ⇒ Mondido::*
Retrieves an object from the API, by ID.
Instance Method Summary collapse
-
#api_params ⇒ Hash
Returns a sanitized hash suitable for posting to the API.
-
#update_from_response(attributes) ⇒ Mondido::*
Takes the JSON response from the API and sets white listed properties for the instance.
Class Method Details
.all(filter = {}) ⇒ Array
Retrieves a list of objects from the API
57 58 59 60 61 62 63 |
# File 'lib/mondido/base_model.rb', line 57 def self.all(filter={}) response = Mondido::RestClient.all(pluralized, filter) JSON.parse(response.body).map do |attributes| object = self.new object.update_from_response(attributes) end end |
.create(attributes) ⇒ Mondido::*
Creates a transaction and posts it to the API
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mondido/base_model.rb', line 34 def self.create(attributes) object = self.new(attributes) object.valid? # Will raise exception if validation fails object.set_hash! if object.respond_to? :set_hash! response = Mondido::RestClient.process(object) object.update_from_response(JSON.parse(response.body)) end |
.delete(id) ⇒ Mondido::*
67 68 69 70 71 |
# File 'lib/mondido/base_model.rb', line 67 def self.delete(id) response = Mondido::RestClient.delete(pluralized, id) object = self.new object.update_from_response(JSON.parse(response.body)) end |
.get(id) ⇒ Mondido::*
Retrieves an object from the API, by ID
48 49 50 51 52 |
# File 'lib/mondido/base_model.rb', line 48 def self.get(id) response = Mondido::RestClient.get(pluralized, id) object = self.new object.update_from_response(JSON.parse(response.body)) end |
Instance Method Details
#api_params ⇒ Hash
Returns a sanitized hash suitable for posting to the API
19 20 21 22 23 24 25 26 27 |
# File 'lib/mondido/base_model.rb', line 19 def api_params excluded = %w(@errors @validation_context).map(&:to_sym) hash = {} instance_variables.reject{|o| excluded.include?(o) }.each do |var_sym| var_sym_without_at = var_sym.to_s[1, var_sym.length-1].to_sym hash[var_sym_without_at] = instance_variable_get(var_sym) end return hash end |
#update_from_response(attributes) ⇒ Mondido::*
Takes the JSON response from the API and sets white listed properties for the instance
7 8 9 10 11 12 13 14 15 |
# File 'lib/mondido/base_model.rb', line 7 def update_from_response(attributes) attributes.each do |attribute, value| attribute_symbol = "@#{attribute}".to_sym setter_symbol = "#{attribute}=".to_sym instance_variable_set attribute_symbol, value if respond_to?(setter_symbol) end return self end |