Class: ApiClient::Base

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

delete, get, patch, post, put, remove_root

Methods included from InstanceMethods

#delete, #get, #patch, #post, #put, #remove_root

Constructor Details

#initialize(attributes = {}) ⇒ Base

Initialize an object based on a hash of attributes.

Parameters:

  • attributes (Hash) (defaults to: {})

    object attributes.


31
32
33
34
35
36
# 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

Instance Attribute Details

#errorsHash

Returns the errors object.

Returns:

  • (Hash)

    the errors object.


25
26
27
# File 'lib/api-client/base.rb', line 25

def errors
  @errors
end

#idInteger

Returns the id of the object.

Returns:

  • (Integer)

    the id of the object.


19
20
21
# File 'lib/api-client/base.rb', line 19

def id
  @id
end

#responseHash

Returns the request response.

Returns:

  • (Hash)

    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.

Parameters:

  • associations (Hash) (defaults to: {})

    classes.


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.

Parameters:

  • vars (Array)

    instance variables.


120
121
122
123
124
# File 'lib/api-client/base.rb', line 120

def self.attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat(vars)
  super
end

.attributesArray

Return an array with all instance variables setted through attr_accessor.

Returns:

  • (Array)

    instance variables.


129
130
131
# File 'lib/api-client/base.rb', line 129

def self.attributes
  @attributes
end

.collectionCollection 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.

Returns:


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

.pathFalse

Return the api name to be used by this model.

Returns:

  • (False)

    return the default api name.


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.

Returns:

  • (False)

    return the default api name.


55
56
57
# File 'lib/api-client/base.rb', line 55

def self.path=(path)
  @path = path.to_sym
end

.resource_pathString

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

Returns:

  • (String)

    the resource path on the api for this object.


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.

Parameters:

  • resource_path (String)

    path string.


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_nodeString

Return the Root node name for this Class.

Returns:

  • (String)

    a string with 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.

Parameters:

  • root_node (String)

    root node 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

#attributesHash

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

Returns:

  • (Hash)

    instance variables and its 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

Parameters:

  • attr (defaults to: {})

    New attributes


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

#pathFalse

Return the api name to be used by this model.

Returns:

  • (False)

    return the default api name.


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.

Returns:

  • (False)

    always return false.


41
42
43
# File 'lib/api-client/base.rb', line 41

def persisted?
  false
end

#to_hashHash

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

Returns:

  • (Hash)

    instance variables and its values.


152
153
154
# File 'lib/api-client/base.rb', line 152

def to_hash
  { self.class.root_node.to_sym => attributes }
end