Class: Coinbasepro::Api::APIObject

Inherits:
Hash
  • Object
show all
Defined in:
lib/coinbasepro/api/api_object.rb

Overview

Response item abstract model

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ APIObject

Returns a new instance of APIObject.



5
6
7
8
# File 'lib/coinbasepro/api/api_object.rb', line 5

def initialize(data)
  super
  update(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



30
31
32
# File 'lib/coinbasepro/api/api_object.rb', line 30

def method_missing(method, *args, &blk)
  format(self[method.to_s]) || super
end

Instance Method Details

#format(var) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/coinbasepro/api/api_object.rb', line 15

def format(var)
  return if var.nil?
  # Looks like a number or currency
  if var =~ /^.{0,1}\s*[0-9,]*\.{0,1}[0-9]*$/
    BigDecimal(var.gsub(/[^0-9\.\-]/, ''))
  else
    var
  end
end

#respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/coinbasepro/api/api_object.rb', line 34

def respond_to_missing?(method, include_all = false)
  self.key?(method.to_s) || super
end

#sizeObject

This is common enough that we should override the hash method



26
27
28
# File 'lib/coinbasepro/api/api_object.rb', line 26

def size
  format(self['size']) || super
end

#update(data) ⇒ Object



10
11
12
13
# File 'lib/coinbasepro/api/api_object.rb', line 10

def update(data)
  return if data.nil?
  data.each { |key, val| self[key] = val } if data.is_a?(Hash)
end