Class: ApiClient::Base
- Inherits:
-
Object
- Object
- ApiClient::Base
- Extended by:
- ActiveModel::Naming, ClassMethods
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, InstanceMethods
- Defined in:
- lib/api-client/base.rb
Overview
ApiClient::Base provides a way to make easy api requests as well as making possible to use it inside rails. A possible implementation:
class Car < ApiClient::Base
attr_accessor :color, :name, :year
end
This class will handle Rails form as well as it works with respond_with.
Instance Attribute Summary collapse
-
#errors ⇒ Hash
The errors object.
-
#id ⇒ Integer
The id of the object.
-
#response ⇒ Hash
The request response.
Class Method Summary collapse
-
.associations=(associations = {}) ⇒ Object
(also: association=)
Set methods to initialize associated objects.
-
.attr_accessor(*vars) ⇒ Object
Overwrite #attr_acessor method to save instance_variable names.
-
.attributes ⇒ Array
Return an array with all instance variables setted through attr_accessor.
-
.collection ⇒ Collection
(also: all)
Initialize a collection of objects.
-
.path ⇒ False
Return the api name to be used by this model.
-
.path=(path) ⇒ False
Return the api name to be used by this model.
-
.resource_path ⇒ String
Return the resource path of the object on the api url.
-
.resource_path=(resource_path) ⇒ Object
Set the resource path of the object on the api.
-
.root_node ⇒ String
Return the Root node name for this Class.
-
.root_node=(root_node) ⇒ Object
Set a custom root node name instead of the Class name.
Instance Method Summary collapse
-
#attributes ⇒ Hash
Return a hash with all instance variables setted through attr_accessor and its currently values.
-
#attributes=(attr = {}) ⇒ Object
Update instance values based on a hash.
-
#initialize(attributes = {}) ⇒ Base
constructor
Initialize an object based on a hash of attributes.
-
#path ⇒ False
Return the api name to be used by this model.
-
#persisted? ⇒ False
Return if a object is persisted on the database or not.
-
#to_hash ⇒ Hash
Return a hash with a root node and all instance variables setted through attr_accessor and its currently values.
Methods included from ClassMethods
delete, get, patch, post, put, remove_root
Methods included from InstanceMethods
#delete, #get, #patch, #post, #put, #remove_root
Constructor Details
Instance Attribute Details
#errors ⇒ Hash
Returns the errors object.
25 26 27 |
# File 'lib/api-client/base.rb', line 25 def errors @errors end |
#id ⇒ Integer
Returns the id of the object.
19 20 21 |
# File 'lib/api-client/base.rb', line 19 def id @id end |
#response ⇒ Hash
Returns the request response.
22 23 24 |
# File 'lib/api-client/base.rb', line 22 def response @response end |
Class Method Details
.associations=(associations = {}) ⇒ Object Also known as: association=
Set methods to initialize associated objects.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/api-client/base.rb', line 99 def self.associations=(associations = {}) associations.each do |association, class_name| class_eval <<-EVAL def #{association.to_s}=(attributes = {}) return @#{association.to_s} = attributes.map { |attr| #{class_name.constantize}.new(attr) } if attributes.instance_of?(Array) @#{association.to_s} = #{class_name.constantize}.new(attributes) end def #{association.to_s} @#{association.to_s} end EVAL end end |
.attr_accessor(*vars) ⇒ Object
Overwrite #attr_acessor method to save instance_variable names.
120 121 122 123 124 |
# File 'lib/api-client/base.rb', line 120 def self.attr_accessor(*vars) @attributes ||= [] @attributes.concat(vars) super end |
.attributes ⇒ Array
Return an array with all instance variables setted through attr_accessor.
129 130 131 |
# File 'lib/api-client/base.rb', line 129 def self.attributes @attributes end |
.collection ⇒ Collection Also known as: all
Initialize a collection of objects. The collection will be an ApiClient::Collection object. The objects in the collection will be all instances of this (ApiClient::Base) class.
160 161 162 163 164 |
# File 'lib/api-client/base.rb', line 160 def self.collection url = "#{ApiClient.config.path[path]}#{resource_path}" attributes = ApiClient::Parser.response(ApiClient::Dispatcher.get(url), url) ApiClient::Collection.new(attributes, self) end |
.path ⇒ False
Return the api name to be used by this model.
48 49 50 |
# File 'lib/api-client/base.rb', line 48 def self.path @path || :default end |
.path=(path) ⇒ False
Return the api name to be used by this model.
55 56 57 |
# File 'lib/api-client/base.rb', line 55 def self.path=(path) @path = path.to_sym end |
.resource_path ⇒ String
Return the resource path of the object on the api url.
69 70 71 72 |
# File 'lib/api-client/base.rb', line 69 def self.resource_path return self.to_s.gsub('::', '/').downcase.pluralize unless @resource_path @resource_path end |
.resource_path=(resource_path) ⇒ Object
Set the resource path of the object on the api.
77 78 79 80 |
# File 'lib/api-client/base.rb', line 77 def self.resource_path=(resource_path) resource_path = resource_path[1, resource_path.size - 1] if resource_path[0, 1] == '/' @resource_path = resource_path end |
.root_node ⇒ String
Return the Root node name for this Class.
85 86 87 |
# File 'lib/api-client/base.rb', line 85 def self.root_node @root_node.blank? ? self.to_s.split('::').last.underscore : @root_node end |
.root_node=(root_node) ⇒ Object
Set a custom root node name instead of the Class name.
92 93 94 |
# File 'lib/api-client/base.rb', line 92 def self.root_node=(root_node) @root_node = root_node end |
Instance Method Details
#attributes ⇒ Hash
Return a hash with all instance variables setted through attr_accessor and its currently values.
136 137 138 |
# File 'lib/api-client/base.rb', line 136 def attributes self.class.instance_variable_get('@attributes').inject({}) { |hash, attribute| hash.merge(attribute.to_sym => self.send("#{attribute}")) } end |
#attributes=(attr = {}) ⇒ Object
Update instance values based on a hash
143 144 145 146 147 |
# File 'lib/api-client/base.rb', line 143 def attributes=(attr = {}) remove_root(attr).each do |key, value| send("#{key}=", value) end end |
#path ⇒ False
Return the api name to be used by this model.
62 63 64 |
# File 'lib/api-client/base.rb', line 62 def path self.class.path end |
#persisted? ⇒ False
Return if a object is persisted on the database or not.
41 42 43 |
# File 'lib/api-client/base.rb', line 41 def persisted? false end |
#to_hash ⇒ Hash
Return a hash with a root node and all instance variables setted through attr_accessor and its currently values.
152 153 154 |
# File 'lib/api-client/base.rb', line 152 def to_hash { self.class.root_node.to_sym => attributes } end |