Class: Omie::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/omie/base_resource.rb

Overview

This class contains all logic shared among available resources.

Direct Known Subclasses

Company, Product, TaxRecommendation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ BaseResource

Initialize the object based on a Hash of attributes their respective values



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/omie/base_resource.rb', line 12

def initialize(args = {})
  args.each do |key, value|
    key_s = key.to_sym
    has_internal_const = self.class.const_defined?('INTERNAL_MODELS')
    if has_internal_const && self.class::INTERNAL_MODELS.key?(key_s)
      klass = self.class::INTERNAL_MODELS[key_s]
      instance = klass.new(value)
      send("#{key}=", instance)
    elsif respond_to?(key_s)
      send("#{key}=", value)
    end
  end
end

Class Method Details

.request(uri, call, params) ⇒ Object



33
34
35
# File 'lib/omie/base_resource.rb', line 33

def self.request(uri, call, params)
  Omie::Connection.request(uri, call, params)
end

.request_and_initialize(uri, call, params) ⇒ Object



37
38
39
40
41
# File 'lib/omie/base_resource.rb', line 37

def self.request_and_initialize(uri, call, params)
  response = request(uri, call, params)

  new(response)
end

Instance Method Details

#update_attributes(args = {}) ⇒ Object

Update the object with the informed attributes passed as a Hash



27
28
29
30
31
# File 'lib/omie/base_resource.rb', line 27

def update_attributes(args = {})
  args.each do |key, value|
    send("#{key}=", value) if respond_to?(key)
  end
end