vivialconnect 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/vivialconnect.rb +14 -0
- data/lib/vivialconnect/account.rb +278 -0
- data/lib/vivialconnect/attachment.rb +74 -0
- data/lib/vivialconnect/client.rb +326 -0
- data/lib/vivialconnect/configuration.rb +172 -0
- data/lib/vivialconnect/contact.rb +4 -0
- data/lib/vivialconnect/log.rb +71 -0
- data/lib/vivialconnect/message.rb +222 -0
- data/lib/vivialconnect/number.rb +296 -0
- data/lib/vivialconnect/resource.rb +261 -0
- data/lib/vivialconnect/user.rb +158 -0
- data/lib/vivialconnect/version.rb +3 -0
- data/lib/vivialconnect/vivial_connect_error.rb +2 -0
- metadata +170 -0
@@ -0,0 +1,296 @@
|
|
1
|
+
module VivialConnect
|
2
|
+
##
|
3
|
+
#=== .all
|
4
|
+
#
|
5
|
+
#Returns an array containing ruby objects corresponding to all Number resources on your account
|
6
|
+
#
|
7
|
+
#
|
8
|
+
# Example usage:
|
9
|
+
#
|
10
|
+
# VivialConnect::Number.all
|
11
|
+
# => [#<VivialConnect::Number account_id=1XXXX, active=true, address_requirements="none", capabilities={"mms"=>true, "sms"=>true, "voice"=>true}, city="ALMELUND", date_created="2017-04-25T09:53:11-04:00", date_modified="2017-04-25T09:53:11-04:00", id=65, lata=nil, master_account_id=1XXXX, message_status_callback=nil, name="(612) 299-1726", phone_number="+16122991726", phone_number_type="local", rate_center="TWINCITIES", region="MN", sms_configuration_id=nil, sms_fallback_method=nil, sms_fallback_url=nil, sms_method=nil, sms_url=nil, voice_forwarding_number=nil>]
|
12
|
+
#
|
13
|
+
#
|
14
|
+
#
|
15
|
+
##
|
16
|
+
#=== .available_numbers(options={})
|
17
|
+
#
|
18
|
+
#Returns an array of available numbers corresponding to your search parameters
|
19
|
+
#
|
20
|
+
#
|
21
|
+
# You must specify exactly one of the following three parameters: in_region, area_code, in_postal_code
|
22
|
+
#
|
23
|
+
# in_region | String | Filters the results include only phone numbers in a specified 2-letter region (US state).
|
24
|
+
# area_code | Fixnum | Filters the results to include only phone numbers by US area code.
|
25
|
+
# in_postal_code | Fixnum | Filters the results to include only phone numbers in a specified 5-digit postal code.
|
26
|
+
#
|
27
|
+
# Optional parameters:
|
28
|
+
#
|
29
|
+
# limit | Fixnum | Number of results to return per page. Default value: 50.
|
30
|
+
# contains | String | Filters the results to include only phone numbers that match a number pattern you specify. The pattern can include letters, digits, and the following wildcard characters:
|
31
|
+
# -- ? : matches any single digit
|
32
|
+
# -- * : matches zero or more digits
|
33
|
+
# local_number | Fixnum | Filters the results to include only phone numbers that match the first three or more digits you specify to immediately follow the area code. To use this parameter, you must also specify an area_code.
|
34
|
+
# in_city | String | Filters the results to include only phone numbers in a specified city.
|
35
|
+
#
|
36
|
+
#
|
37
|
+
# Example Usage:
|
38
|
+
# VivialConnect::Number.available_numbers(in_region: "PA", in_city: "Chambersburg", limit: 10)
|
39
|
+
# => [#<VivialConnect::Number city="CHAMBERSBG", lata=nil, name="(717) 496-4018", phone_number="+17174964018", phone_number_type="local", rate_center="CHAMBERSBG", region="PA">, #<VivialConnect::Number city="CHAMBERSBG", lata=nil, name="(717) 496-4019", phone_number="+17174964019", phone_number_type="local", rate_center="CHAMBERSBG", region="PA">, ... 9 more numbers ]
|
40
|
+
#
|
41
|
+
#
|
42
|
+
##
|
43
|
+
#=== .count
|
44
|
+
#
|
45
|
+
#Returns the number of phone numbers associated with your account.
|
46
|
+
#
|
47
|
+
#
|
48
|
+
# Example usage:
|
49
|
+
#
|
50
|
+
# VivialConnect::Number.count
|
51
|
+
# => 5
|
52
|
+
#
|
53
|
+
#
|
54
|
+
##
|
55
|
+
#=== .create(options={})
|
56
|
+
#
|
57
|
+
#Buys a number and attaches it to your account. This method is aliased as .buy(options={}) as well. Returns the number you just aquired as an object.
|
58
|
+
#
|
59
|
+
#
|
60
|
+
# You must specify at least one of the following two parameters:
|
61
|
+
#
|
62
|
+
#
|
63
|
+
# phone_number | String | "+16125551212" <--NOTE: "+1" in front of the number.
|
64
|
+
# area_code | String | "612"
|
65
|
+
#
|
66
|
+
#
|
67
|
+
# Optional parameters:
|
68
|
+
#
|
69
|
+
# name | String | New phone number as it is displayed to users. Default format: Friendly national format: (xxx) yyy-zzzz.
|
70
|
+
# message_status_callback | String | URL to receive message status callback requests for messages sent via the API using this associated phone number. Max. length: 256 characters.
|
71
|
+
# sms_configuration_id | String | Unique identifier of the message status callback configuration to be used to handle SMS messages sent to the associated number.
|
72
|
+
# sms_url | String | sms_url URL for receiving SMS messages to the associated phone number. Max. length: 256 characters.
|
73
|
+
# sms_method | String | HTTP method used for the sms_url requests. Max. length: 8 characters. Possible values: GET or POST. Default value: POST.
|
74
|
+
# sms_fallback_url | String | URL for receiving SMS messages if sms_url fails. Only valid if you provide a value for the sms_url parameter. Max. length: 256 characters.
|
75
|
+
# sms_fallback_method | String | HTTP method used for sms_url_fallback requests. Max. length: 8 characters. Possible values: GET or POST. Default value: POST.
|
76
|
+
#
|
77
|
+
# Example usage:
|
78
|
+
#
|
79
|
+
# VivialConnect::Number.create(phone_number: "+14124330365")
|
80
|
+
# VivialConnect::Number.buy(phone_number: "+14124330365")
|
81
|
+
# => #<VivialConnect::Number account_id=1XXXX, active=true, address_requirements="none", capabilities={"mms"=>true, "sms"=>true, "voice"=>true}, city="PITTSBURGH ", date_created="2017-04-25T09:56:04-04:00", date_modified="2017-04-25T09:56:04-04:00", id=66, lata=nil, master_account_id=10096, message_status_callback=nil, name="(412) 433-0365", phone_number="+14124330365", phone_number_type="local", rate_center="PTTSBGZON1", region="PA", sms_configuration_id=nil, sms_fallback_method=nil, sms_fallback_url=nil, sms_method=nil, sms_url=nil, voice_forwarding_number=nil>
|
82
|
+
#
|
83
|
+
##
|
84
|
+
#=== .delete(id)
|
85
|
+
#
|
86
|
+
#Deletes a phone number with the provided id from your account and returns true. WARNING: this cannot be undone. Once the number is released from your account it can be aquired by other users.
|
87
|
+
#
|
88
|
+
# Example usage:
|
89
|
+
#
|
90
|
+
# VivialConnect::Number.delete(100230)
|
91
|
+
# => true
|
92
|
+
#
|
93
|
+
#
|
94
|
+
##
|
95
|
+
#=== .filter_all(options={})
|
96
|
+
#
|
97
|
+
#Returns an array of number objects the contents of which are determined by the search parameters
|
98
|
+
#
|
99
|
+
#
|
100
|
+
# Parameters:
|
101
|
+
#
|
102
|
+
# limit | Fixnum | Number of results to return per page. Default value: 50.
|
103
|
+
# contains | String | Filters the results to include only phone numbers that match a number pattern you specify. The pattern can include letters, digits, and the following wildcard characters:
|
104
|
+
# -- ? : matches any single digit
|
105
|
+
# -- * : matches zero or more digits
|
106
|
+
# page | Fixnum | Page number within the returned list of associated phone numbers. Default value: 1.
|
107
|
+
# name | String | Filters the results to include only phone numbers exactly matching the specified name.
|
108
|
+
#
|
109
|
+
# NOTE: If you do not pass any filtering parameters, you will get an array containing all of your phone_numbers i.e.
|
110
|
+
# the same behavior as .all
|
111
|
+
#
|
112
|
+
#
|
113
|
+
# Example usage:
|
114
|
+
#
|
115
|
+
# VivialConnect::Number.filter_all(contains: "612")
|
116
|
+
# => [#<VivialConnect::Number phone_number="+1612XXXXXXX">, #<VivialConnect::Number phone_number="+1612XXXXXXX">, ...]
|
117
|
+
#
|
118
|
+
#
|
119
|
+
##
|
120
|
+
#=== .find(id)
|
121
|
+
#
|
122
|
+
#Returns number object with the id provided
|
123
|
+
#
|
124
|
+
#
|
125
|
+
# Required parameter:
|
126
|
+
#
|
127
|
+
# id | Fixnum | 726
|
128
|
+
#
|
129
|
+
#
|
130
|
+
# Example usage:
|
131
|
+
#
|
132
|
+
# VivialConnect::Number.find(726)
|
133
|
+
# => #<VivialConnect::Number account_id=1XXXX, active=true, address_requirements="none", capabilities={"mms"=>true, "sms"=>true, "voice"=>true}, city="ALMELUND", date_created="2017-04-25T10:22:07-04:00", date_modified="2017-04-25T10:41:16-04:00", id=726, lata=nil, master_account_id=1XXXX, message_status_callback=nil, name="Dr. Jones", phone_number="+16123151041", phone_number_type="local", rate_center="TWINCITIES", region="MN", sms_configuration_id=nil, sms_fallback_method=nil, sms_fallback_url=nil, sms_method=nil, sms_url=nil, voice_forwarding_number=nil>
|
134
|
+
#
|
135
|
+
#
|
136
|
+
##
|
137
|
+
#=== .find_each(start: 1, finish: nil, batch_size: 150)
|
138
|
+
#
|
139
|
+
#Iterates through all of the numbers on your account in N sized batches and returns an array containing all the numbers beginning at the `start: value` and ending at the `finish: value`.
|
140
|
+
#
|
141
|
+
#
|
142
|
+
# When a block is given this method yields an individual Number object.
|
143
|
+
# Without a block, this method returns an Enumerator.
|
144
|
+
#
|
145
|
+
#
|
146
|
+
# By default, it will begin at the first number and end at the last number
|
147
|
+
#
|
148
|
+
#
|
149
|
+
# With default batch_size: 150, if you wanted to get your records from
|
150
|
+
# 150 to 300 you would start at 2 and finish at 2.
|
151
|
+
#
|
152
|
+
#
|
153
|
+
# Returns an Array of objects corresponding to the `start` and `finish` values. Default is all objects.
|
154
|
+
#
|
155
|
+
# Optional parameters:
|
156
|
+
#
|
157
|
+
# start | Fixnum | batch to start with
|
158
|
+
# finish | Fixnum | batch to end with
|
159
|
+
# batch_size | Fixnum | between 1..150
|
160
|
+
#
|
161
|
+
#
|
162
|
+
# Example usage:
|
163
|
+
#
|
164
|
+
#
|
165
|
+
# VivialConnect::Number.find_each {|number| puts number.name}
|
166
|
+
# example number name
|
167
|
+
# => [#<VivialConnect::Number account_id=1XXXX>, #<VivialConnect::Number account_id=1XXXX>, ... ]
|
168
|
+
#
|
169
|
+
#
|
170
|
+
##
|
171
|
+
#=== .find_in_batches(start: 1, finish: nil, batch_size: 150)
|
172
|
+
#
|
173
|
+
#Iterates through all of the numbers on your account in N sized batches and returns an array containing all the numbers beginning at the `start: value` and ending at the `finish: value`.
|
174
|
+
#
|
175
|
+
#
|
176
|
+
# When a block is given this method yields an array of batch_size resource objects.
|
177
|
+
# Without a block, it returns an Enumerator.
|
178
|
+
#
|
179
|
+
#
|
180
|
+
# By default, it will begin at the first number and end at the last number
|
181
|
+
#
|
182
|
+
#
|
183
|
+
# With default batch_size: 150, if you wanted to get your records from
|
184
|
+
# 150 to 300 you would start at 2 and finish at 2.
|
185
|
+
#
|
186
|
+
#
|
187
|
+
# Returns an Array of objects corresponding to the `start` and `finish` values. Default is all objects.
|
188
|
+
#
|
189
|
+
# Optional parameters:
|
190
|
+
#
|
191
|
+
# start | Fixnum | batch to start with
|
192
|
+
# finish | Fixnum | batch to end with
|
193
|
+
# batch_size | Fixnum | between 1..150
|
194
|
+
#
|
195
|
+
#
|
196
|
+
# Example usage:
|
197
|
+
#
|
198
|
+
#
|
199
|
+
# VivialConnect::Number.find_in_batches {|batch| do_something_with_an_array(batch)}
|
200
|
+
# => [#<VivialConnect::Number account_id=1XXXX>, #<VivialConnect::Number account_id=1XXXX, ... ]
|
201
|
+
#
|
202
|
+
#
|
203
|
+
##
|
204
|
+
#=== .local
|
205
|
+
#
|
206
|
+
#Returns an array of all US local numbers owned by your account
|
207
|
+
#
|
208
|
+
#
|
209
|
+
# Example usage:
|
210
|
+
#
|
211
|
+
#
|
212
|
+
# VivialConnect::Number.local
|
213
|
+
# => [#<VivialConnect::Number account_id=1XXXX, active=true, address_requirements="none", capabilities={"mms"=>true, "sms"=>true, "voice"=>true}, city="BELCHERTOWN", date_created="2017-02-06T15:51:27-05:00", date_modified="2017-04-10T17:43:17-04:00", id=27, lata=nil, master_account_id=1XXXX, message_status_callback=nil, name="App Number 1", phone_number="+1XXXXXXXXXX", phone_number_type="local", rate_center="BELCHERTN", region="MA", sms_configuration_id=nil, sms_fallback_method=nil, sms_fallback_url=nil, sms_method=nil, sms_url="http://requestb.in/174w8nz1", voice_forwarding_number="+1XXXXXXXXXX">]
|
214
|
+
#
|
215
|
+
#
|
216
|
+
##
|
217
|
+
#=== .lookup(number)
|
218
|
+
#
|
219
|
+
#Returns lookup information for a phone number
|
220
|
+
#
|
221
|
+
# Required parameter:
|
222
|
+
# number | String | "+1XXXXXXXXXX" <--NOTE: "+1" in front of the number.
|
223
|
+
#
|
224
|
+
# Example Usage:
|
225
|
+
#
|
226
|
+
#
|
227
|
+
# VivialConnect::Number.lookup("+1XXXXXXXXXX")
|
228
|
+
# => #<VivialConnect::Number carrier={"capabilities"=>{"deactivation_files"=>true, "device_lookup_api"=>true, "location_data"=>true, "mms_connection_status"=>true, "mms_dr"=>true, "sms_connection_status"=>true, "sms_fteu"=>true, "sms_handset_dr"=>true, "uaprof_in_mms_dr"=>true}, "country"=>"US", "name"=>"T-Mobile"}, date_created="2017-04-06T11:39:48-04:00", date_modified="2017-04-06T11:39:48-04:00", device={"error"=>"15012- Device lookup is not supported by this carrier.", "model"=>nil}, phone_number="+1XXXXXXXXXX">
|
229
|
+
#
|
230
|
+
#
|
231
|
+
##
|
232
|
+
#=== .update(id, options={})
|
233
|
+
#
|
234
|
+
#Updates the phone_number record and returns the record as an object.
|
235
|
+
#
|
236
|
+
# Required parameter:
|
237
|
+
#
|
238
|
+
# id | Fixnum | 875
|
239
|
+
#
|
240
|
+
#
|
241
|
+
# Optional parameters:
|
242
|
+
#
|
243
|
+
# name | String | New phone number as it is displayed to users. Default format: Friendly national format: (xxx) yyy-zzzz.
|
244
|
+
# message_status_callback | String | URL to receive message status callback requests for messages sent via the API using this associated phone number. Max. length: 256 characters.
|
245
|
+
# sms_configuration_id | String | Unique identifier of the message status callback configuration to be used to handle SMS messages sent to the associated number.
|
246
|
+
# sms_url | String | sms_url URL for receiving SMS messages to the associated phone number. Max. length: 256 characters.
|
247
|
+
# sms_method | String | HTTP method used for the sms_url requests. Max. length: 8 characters. Possible values: GET or POST. Default value: POST.
|
248
|
+
# sms_fallback_url | String | URL for receiving SMS messages if sms_url fails. Only valid if you provide a value for the sms_url parameter. Max. length: 256 characters.
|
249
|
+
# sms_fallback_method | String | HTTP method used for sms_url_fallback requests. Max. length: 8 characters. Possible values: GET or POST. Default value: POST.
|
250
|
+
#
|
251
|
+
# Example usage:
|
252
|
+
#
|
253
|
+
#
|
254
|
+
# VivialConnect::Number.update(875, name: "Dr. Jones")
|
255
|
+
# => #<VivialConnect::Number account_id=10096, active=true, address_requirements="none", capabilities={"mms"=>true, "sms"=>true, "voice"=>true}, city="ALMELUND", date_created="2017-04-25T10:22:07-04:00", date_modified="2017-04-25T10:41:16-04:00", id=69, lata=nil, master_account_id=1XXXX, message_status_callback=nil, name="Dr. Jones", phone_number="+16123151041", phone_number_type="local", rate_center="TWINCITIES", region="MN", sms_configuration_id=nil, sms_fallback_method=nil, sms_fallback_url=nil, sms_method=nil, sms_url=nil, voice_forwarding_number=nil> #<VivialConnect::Number account_id=1XXXX, active=true, address_requirements="none", capabilities={"mms"=>true, "sms"=>true, "voice"=>true}, city="PITTSBURGH ", date_created="2017-04-25T09:56:04-04:00", date_modified="2017-04-25T09:56:04-04:00", id=66, lata=nil, master_account_id=10096, message_status_callback=nil, name="(412) 433-0365", phone_number="+14124330365", phone_number_type="local", rate_center="PTTSBGZON1", region="PA", sms_configuration_id=nil, sms_fallback_method=nil, sms_fallback_url=nil, sms_method=nil, sms_url=nil, voice_forwarding_number=nil>
|
256
|
+
#
|
257
|
+
|
258
|
+
class Number < Resource
|
259
|
+
|
260
|
+
singleton_class.send(:alias_method, :buy, :create)
|
261
|
+
|
262
|
+
def self.available_numbers(options={}) # :nodoc:
|
263
|
+
country_code ='US'
|
264
|
+
number_type='local'
|
265
|
+
uri = "/numbers/available/#{country_code}/#{number_type}.json"
|
266
|
+
numbers_template = Addressable::Template.new("#{uri}{?query*}")
|
267
|
+
uri = numbers_template.expand(query: options).to_s
|
268
|
+
VivialConnect::Client.instance.make_request('GET', uri)
|
269
|
+
end
|
270
|
+
|
271
|
+
def self.filter_all(options={}) # :nodoc:
|
272
|
+
country_code ='US'
|
273
|
+
number_type='local'
|
274
|
+
uri = "/numbers.json"
|
275
|
+
numbers_template = Addressable::Template.new("#{uri}{?query*}")
|
276
|
+
uri = numbers_template.expand(query: options).to_s
|
277
|
+
VivialConnect::Client.instance.make_request('GET', uri)
|
278
|
+
end
|
279
|
+
|
280
|
+
def self.lookup(number) # :nodoc:
|
281
|
+
number = {phone_number: number}
|
282
|
+
uri='/numbers/lookup.json'
|
283
|
+
numbers_template = Addressable::Template.new("#{uri}{?query*}")
|
284
|
+
uri = numbers_template.expand(query: number).to_s
|
285
|
+
VivialConnect::Client.instance.make_request('GET', uri)
|
286
|
+
end
|
287
|
+
|
288
|
+
def self.local # :nodoc:
|
289
|
+
VivialConnect::Client.instance.make_request('GET', '/numbers/local.json')
|
290
|
+
end
|
291
|
+
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
end
|
296
|
+
|
@@ -0,0 +1,261 @@
|
|
1
|
+
##
|
2
|
+
# This class implements the shared behavior of the other resource classes
|
3
|
+
class Resource < OpenStruct # :nodoc:
|
4
|
+
|
5
|
+
##
|
6
|
+
# Creates a Vivial Connect API resource, returns a ruby object containing the newly created resource.
|
7
|
+
# Options hash values are determined by the API for the resource in question.
|
8
|
+
|
9
|
+
def self.create(options = {})
|
10
|
+
#TODO: delete this when more phone_number_types are possible
|
11
|
+
if self == VivialConnect::Number && !options.keys.include?('phone_number_type')
|
12
|
+
options.merge!(phone_number_type: 'local')
|
13
|
+
end
|
14
|
+
data = build_hash_root_and_add_user_hash(options)
|
15
|
+
data = data.to_json
|
16
|
+
uri = path_builder(:create)
|
17
|
+
VivialConnect::Client.instance.make_request('POST', uri, data)
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Returns an array containing all of the API resources
|
22
|
+
|
23
|
+
def self.all
|
24
|
+
final_array = []
|
25
|
+
page = 1
|
26
|
+
limit = 100
|
27
|
+
while true
|
28
|
+
path = path_builder(:all)
|
29
|
+
uri = build_template_uri(path: path, start: page, batch_size: limit)
|
30
|
+
current_iteration = VivialConnect::Client.instance.make_request('GET', uri)
|
31
|
+
final_array = update_final_array(current_iteration, final_array)
|
32
|
+
break final_array if current_iteration.count < limit
|
33
|
+
page += 1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Iterates through each record of a resource in batches of N
|
39
|
+
# beginning at the start: value and ending at the finish: value.
|
40
|
+
#
|
41
|
+
# When a block is given this method yields an individual resource objects.
|
42
|
+
# Without a block, this method creates an Enumerator.
|
43
|
+
#
|
44
|
+
# With default batch_size: 150, if you wanted to get your records from
|
45
|
+
# 150 to 300 you would start at 2 and finish at 2.
|
46
|
+
|
47
|
+
|
48
|
+
def self.find_each(start: 1, finish: nil, batch_size: 150)
|
49
|
+
return to_enum(:find_each, start: start, finish: finish, batch_size: batch_size) unless block_given?
|
50
|
+
final_array = []
|
51
|
+
page = start
|
52
|
+
|
53
|
+
while true
|
54
|
+
path = path_builder(:all)
|
55
|
+
uri = build_template_uri(path: path, start: page, batch_size: batch_size)
|
56
|
+
current_batch = VivialConnect::Client.instance.make_request('GET', uri)
|
57
|
+
final_array = update_final_array(current_batch, final_array)
|
58
|
+
|
59
|
+
current_batch.each { |record| yield record }
|
60
|
+
|
61
|
+
if current_batch.count < batch_size || page == finish
|
62
|
+
break final_array
|
63
|
+
end
|
64
|
+
page += 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# Iterates through resources in batches of N
|
70
|
+
# beginning at the start: value and ending at the finish: value, in batch_sized chunks.
|
71
|
+
#
|
72
|
+
# When a block is given this method yields an array of batch_size resource objects.
|
73
|
+
# Without a block, it returns an Enumerator.
|
74
|
+
|
75
|
+
|
76
|
+
def self.find_in_batches(start: 1, finish: nil, batch_size: 150)
|
77
|
+
return to_enum(:find_in_batches, start: start, finish: finish, batch_size: batch_size) unless block_given?
|
78
|
+
final_array = []
|
79
|
+
page = start
|
80
|
+
|
81
|
+
while true
|
82
|
+
path = path_builder(:all)
|
83
|
+
uri = build_template_uri(path: path, start: page, batch_size: batch_size)
|
84
|
+
current_batch = VivialConnect::Client.instance.make_request('GET', uri)
|
85
|
+
final_array = update_final_array(current_batch, final_array)
|
86
|
+
|
87
|
+
yield current_batch
|
88
|
+
|
89
|
+
if current_batch.count < batch_size || page == finish
|
90
|
+
break final_array
|
91
|
+
end
|
92
|
+
page += 1
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
##
|
98
|
+
# Returns the resource defined by the provided id.
|
99
|
+
|
100
|
+
def self.find(id)
|
101
|
+
uri = path_builder(:find, id)
|
102
|
+
VivialConnect::Client.instance.make_request('GET', uri)
|
103
|
+
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# Returns the amount of resources.
|
107
|
+
|
108
|
+
def self.count
|
109
|
+
uri = path_builder(:count)
|
110
|
+
VivialConnect::Client.instance.make_request('GET', uri)
|
111
|
+
end
|
112
|
+
|
113
|
+
##
|
114
|
+
# Updates the record defined by the provided id with data from the options hash.
|
115
|
+
# Options hash values are determined by the API for the resource in question
|
116
|
+
|
117
|
+
def self.update(id, options={})
|
118
|
+
options = options.merge(id: id)
|
119
|
+
data = build_hash_root_and_add_user_hash(options)
|
120
|
+
data = data.to_json
|
121
|
+
uri = path_builder(:update, id)
|
122
|
+
VivialConnect::Client.instance.make_request('PUT', uri, data)
|
123
|
+
end
|
124
|
+
|
125
|
+
##
|
126
|
+
# Deletes the record defined by the provided id.
|
127
|
+
# NOTE: not possible for all resource types
|
128
|
+
|
129
|
+
def self.delete(id)
|
130
|
+
uri = path_builder(:delete, id)
|
131
|
+
data = {}
|
132
|
+
data['id'] = id
|
133
|
+
data = data.to_json
|
134
|
+
VivialConnect::Client.instance.make_request('DELETE', uri, data)
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
def self.redact(id)
|
139
|
+
raise VivialConnectClientError, "You can only redact message objects" unless self == VivialConnect::Message
|
140
|
+
data = {}
|
141
|
+
data['message'] = {id: id, body: ""}
|
142
|
+
data = data.to_json
|
143
|
+
VivialConnect::Client.instance.make_request('PUT',"/messages/#{id}.json", data)
|
144
|
+
end
|
145
|
+
|
146
|
+
def add_methods(options={}) # :nodoc:
|
147
|
+
options.each do |k,v|
|
148
|
+
new_ostruct_member(k)
|
149
|
+
send "#{k}=" , v
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def self.path_builder(resource_method, id=nil) # :nodoc:
|
154
|
+
json = '.json'
|
155
|
+
base = class_to_path
|
156
|
+
if resource_method == :create || resource_method == :all
|
157
|
+
url = base + json
|
158
|
+
elsif resource_method == :find || resource_method == :delete || resource_method == :update
|
159
|
+
url = base + '/' + id.to_s + json
|
160
|
+
elsif resource_method == :count
|
161
|
+
if self == VivialConnect::Account
|
162
|
+
url = '/' + base + '/' + 'count' + json
|
163
|
+
else
|
164
|
+
url = base + '/' + 'count' + json
|
165
|
+
end
|
166
|
+
end
|
167
|
+
return url
|
168
|
+
end
|
169
|
+
|
170
|
+
def self.pluralize(string) # :nodoc:
|
171
|
+
# avoids importing a library but will need to be improved upon depending on future api resource names
|
172
|
+
string + 's'
|
173
|
+
end
|
174
|
+
|
175
|
+
def self.build_hash_root_and_add_user_hash(options) # :nodoc:
|
176
|
+
root = class_to_json_root
|
177
|
+
data = {}
|
178
|
+
if root != "number"
|
179
|
+
data[root] = options
|
180
|
+
return data
|
181
|
+
else
|
182
|
+
data['phone_number'] = options
|
183
|
+
return data
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def self.class_to_path # :nodoc:
|
188
|
+
base = self.to_s.split('::')[-1].downcase
|
189
|
+
base = pluralize(base)
|
190
|
+
base = base.prepend('/') unless base == 'accounts'
|
191
|
+
base
|
192
|
+
end
|
193
|
+
|
194
|
+
def self.class_to_json_root # :nodoc:
|
195
|
+
base = self.to_s.split('::')[-1]
|
196
|
+
base = base.split("")
|
197
|
+
final = []
|
198
|
+
final << base[0]
|
199
|
+
base[1..-1].each do |letter|
|
200
|
+
if letter == letter.upcase
|
201
|
+
final << "_" + letter
|
202
|
+
else
|
203
|
+
final << letter
|
204
|
+
end
|
205
|
+
end
|
206
|
+
final.join("").downcase
|
207
|
+
end
|
208
|
+
|
209
|
+
def self.build_template_uri(path: p, start: nil, batch_size: nil) # :nodoc:
|
210
|
+
all_template = Addressable::Template.new("#{path}{?query*}")
|
211
|
+
uri = all_template.expand(query: {page: start, limit: batch_size}).to_s
|
212
|
+
end
|
213
|
+
|
214
|
+
def self.update_final_array(current_iteration, final_array) # :nodoc:
|
215
|
+
final_array << current_iteration
|
216
|
+
final_array.flatten!
|
217
|
+
end
|
218
|
+
|
219
|
+
##
|
220
|
+
# This instance method saves the current object if it does not have an id
|
221
|
+
# and updates the existing object if it does have an id. It is equivalent to calling .create or .update
|
222
|
+
# on the instance level.
|
223
|
+
|
224
|
+
def save
|
225
|
+
if self.id.nil?
|
226
|
+
data = self.class.build_hash_root_and_add_user_hash(self.to_h)
|
227
|
+
data = data.to_json
|
228
|
+
uri = self.class.path_builder(:create)
|
229
|
+
response_object = VivialConnect::Client.instance.make_request('POST', uri, data)
|
230
|
+
new_object_hash =response_object.marshal_dump
|
231
|
+
self.add_methods(new_object_hash)
|
232
|
+
self
|
233
|
+
else
|
234
|
+
options = self.to_h
|
235
|
+
data = self.class.build_hash_root_and_add_user_hash(self.to_h)
|
236
|
+
data = data.to_json
|
237
|
+
uri = self.class.path_builder(:update, id)
|
238
|
+
response_object = VivialConnect::Client.instance.make_request('PUT', uri, data)
|
239
|
+
new_object_hash =response_object.marshal_dump
|
240
|
+
self.add_methods(new_object_hash)
|
241
|
+
self
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
##
|
246
|
+
# This instance method deletes the current object.
|
247
|
+
# NOTE: not all resouce types can be deleted
|
248
|
+
|
249
|
+
def delete
|
250
|
+
options = self.to_h
|
251
|
+
uri = self.class.path_builder(:delete, self.id)
|
252
|
+
data = {}
|
253
|
+
data['id'] = self.id
|
254
|
+
data = data.to_json
|
255
|
+
VivialConnect::Client.instance.make_request('DELETE', uri, data)
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
end
|