Class: Coinbase::Wallet::APIObject

Inherits:
Hash
  • Object
show all
Defined in:
lib/coinbase/wallet/models/api_object.rb

Overview

Response item abstract model

Direct Known Subclasses

Account, Address, Checkout, Order, Transaction, Transfer, User

Instance Method Summary collapse

Constructor Details

#initialize(client, data) ⇒ APIObject

Returns a new instance of APIObject.



5
6
7
8
9
# File 'lib/coinbase/wallet/models/api_object.rb', line 5

def initialize(client, data)
  super()
  update(data)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



37
38
39
# File 'lib/coinbase/wallet/models/api_object.rb', line 37

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

Instance Method Details

#format(key, val) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/coinbase/wallet/models/api_object.rb', line 23

def format(key, val)
  return if val.nil?
  # Looks like a number or currency
  if val.class == Hash
    APIObject.new(@client, val)
  elsif key =~ /_at$/ && (Time.iso8601(val) rescue nil)
    Time.parse(val)
  elsif key == "amount" && val =~ /^.{0,1}\s*[0-9,]*\.{0,1}[0-9]*$/
    BigDecimal(val.gsub(/[^0-9\.-]/, ''))
  else
    val
  end
end

#refresh!(params = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/coinbase/wallet/models/api_object.rb', line 11

def refresh!(params = {})
  @client.get(self['resource_path'], params) do |resp|
    update(resp.data)
    yield(resp.data, resp) if block_given?
  end
end

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/coinbase/wallet/models/api_object.rb', line 41

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

#update(data) ⇒ Object



18
19
20
21
# File 'lib/coinbase/wallet/models/api_object.rb', line 18

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