class ApiClient::Base

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.

Attributes

errors[R]

@return [Hash] the errors object.

id[RW]

@return [Integer] the id of the object.

response[RW]

@return [Hash] the request response.

Public Class Methods

all()
Alias for: collection
association=(associations = {})
Alias for: associations=
associations=(associations = {}) click to toggle source

Set methods to initialize associated objects.

@param [Hash] associations classes.

# 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
Also aliased as: association=
attr_accessor(*vars) click to toggle source

Overwrite attr_acessor method to save instance_variable names.

@param [Array] vars instance variables.

Calls superclass method
# File lib/api-client/base.rb, line 120
def self.attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat(vars)
  super
end
attributes() click to toggle source

Return an array with all instance variables setted through attr_accessor.

@return [Array] instance variables.

# File lib/api-client/base.rb, line 129
def self.attributes
  @attributes
end
collection() click to toggle source

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.

@return [Collection] a collection of objects.

# 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
Also aliased as: all
new(attributes = {}) click to toggle source

Initialize an object based on a hash of attributes.

@param [Hash] attributes object attributes. @return [Base] the object initialized.

# File lib/api-client/base.rb, line 31
def initialize(attributes = {})
  @errors = ApiClient::Errors.new(self)
  remove_root(attributes).each do |name, value|
    send("#{name.to_s}=", value)
  end
end
path() click to toggle source

Return the api name to be used by this model.

@return [False] return the default api name.

# File lib/api-client/base.rb, line 48
def self.path
  @path || :default
end
path=(path) click to toggle source

Return the api name to be used by this model.

@return [False] return the default api name.

# File lib/api-client/base.rb, line 55
def self.path=(path)
  @path = path.to_sym
end
resource_path() click to toggle source

Return the resource path of the object on the api url.

@return [String] the resource path on the api for this object.

# 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) click to toggle source

Set the resource path of the object on the api.

@param [String] resource_path path string.

# 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() click to toggle source

Return the Root node name for this Class.

@return [String] a string with the root node name for this class.

# 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) click to toggle source

Set a custom root node name instead of the Class name.

@param [String] root_node root node name.

# File lib/api-client/base.rb, line 92
def self.root_node=(root_node)
  @root_node = root_node
end

Public Instance Methods

attributes() click to toggle source

Return a hash with all instance variables setted through attr_accessor and its currently values.

@return [Hash] instance variables and its values.

# 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 = {}) click to toggle source

Update instance values based on a hash

@param attr New attributes

# File lib/api-client/base.rb, line 143
def attributes=(attr = {})
  remove_root(attr).each do |key, value|
    send("#{key}=", value)
  end
end
errors=(errs = {}) click to toggle source

Set the hash of errors, making keys symbolic.

@param [Hash] errs errors of the object.

# File lib/api-client/base.rb, line 173
def errors=(errs = {})
  errors.add_errors(Hash[errs.map{|(key,value)| [key.to_sym,value]}])
end
path() click to toggle source

Return the api name to be used by this model.

@return [False] return the default api name.

# File lib/api-client/base.rb, line 62
def path
  self.class.path
end
persisted?() click to toggle source

Return if a object is persisted on the database or not.

@return [False] always return false.

# File lib/api-client/base.rb, line 41
def persisted?
  false
end
to_hash() click to toggle source

Return a hash with a root node and all instance variables setted through attr_accessor and its currently values.

@return [Hash] instance variables and its values.

# File lib/api-client/base.rb, line 152
def to_hash
  { self.class.root_node.to_sym => attributes }
end