Class: CoinbaseCommerceClient::APIResources::Base::APIObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/coinbase_commerce_client/api_resources/base/api_object.rb

Direct Known Subclasses

APIResource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, client = nil) ⇒ APIObject

Returns a new instance of APIObject.



7
8
9
10
11
12
13
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 7

def initialize(id = nil, client = nil)
  @data = {}
  @data[:id] = id if id
  @client = client
  @unsaved_values = Set.new
  @transient_values = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 64

def method_missing(name, *args)
  if name.to_s.end_with?("=")

    attr = name.to_s[0...-1].to_sym
    val = args.first
    add_accessors([attr], attr => val)

    begin
      mth = method(name)
    rescue NameError
      raise NoMethodError, "Cannot set #{attr} on this object."
    end

    return mth.call(args[0])

  elsif @data.key?(name)
    return @data[name]
  end

  begin
    super
  rescue NoMethodError => e
    raise unless @transient_values.include?(name)
    raise NoMethodError, e.message + " Available attributes: #{@data.keys.join(', ')}"
  end
end

Class Method Details

.create_from(values, client = nil) ⇒ Object

Object initialize/update section



123
124
125
126
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 123

def self.create_from(values, client = nil)
  values = Util.symbolize_names(values)
  new(values[:id], client).send(:initialize_from, values)
end

Instance Method Details

#[](k) ⇒ Object

Base object options section



16
17
18
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 16

def [](k)
  @data[k.to_sym]
end

#[]=(k, v) ⇒ Object



20
21
22
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 20

def []=(k, v)
  send(:"#{k}=", v)
end

#each(&blk) ⇒ Object



32
33
34
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 32

def each(&blk)
  @data.each(&blk)
end

#initialize_from(values, partial = false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 128

def initialize_from(values, partial = false)
  removed = partial ? Set.new : Set.new(@data.keys - values.keys)
  added = Set.new(values.keys - @data.keys)

  remove_accessors(removed)
  add_accessors(added, values)

  removed.each do |k|
    @data.delete(k)
    @transient_values.add(k)
    @unsaved_values.delete(k)
  end

  update_attributes(values)
  values.each_key do |k|
    @transient_values.delete(k)
    @unsaved_values.delete(k)
  end

  self
end

#inspectObject



55
56
57
58
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 55

def inspect
  item_id = respond_to?(:id) && !id.nil? ? "id=#{id}" : "No ID"
  "#{self.class}: #{item_id}> Serialized: " + JSON.pretty_generate(@data)
end

#keysObject



24
25
26
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 24

def keys
  @data.keys
end

#respond_to_missing?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 60

def respond_to_missing?(symbol, include_private = false)
  @data && @data.key?(symbol) || super
end

#serialize_params(options = {}) ⇒ Object

Object serialize section



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 92

def serialize_params(options = {})
  update_hash = {}

  @data.each do |k, v|
    if options[:push] || @unsaved_values.include?(k) || v.is_a?(APIObject)
      push = options[:push] || @unsaved_values.include?(k)
      update_hash[k.to_sym] = serialize_params_value(@data[k], push)
    end
  end

  update_hash.reject! {|_, v| v.nil? || v.empty?}
  update_hash
end

#serialize_params_value(value, push) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 106

def serialize_params_value(value, push)
  if value.nil?
    ""
  elsif value.is_a?(Array)
    value.map {|v| serialize_params_value(v, push)}

  elsif value.is_a?(Hash)
    Util.convert_to_api_object(value, @opts).serialize_params

  elsif value.is_a?(APIObject)
    value.serialize_params(push: push)
  else
    value
  end
end

#to_hashObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 40

def to_hash
  @data.each_with_object({}) do |(key, value), output|
    case value
    when Array
      output[key] = value.map {|v| v.respond_to?(:to_hash) ? v.to_hash : v}
    else
      output[key] = value.respond_to?(:to_hash) ? value.to_hash : value
    end
  end
end

#to_json(*_a) ⇒ Object



51
52
53
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 51

def to_json(*_a)
  JSON.generate(@data)
end

#to_s(*_args) ⇒ Object



36
37
38
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 36

def to_s(*_args)
  JSON.pretty_generate(to_hash)
end

#update_attributes(values) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 150

def update_attributes(values)
  values.each do |k, v|
    add_accessors([k], values) unless metaclass.method_defined?(k.to_sym)
    @data[k] = Util.convert_to_api_object(v, @client)
    @unsaved_values.add(k)
  end
end

#valuesObject



28
29
30
# File 'lib/coinbase_commerce_client/api_resources/base/api_object.rb', line 28

def values
  @data.values
end