telnyx 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitattributes +4 -0
- data/.github/ISSUE_TEMPLATE.md +5 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +32 -0
- data/.rubocop_todo.yml +50 -0
- data/.travis.yml +42 -0
- data/CHANGELOG.md +2 -0
- data/CONTRIBUTORS +0 -0
- data/Gemfile +40 -0
- data/Guardfile +8 -0
- data/LICENSE +22 -0
- data/README.md +173 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/bin/telnyx-console +16 -0
- data/lib/telnyx.rb +151 -0
- data/lib/telnyx/api_operations/create.rb +12 -0
- data/lib/telnyx/api_operations/delete.rb +13 -0
- data/lib/telnyx/api_operations/list.rb +29 -0
- data/lib/telnyx/api_operations/nested_resource.rb +63 -0
- data/lib/telnyx/api_operations/request.rb +57 -0
- data/lib/telnyx/api_operations/save.rb +103 -0
- data/lib/telnyx/api_resource.rb +69 -0
- data/lib/telnyx/available_phone_number.rb +9 -0
- data/lib/telnyx/errors.rb +166 -0
- data/lib/telnyx/event.rb +9 -0
- data/lib/telnyx/list_object.rb +155 -0
- data/lib/telnyx/message.rb +9 -0
- data/lib/telnyx/messaging_phone_number.rb +10 -0
- data/lib/telnyx/messaging_profile.rb +32 -0
- data/lib/telnyx/messaging_sender_id.rb +12 -0
- data/lib/telnyx/messaging_short_code.rb +10 -0
- data/lib/telnyx/number_order.rb +11 -0
- data/lib/telnyx/number_reservation.rb +11 -0
- data/lib/telnyx/public_key.rb +7 -0
- data/lib/telnyx/singleton_api_resource.rb +24 -0
- data/lib/telnyx/telnyx_client.rb +545 -0
- data/lib/telnyx/telnyx_object.rb +521 -0
- data/lib/telnyx/telnyx_response.rb +50 -0
- data/lib/telnyx/util.rb +328 -0
- data/lib/telnyx/version.rb +5 -0
- data/lib/telnyx/webhook.rb +66 -0
- data/telnyx.gemspec +25 -0
- data/test/api_stub_helpers.rb +1 -0
- data/test/openapi/README.md +9 -0
- data/test/telnyx/api_operations_test.rb +85 -0
- data/test/telnyx/api_resource_test.rb +293 -0
- data/test/telnyx/available_phone_number_test.rb +14 -0
- data/test/telnyx/errors_test.rb +23 -0
- data/test/telnyx/list_object_test.rb +244 -0
- data/test/telnyx/message_test.rb +19 -0
- data/test/telnyx/messaging_phone_number_test.rb +33 -0
- data/test/telnyx/messaging_profile_test.rb +70 -0
- data/test/telnyx/messaging_sender_id_test.rb +46 -0
- data/test/telnyx/messaging_short_code_test.rb +33 -0
- data/test/telnyx/number_order_test.rb +39 -0
- data/test/telnyx/number_reservation_test.rb +12 -0
- data/test/telnyx/public_key_test.rb +13 -0
- data/test/telnyx/telnyx_client_test.rb +631 -0
- data/test/telnyx/telnyx_object_test.rb +497 -0
- data/test/telnyx/telnyx_response_test.rb +49 -0
- data/test/telnyx/util_test.rb +380 -0
- data/test/telnyx/webhook_test.rb +108 -0
- data/test/telnyx_mock.rb +78 -0
- data/test/telnyx_test.rb +40 -0
- data/test/test_data.rb +149 -0
- data/test/test_helper.rb +73 -0
- metadata +162 -0
@@ -0,0 +1,521 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Telnyx
|
4
|
+
class TelnyxObject
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
@@permanent_attributes = Set.new([:id])
|
8
|
+
|
9
|
+
# The default :id method is deprecated and isn't useful to us
|
10
|
+
undef :id if method_defined?(:id)
|
11
|
+
|
12
|
+
# Sets the given parameter name to one which is known to be an additive
|
13
|
+
# object.
|
14
|
+
#
|
15
|
+
# Additive objects are subobjects in the API that don't have the same
|
16
|
+
# semantics as most subobjects, which are fully replaced when they're set.
|
17
|
+
# This is best illustrated by example. The `source` parameter sent when
|
18
|
+
# updating a subscription is *not* additive; if we set it:
|
19
|
+
#
|
20
|
+
# source[object]=card&source[number]=123
|
21
|
+
#
|
22
|
+
# We expect the old `source` object to have been overwritten completely. If
|
23
|
+
# the previous source had an `address_state` key associated with it and we
|
24
|
+
# didn't send one this time, that value of `address_state` is gone.
|
25
|
+
#
|
26
|
+
# By contrast, additive objects are those that will have new data added to
|
27
|
+
# them while keeping any existing data in place. The only known case of its
|
28
|
+
# use is for `metadata`, but it could in theory be more general. As an
|
29
|
+
# example, say we have a `metadata` object that looks like this on the
|
30
|
+
# server side:
|
31
|
+
#
|
32
|
+
# metadata = { old: "old_value" }
|
33
|
+
#
|
34
|
+
# If we update the object with `metadata[new]=new_value`, the server side
|
35
|
+
# object now has *both* fields:
|
36
|
+
#
|
37
|
+
# metadata = { old: "old_value", new: "new_value" }
|
38
|
+
#
|
39
|
+
# This is okay in itself because usually users will want to treat it as
|
40
|
+
# additive:
|
41
|
+
#
|
42
|
+
# obj.metadata[:new] = "new_value"
|
43
|
+
# obj.save
|
44
|
+
#
|
45
|
+
# However, in other cases, they may want to replace the entire existing
|
46
|
+
# contents:
|
47
|
+
#
|
48
|
+
# obj.metadata = { new: "new_value" }
|
49
|
+
# obj.save
|
50
|
+
#
|
51
|
+
# This is where things get a little bit tricky because in order to clear
|
52
|
+
# any old keys that may have existed, we actually have to send an explicit
|
53
|
+
# empty string to the server. So the operation above would have to send
|
54
|
+
# this form to get the intended behavior:
|
55
|
+
#
|
56
|
+
# metadata[old]=&metadata[new]=new_value
|
57
|
+
#
|
58
|
+
# This method allows us to track which parameters are considered additive,
|
59
|
+
# and lets us behave correctly where appropriate when serializing
|
60
|
+
# parameters to be sent.
|
61
|
+
def self.additive_object_param(name)
|
62
|
+
@additive_params ||= Set.new
|
63
|
+
@additive_params << name
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns whether the given name is an additive object parameter. See
|
67
|
+
# `.additive_object_param` for details.
|
68
|
+
def self.additive_object_param?(name)
|
69
|
+
@additive_params ||= Set.new
|
70
|
+
@additive_params.include?(name)
|
71
|
+
end
|
72
|
+
|
73
|
+
def initialize(id = nil, opts = {})
|
74
|
+
id, @retrieve_params = Util.normalize_id(id)
|
75
|
+
@opts = Util.normalize_opts(opts)
|
76
|
+
@original_values = {}
|
77
|
+
@values = {}
|
78
|
+
# This really belongs in APIResource, but not putting it there allows us
|
79
|
+
# to have a unified inspect method
|
80
|
+
@unsaved_values = Set.new
|
81
|
+
@transient_values = Set.new
|
82
|
+
@values[:id] = id if id
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.construct_from(values, opts = {})
|
86
|
+
values = Telnyx::Util.symbolize_names(values)
|
87
|
+
|
88
|
+
# work around protected #initialize_from for now
|
89
|
+
new(values[:id]).send(:initialize_from, values, opts)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Determines the equality of two Telnyx objects. Telnyx objects are
|
93
|
+
# considered to be equal if they have the same set of values and each one
|
94
|
+
# of those values is the same.
|
95
|
+
def ==(other)
|
96
|
+
other.is_a?(TelnyxObject) && @values == other.instance_variable_get(:@values)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Hash equality. As with `#==`, we consider two equivalent Telnyx objects equal.
|
100
|
+
def eql?(other)
|
101
|
+
# Defer to the implementation on `#==`.
|
102
|
+
self == other
|
103
|
+
end
|
104
|
+
|
105
|
+
# As with equality in `#==` and `#eql?`, we hash two Telnyx objects to the
|
106
|
+
# same value if they're equivalent objects.
|
107
|
+
def hash
|
108
|
+
@values.hash
|
109
|
+
end
|
110
|
+
|
111
|
+
# Indicates whether or not the resource has been deleted on the server.
|
112
|
+
# Note that some, but not all, resources can indicate whether they have
|
113
|
+
# been deleted.
|
114
|
+
def deleted?
|
115
|
+
@values.fetch(:deleted, false)
|
116
|
+
end
|
117
|
+
|
118
|
+
def to_s(*_args)
|
119
|
+
JSON.pretty_generate(to_hash)
|
120
|
+
end
|
121
|
+
|
122
|
+
def inspect
|
123
|
+
id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : ""
|
124
|
+
"#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Mass assigns attributes on the model.
|
128
|
+
#
|
129
|
+
# This is a version of +update_attributes+ that takes some extra options
|
130
|
+
# for internal use.
|
131
|
+
#
|
132
|
+
# ==== Attributes
|
133
|
+
#
|
134
|
+
# * +values+ - Hash of values to use to update the current attributes of
|
135
|
+
# the object.
|
136
|
+
# * +opts+ - Options for +TelnyxObject+ like an API key that will be reused
|
137
|
+
# on subsequent API calls.
|
138
|
+
#
|
139
|
+
# ==== Options
|
140
|
+
#
|
141
|
+
# * +:dirty+ - Whether values should be initiated as "dirty" (unsaved) and
|
142
|
+
# which applies only to new TelnyxObjects being initiated under this
|
143
|
+
# TelnyxObject. Defaults to true.
|
144
|
+
def update_attributes(values, opts = {}, dirty: true)
|
145
|
+
values.each do |k, v|
|
146
|
+
add_accessors([k], values) unless metaclass.method_defined?(k.to_sym)
|
147
|
+
@values[k] = Util.convert_to_telnyx_object(v, opts)
|
148
|
+
dirty_value!(@values[k]) if dirty
|
149
|
+
@unsaved_values.add(k)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def [](k)
|
154
|
+
@values[k.to_sym]
|
155
|
+
end
|
156
|
+
|
157
|
+
def []=(k, v)
|
158
|
+
send(:"#{k}=", v)
|
159
|
+
end
|
160
|
+
|
161
|
+
def keys
|
162
|
+
@values.keys
|
163
|
+
end
|
164
|
+
|
165
|
+
def values
|
166
|
+
@values.values
|
167
|
+
end
|
168
|
+
|
169
|
+
def to_json(*_a)
|
170
|
+
JSON.generate(@values)
|
171
|
+
end
|
172
|
+
|
173
|
+
def as_json(*a)
|
174
|
+
@values.as_json(*a)
|
175
|
+
end
|
176
|
+
|
177
|
+
def to_hash
|
178
|
+
maybe_to_hash = lambda do |value|
|
179
|
+
value && value.respond_to?(:to_hash) ? value.to_hash : value
|
180
|
+
end
|
181
|
+
|
182
|
+
@values.each_with_object({}) do |(key, value), acc|
|
183
|
+
acc[key] = case value
|
184
|
+
when Array
|
185
|
+
value.map(&maybe_to_hash)
|
186
|
+
else
|
187
|
+
maybe_to_hash.call(value)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def each(&blk)
|
193
|
+
@values.each(&blk)
|
194
|
+
end
|
195
|
+
|
196
|
+
# Sets all keys within the TelnyxObject as unsaved so that they will be
|
197
|
+
# included with an update when #serialize_params is called. This method is
|
198
|
+
# also recursive, so any TelnyxObjects contained as values or which are
|
199
|
+
# values in a tenant array are also marked as dirty.
|
200
|
+
def dirty!
|
201
|
+
@unsaved_values = Set.new(@values.keys)
|
202
|
+
@values.each_value do |v|
|
203
|
+
dirty_value!(v)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# Implements custom encoding for Ruby's Marshal. The data produced by this
|
208
|
+
# method should be comprehendable by #marshal_load.
|
209
|
+
#
|
210
|
+
# This allows us to remove certain features that cannot or should not be
|
211
|
+
# serialized.
|
212
|
+
def marshal_dump
|
213
|
+
# The TelnyxClient instance in @opts is not serializable and is not
|
214
|
+
# really a property of the TelnyxObject, so we exclude it when
|
215
|
+
# dumping
|
216
|
+
opts = @opts.clone
|
217
|
+
opts.delete(:client)
|
218
|
+
[@values, opts]
|
219
|
+
end
|
220
|
+
|
221
|
+
# Implements custom decoding for Ruby's Marshal. Consumes data that's
|
222
|
+
# produced by #marshal_dump.
|
223
|
+
def marshal_load(data)
|
224
|
+
values, opts = data
|
225
|
+
initialize(values[:id])
|
226
|
+
initialize_from(values, opts)
|
227
|
+
end
|
228
|
+
|
229
|
+
def serialize_params(options = {})
|
230
|
+
update_hash = {}
|
231
|
+
|
232
|
+
@values.each do |k, v|
|
233
|
+
# There are a few reasons that we may want to add in a parameter for
|
234
|
+
# update:
|
235
|
+
#
|
236
|
+
# 1. The `force` option has been set.
|
237
|
+
# 2. We know that it was modified.
|
238
|
+
# 3. Its value is a TelnyxObject. A TelnyxObject may contain modified
|
239
|
+
# values within in that its parent TelnyxObject doesn't know about.
|
240
|
+
#
|
241
|
+
unsaved = @unsaved_values.include?(k)
|
242
|
+
if options[:force] || unsaved || v.is_a?(TelnyxObject)
|
243
|
+
update_hash[k.to_sym] =
|
244
|
+
serialize_params_value(@values[k], @original_values[k], unsaved, options[:force], key: k)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# a `nil` that makes it out of `#serialize_params_value` signals an empty
|
249
|
+
# value that we shouldn't appear in the serialized form of the object
|
250
|
+
update_hash.reject! { |_, v| v.nil? }
|
251
|
+
|
252
|
+
update_hash
|
253
|
+
end
|
254
|
+
|
255
|
+
# A protected field is one that doesn't get an accessor assigned to it
|
256
|
+
# (i.e. `obj.public = ...`) and one which is not allowed to be updated via
|
257
|
+
# the class level `Model.update(id, { ... })`.
|
258
|
+
def self.protected_fields
|
259
|
+
[]
|
260
|
+
end
|
261
|
+
|
262
|
+
protected
|
263
|
+
|
264
|
+
def metaclass
|
265
|
+
class << self; self; end
|
266
|
+
end
|
267
|
+
|
268
|
+
def remove_accessors(keys)
|
269
|
+
# not available in the #instance_eval below
|
270
|
+
protected_fields = self.class.protected_fields
|
271
|
+
|
272
|
+
metaclass.instance_eval do
|
273
|
+
keys.each do |k|
|
274
|
+
next if protected_fields.include?(k)
|
275
|
+
next if @@permanent_attributes.include?(k)
|
276
|
+
|
277
|
+
# Remove methods for the accessor's reader and writer.
|
278
|
+
[k, :"#{k}=", :"#{k}?"].each do |method_name|
|
279
|
+
remove_method(method_name) if method_defined?(method_name)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def add_accessors(keys, values)
|
286
|
+
# not available in the #instance_eval below
|
287
|
+
protected_fields = self.class.protected_fields
|
288
|
+
|
289
|
+
metaclass.instance_eval do
|
290
|
+
keys.each do |k|
|
291
|
+
next if protected_fields.include?(k)
|
292
|
+
next if @@permanent_attributes.include?(k)
|
293
|
+
|
294
|
+
if k == :method
|
295
|
+
# Object#method is a built-in Ruby method that accepts a symbol
|
296
|
+
# and returns the corresponding Method object. Because the API may
|
297
|
+
# also use `method` as a field name, we check the arity of *args
|
298
|
+
# to decide whether to act as a getter or call the parent method.
|
299
|
+
define_method(k) { |*args| args.empty? ? @values[k] : super(*args) }
|
300
|
+
else
|
301
|
+
define_method(k) { @values[k] }
|
302
|
+
end
|
303
|
+
|
304
|
+
define_method(:"#{k}=") do |v|
|
305
|
+
if v == ""
|
306
|
+
raise ArgumentError, "You cannot set #{k} to an empty string. " \
|
307
|
+
"We interpret empty strings as nil in requests. " \
|
308
|
+
"You may set (object).#{k} = nil to delete the property."
|
309
|
+
end
|
310
|
+
@values[k] = Util.convert_to_telnyx_object(v, @opts)
|
311
|
+
dirty_value!(@values[k])
|
312
|
+
@unsaved_values.add(k)
|
313
|
+
end
|
314
|
+
|
315
|
+
if [FalseClass, TrueClass].include?(values[k].class)
|
316
|
+
define_method(:"#{k}?") { @values[k] }
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
def method_missing(name, *args)
|
323
|
+
# TODO: only allow setting in updateable classes.
|
324
|
+
if name.to_s.end_with?("=")
|
325
|
+
attr = name.to_s[0...-1].to_sym
|
326
|
+
|
327
|
+
# Pull out the assigned value. This is only used in the case of a
|
328
|
+
# boolean value to add a question mark accessor (i.e. `foo?`) for
|
329
|
+
# convenience.
|
330
|
+
val = args.first
|
331
|
+
|
332
|
+
# the second argument is only required when adding boolean accessors
|
333
|
+
add_accessors([attr], attr => val)
|
334
|
+
|
335
|
+
begin
|
336
|
+
mth = method(name)
|
337
|
+
rescue NameError
|
338
|
+
raise NoMethodError, "Cannot set #{attr} on this object. HINT: you can't set: #{@@permanent_attributes.to_a.join(', ')}"
|
339
|
+
end
|
340
|
+
return mth.call(args[0])
|
341
|
+
elsif @values.key?(name)
|
342
|
+
return @values[name]
|
343
|
+
end
|
344
|
+
|
345
|
+
begin
|
346
|
+
super
|
347
|
+
rescue NoMethodError => e
|
348
|
+
# If we notice the accessed name if our set of transient values we can
|
349
|
+
# give the user a slightly more helpful error message. If not, just
|
350
|
+
# raise right away.
|
351
|
+
raise unless @transient_values.include?(name)
|
352
|
+
|
353
|
+
raise NoMethodError, e.message + ". HINT: The '#{name}' attribute was set in the past, however. It was then wiped when refreshing the object with the result returned by Telnyx's API, probably as a result of a save(). The attributes currently available on this object are: #{@values.keys.join(', ')}"
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
def respond_to_missing?(symbol, include_private = false)
|
358
|
+
@values && @values.key?(symbol) || super
|
359
|
+
end
|
360
|
+
|
361
|
+
# Re-initializes the object based on a hash of values (usually one that's
|
362
|
+
# come back from an API call). Adds or removes value accessors as necessary
|
363
|
+
# and updates the state of internal data.
|
364
|
+
#
|
365
|
+
# Protected on purpose! Please do not expose.
|
366
|
+
#
|
367
|
+
# ==== Options
|
368
|
+
#
|
369
|
+
# * +:values:+ Hash used to update accessors and values.
|
370
|
+
# * +:opts:+ Options for TelnyxObject like an API key.
|
371
|
+
# * +:partial:+ Indicates that the re-initialization should not attempt to
|
372
|
+
# remove accessors.
|
373
|
+
def initialize_from(values, opts, partial = false)
|
374
|
+
@opts = Util.normalize_opts(opts)
|
375
|
+
|
376
|
+
# the `#send` is here so that we can keep this method private
|
377
|
+
@original_values = self.class.send(:deep_copy, values)
|
378
|
+
|
379
|
+
removed = partial ? Set.new : Set.new(@values.keys - values.keys)
|
380
|
+
added = Set.new(values.keys - @values.keys)
|
381
|
+
|
382
|
+
# Wipe old state before setting new.
|
383
|
+
|
384
|
+
remove_accessors(removed)
|
385
|
+
add_accessors(added, values)
|
386
|
+
|
387
|
+
removed.each do |k|
|
388
|
+
@values.delete(k)
|
389
|
+
@transient_values.add(k)
|
390
|
+
@unsaved_values.delete(k)
|
391
|
+
end
|
392
|
+
|
393
|
+
update_attributes(values, opts, dirty: false)
|
394
|
+
values.each_key do |k|
|
395
|
+
@transient_values.delete(k)
|
396
|
+
@unsaved_values.delete(k)
|
397
|
+
end
|
398
|
+
|
399
|
+
self
|
400
|
+
end
|
401
|
+
|
402
|
+
def serialize_params_value(value, original, unsaved, force, key: nil)
|
403
|
+
if value.nil?
|
404
|
+
""
|
405
|
+
|
406
|
+
# The logic here is that essentially any object embedded in another
|
407
|
+
# object that had a `type` is actually an API resource of a different
|
408
|
+
# type that's been included in the response. These other resources must
|
409
|
+
# be updated from their proper endpoints, and therefore they are not
|
410
|
+
# included when serializing even if they've been modified.
|
411
|
+
#
|
412
|
+
# There are _some_ known exceptions though.
|
413
|
+
#
|
414
|
+
# For example, if the value is unsaved (meaning the user has set it), and
|
415
|
+
# it looks like the API resource is persisted with an ID, then we include
|
416
|
+
# the object so that parameters are serialized with a reference to its
|
417
|
+
# ID.
|
418
|
+
#
|
419
|
+
# We throw an error if a property was set explicitly but we can't do
|
420
|
+
# anything with it because the integration is probably not working as the
|
421
|
+
# user intended it to.
|
422
|
+
elsif value.is_a?(APIResource) && !value.save_with_parent
|
423
|
+
if !unsaved
|
424
|
+
nil
|
425
|
+
elsif value.respond_to?(:id) && !value.id.nil?
|
426
|
+
value
|
427
|
+
else
|
428
|
+
raise ArgumentError, "Cannot save property `#{key}` containing " \
|
429
|
+
"an API resource. It doesn't appear to be persisted and is " \
|
430
|
+
"not marked as `save_with_parent`."
|
431
|
+
end
|
432
|
+
|
433
|
+
elsif value.is_a?(Array)
|
434
|
+
update = value.map { |v| serialize_params_value(v, nil, true, force) }
|
435
|
+
|
436
|
+
# This prevents an array that's unchanged from being resent.
|
437
|
+
update if update != serialize_params_value(original, nil, true, force)
|
438
|
+
|
439
|
+
# Handle a Hash for now, but in the long run we should be able to
|
440
|
+
# eliminate all places where hashes are stored as values internally by
|
441
|
+
# making sure any time one is set, we convert it to a TelnyxObject. This
|
442
|
+
# will simplify our model by making data within an object more
|
443
|
+
# consistent.
|
444
|
+
#
|
445
|
+
# For now, you can still run into a hash if someone appends one to an
|
446
|
+
# existing array being held by a TelnyxObject. This could happen for
|
447
|
+
# example by appending a new hash onto `additional_owners` for an
|
448
|
+
# account.
|
449
|
+
elsif value.is_a?(Hash)
|
450
|
+
Util.convert_to_telnyx_object(value, @opts).serialize_params
|
451
|
+
|
452
|
+
elsif value.is_a?(TelnyxObject)
|
453
|
+
update = value.serialize_params(force: force)
|
454
|
+
|
455
|
+
# If the entire object was replaced and this is an additive object,
|
456
|
+
# then we need blank each field of the old object that held a value
|
457
|
+
# because otherwise the update to the keys of the object will be
|
458
|
+
# additive instead of a full replacement. The new serialized values
|
459
|
+
# will override any of these empty values.
|
460
|
+
if original && unsaved && key && self.class.additive_object_param?(key)
|
461
|
+
update = empty_values(original).merge(update)
|
462
|
+
end
|
463
|
+
|
464
|
+
update
|
465
|
+
|
466
|
+
else
|
467
|
+
value
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
private
|
472
|
+
|
473
|
+
# Produces a deep copy of the given object including support for arrays,
|
474
|
+
# hashes, and TelnyxObjects.
|
475
|
+
def self.deep_copy(obj)
|
476
|
+
case obj
|
477
|
+
when Array
|
478
|
+
obj.map { |e| deep_copy(e) }
|
479
|
+
when Hash
|
480
|
+
obj.each_with_object({}) do |(k, v), copy|
|
481
|
+
copy[k] = deep_copy(v)
|
482
|
+
copy
|
483
|
+
end
|
484
|
+
when TelnyxObject
|
485
|
+
obj.class.construct_from(
|
486
|
+
deep_copy(obj.instance_variable_get(:@values)),
|
487
|
+
obj.instance_variable_get(:@opts).select do |k, _v|
|
488
|
+
Util::OPTS_COPYABLE.include?(k)
|
489
|
+
end
|
490
|
+
)
|
491
|
+
else
|
492
|
+
obj
|
493
|
+
end
|
494
|
+
end
|
495
|
+
private_class_method :deep_copy
|
496
|
+
|
497
|
+
def dirty_value!(value)
|
498
|
+
case value
|
499
|
+
when Array
|
500
|
+
value.map { |v| dirty_value!(v) }
|
501
|
+
when TelnyxObject
|
502
|
+
value.dirty!
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
# Returns a hash of empty values for all the values that are in the given
|
507
|
+
# TelnyxObject.
|
508
|
+
def empty_values(obj)
|
509
|
+
values = case obj
|
510
|
+
when Hash then obj
|
511
|
+
when TelnyxObject then obj.instance_variable_get(:@values)
|
512
|
+
else
|
513
|
+
raise ArgumentError, "#empty_values got unexpected object type: #{obj.class.name}"
|
514
|
+
end
|
515
|
+
|
516
|
+
values.each_with_object({}) do |(k, _), update|
|
517
|
+
update[k] = ""
|
518
|
+
end
|
519
|
+
end
|
520
|
+
end
|
521
|
+
end
|