Class: CoinbaseCommerce::APIResources::Base::APIObject
- Inherits:
-
Object
- Object
- CoinbaseCommerce::APIResources::Base::APIObject
show all
- Includes:
- Enumerable
- Defined in:
- lib/coinbase_commerce/api_resources/base/api_object.rb
Overview
Base APIObject class Used to work and display with all the data that Coinbase Commerce API returns
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(id = nil, client = nil) ⇒ APIObject
Returns a new instance of APIObject.
10
11
12
13
14
15
16
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 10
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 67
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
126
127
128
129
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 126
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
19
20
21
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 19
def [](k)
@data[k.to_sym]
end
|
#[]=(k, v) ⇒ Object
23
24
25
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 23
def []=(k, v)
send(:"#{k}=", v)
end
|
#each(&blk) ⇒ Object
35
36
37
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 35
def each(&blk)
@data.each(&blk)
end
|
#initialize_from(values, partial = false) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 131
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
|
#inspect ⇒ Object
58
59
60
61
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 58
def inspect
item_id = respond_to?(:id) && !id.nil? ? "id=#{id}" : "No ID"
"#{self.class}: #{item_id}> Serialized: " + JSON.pretty_generate(@data)
end
|
#keys ⇒ Object
27
28
29
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 27
def keys
@data.keys
end
|
#respond_to_missing?(symbol, include_private = false) ⇒ Boolean
63
64
65
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 63
def respond_to_missing?(symbol, include_private = false)
@data && @data.key?(symbol) || super
end
|
#serialize_params(options = {}) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 95
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 109
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_hash ⇒ Object
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 43
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
54
55
56
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 54
def to_json(*_a)
JSON.generate(@data)
end
|
#to_s(*_args) ⇒ Object
39
40
41
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 39
def to_s(*_args)
JSON.pretty_generate(to_hash)
end
|
#update_attributes(values) ⇒ Object
153
154
155
156
157
158
159
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 153
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
|
#values ⇒ Object
31
32
33
|
# File 'lib/coinbase_commerce/api_resources/base/api_object.rb', line 31
def values
@data.values
end
|