tropo-provisioning 0.0.21 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+
2
+ ##
3
+ # Models an error while accessing the Tropo provisioning API
4
+ class TropoError < RuntimeError
5
+ attr_reader :http_status
6
+
7
+ ##
8
+ # Initializer
9
+ def initialize(http_status = '500')
10
+ @http_status = http_status
11
+ end
12
+ end
@@ -0,0 +1,930 @@
1
+ require 'active_support'
2
+ require 'active_support/inflector'
3
+
4
+ ##
5
+ # This class is a wrapper that allows an easy way to access Tropo HTTP Provisioning API
6
+ # It defines a set of methods to create, update, retrieve or delete different kind of resources.
7
+ class TropoProvisioning
8
+
9
+ autoload :TropoClient, 'tropo-provisioning/tropo_client'
10
+ autoload :TropoError, 'tropo-provisioning/tropo_error'
11
+
12
+ # Defaults for the creation of applications
13
+ DEFAULT_OPTIONS = { :partition => 'staging', :platform => 'scripting' }
14
+
15
+ # Array of supported platforms in Tropo
16
+ VALID_PLATFORMS = %w(scripting webapi)
17
+ # Array of supported partitions in Tropo
18
+ VALID_PARTITIONS = %w(staging production)
19
+
20
+ attr_reader :user_data
21
+
22
+ ##
23
+ # Creates a new TropoProvisioning object
24
+ #
25
+ # ==== Parameters
26
+ # * [required, String] username for your Tropo user
27
+ # * [required, String] password for your Tropo user
28
+ # * [optional, Hash] params
29
+ # * [optional, String] :base_uri to use for accessing the provisioning API if you would like a custom one
30
+ #
31
+ # ==== Return
32
+ #
33
+ # TropoProvisioning object
34
+ def initialize(username, password, params={})
35
+ base_uri = params[:base_uri] || "http://api.tropo.com/v1/"
36
+ @tropo_client = TropoClient.new(username, password, base_uri, { 'Content-Type' => 'application/json' })
37
+ user(username)
38
+ end
39
+
40
+ ##
41
+ # Retrieves specific user information
42
+ # ==== Parameters
43
+ def account(username, password)
44
+ case current_method_name
45
+ when 'account'
46
+ action = 'get'
47
+ when 'authenticate_account'
48
+ action = 'authenticate'
49
+ end
50
+ temp_request("/#{action}.jsp?username=#{username}&password=#{password}")
51
+ end
52
+
53
+ ##
54
+ # Username used for HTTP authentication (valid Tropo user)
55
+ def username
56
+ @tropo_client.username
57
+ end
58
+
59
+ alias :authenticate_account :account
60
+
61
+ ##
62
+ # Obtain information about a user
63
+ #
64
+ # ==== Parameters
65
+ # * [required, String] the user ID or username to obtain the account details of
66
+ #
67
+ # ==== Return
68
+ # * [Hash]
69
+ # contains the information on the user
70
+ def user(user_identifier)
71
+ result = @tropo_client.get("users/#{user_identifier}")
72
+ if result['username']
73
+ # Only add/update this if we are fetching the user we are logged in as
74
+ result['username'].downcase == username.downcase and @user_data = result
75
+ end
76
+ result
77
+ end
78
+
79
+ ##
80
+ # Confirms a user after they have been created. For example, you may want to email your user to make
81
+ # sure they are real before activating the user.
82
+ #
83
+ # ==== Parameters
84
+ # * [required, String] user_id returned when you created the user you now want to confirm
85
+ # * [required, String] confirmation_key returned when you created the user you now want to confirm
86
+ # * [required, String] the ip_address of the user client that did the confirmation
87
+ #
88
+ # ==== Return
89
+ # * [Hash]
90
+ # contains a message key confirming the confirmation was successful
91
+ def confirm_user(user_id, confirmation_key, ip_address)
92
+ params = { :key => confirmation_key, :endUserHost => ip_address }
93
+ @tropo_client.post("users/#{user_id}/confirmations", params)
94
+ end
95
+
96
+ ##
97
+ # Creates a new user in a pending state. Once you receive the href/user_id/confirmation_key
98
+ # you may then invoke the confirm_user method once you have taken appropriate steps to confirm the
99
+ # user
100
+ #
101
+ # ==== Parameters
102
+ # * [required, Hash] params to create the user
103
+ # * [required, String] :username the name of the user to create the user for
104
+ # * [required, String] :password the password to use for the user
105
+ # * [required, String] :email the email address to use
106
+ # * [optional, String] :first_name of the user
107
+ # * [optional, String] :last_name of the user
108
+ # * [optional, String] :website the URL of the user's website
109
+ # * [optional, String] :organization of the user, such as a company name
110
+ # * [optional, String] :job_title of the user
111
+ # * [optional, String] :address of the user
112
+ # * [optional, String] :address2 second live of the address of the user
113
+ # * [optional, String] :city of the user
114
+ # * [optional, String] :state of the user
115
+ # * [optional, String] :postal_code of the user
116
+ # * [optional, String] :country of the user
117
+ # * [optional, String] :marketing_opt_in
118
+ # * [optional, String] :twitter_id
119
+ # * [optional, String] :joined_from_host IP address of the host they signed up from
120
+ #
121
+ # ==== Return
122
+ # * [Hash] details of the user created
123
+ # includes the href, user_id and confirmation_key
124
+ # * [ArgumentError]
125
+ # if missing the :username, :password or :email parameters
126
+ def create_user(params={})
127
+ validate_params(params, %w(username password email))
128
+
129
+ # Set the Company Branding ID, or use default
130
+ params[:website] = 'tropo' unless params[:website]
131
+
132
+ result = @tropo_client.post("users", params)
133
+ result[:user_id] = get_element(result.href)
134
+ result[:confirmation_key] = result['confirmationKey']
135
+ result.delete('confirmationKey')
136
+ result
137
+ end
138
+
139
+ ##
140
+ # Modify/update an existing user
141
+ #
142
+ # ==== Parameters
143
+ # * [required, String] user_id of the user you would like to update
144
+ # * [required, Hash] the parameters of the user you would like to update
145
+ #
146
+ # ==== Return
147
+ # * [Hash]
148
+ # the href of the resource that was modified/updated
149
+ def modify_user(user_id, params={})
150
+ result = @tropo_client.put("users/#{user_id}", params)
151
+ if result['href']
152
+ # Only add/update this if we are fetching the user we are logged in as
153
+ @user_data.merge!(params) if user_id == @user_data['id']
154
+ end
155
+ result
156
+ end
157
+
158
+ ##
159
+ # Allows you to search users to find a list of users
160
+ #
161
+ # ==== Parameters
162
+ # * [required] search_term
163
+ # * [String] a key/value of the search term you would like to use, such as 'username=foobar', or 'city=Orlando'
164
+ # * [Hash] a Hash instance, such as {:username => "foobar"}, or {:city => "Orlando"}
165
+ #
166
+ # ==== Return
167
+ # * [Array]
168
+ # a hash containing an array of hashes with the qualifying account details
169
+ def search_users(search_term)
170
+ if search_term.is_a?(String)
171
+ @tropo_client.get("users/?#{search_term}")
172
+ elsif search_term.is_a?(Hash)
173
+ @tropo_client.get('users/', search_term)
174
+ else
175
+ nil
176
+ end
177
+ end
178
+
179
+ ##
180
+ # Allows you to search if a username exists or not
181
+ #
182
+ # ==== Parameters
183
+ # * [required, String] a username to check
184
+ # ==== Return
185
+ # * [Array]
186
+ # a hash containing an array of hashes with the qualifying account details
187
+ def username_exists?(username)
188
+ @tropo_client.get("usernames/#{username}")
189
+ end
190
+
191
+ ##
192
+ # Fetches the payment information for a user
193
+ #
194
+ # ==== Parameters
195
+ # * [required, String] user_id to fetch the payment details for
196
+ #
197
+ # ==== Return
198
+ # * [Hash]
199
+ # a hash containing the accountNumber, paymentType, paymentTypeName, rechargeAmount and rechargeThreshold
200
+ def user_payment_method(user_id)
201
+ result = @tropo_client.get("users/#{user_id}/payment/method")
202
+ result.merge!({ :id => get_element(result.paymentType) })
203
+ result
204
+ end
205
+
206
+ ##
207
+ # Lists the available payment types
208
+ #
209
+ # ==== Return
210
+ # * [Hash]
211
+ # an array of available payment types that each include an id, href and name
212
+ def available_payment_types
213
+ @tropo_client.get("types/payment")
214
+ end
215
+
216
+ ##
217
+ # Obtain the current balance of a user
218
+ #
219
+ # ==== Parameters
220
+ # * [required, String] user_id of the user to obtain the balance for
221
+ #
222
+ # ==== Return
223
+ # * [Hash]
224
+ # the balance, pendingRechargeAmount and pendingUsageAmount for the user account
225
+ def balance(user_id)
226
+ @tropo_client.get("users/#{user_id}/usage")
227
+ end
228
+
229
+ ##
230
+ # Return the list of available countries
231
+ #
232
+ # ==== Return
233
+ # * [Hash]
234
+ # returns an Array of hashes that include the country details available
235
+ def countries
236
+ result = @tropo_client.get("countries")
237
+ add_ids(result)
238
+ end
239
+
240
+ ##
241
+ # Return the list of available states for a country
242
+ #
243
+ # ==== Return
244
+ # * [Hash]
245
+ # returns an Array of hashes that include the state details for a country that are available
246
+ def states(id)
247
+ result = @tropo_client.get("countries/#{id}/states")
248
+ add_ids(result)
249
+ end
250
+
251
+ ##
252
+ # Lists the available features
253
+ #
254
+ # ==== Return
255
+ # * [Hash]
256
+ # an array of available features that each include an id, href, name and description
257
+ def features
258
+ @tropo_client.get("features")
259
+ end
260
+
261
+ ##
262
+ # Lists the features configured for a user
263
+ #
264
+ # ==== Return
265
+ # * [Hash]
266
+ # an array of available features that each include an href, feature and featureName
267
+ def user_features(user_id)
268
+ @tropo_client.get("users/#{user_id}/features")
269
+ end
270
+
271
+ ##
272
+ # Enable a particular feature for a user
273
+ #
274
+ # ==== Parameters
275
+ # * [required, String] user_id of the user to add the feature to
276
+ # * [required, String] feature identifier of the feature you want to add
277
+ #
278
+ # ==== Return
279
+ # * [Hash]
280
+ # the href of the feature added
281
+ def user_enable_feature(user_id, feature)
282
+ @tropo_client.post("users/#{user_id}/features", { :feature => feature })
283
+ end
284
+
285
+ ##
286
+ # Disable a particular feature for a user
287
+ #
288
+ # ==== Parameters
289
+ # * [required, String] user_id of the user to disable the feature to
290
+ # * [required, String] feature number of the feature you want to disable
291
+ #
292
+ # ==== Return
293
+ # * [Hash]
294
+ # the href of the feature disable
295
+ def user_disable_feature(user_id, feature_number)
296
+ @tropo_client.delete("users/#{user_id}/features/#{feature_number}")
297
+ end
298
+
299
+ ##
300
+ # Add/modify payment info for a user
301
+ #
302
+ # ==== Parameters
303
+ # * [required, String] user_id to add the payment details for
304
+ # * [require, Hash] params the params to add the payment info
305
+ # * [required, String] :account_number the credit card number
306
+ # * [required, String] :payment_type the type, such as visa, mastercard, etc
307
+ # * [required, String] :address
308
+ # * [optional, String] :address2
309
+ # * [required, String] :city
310
+ # * [required, String] :state
311
+ # * [required, String] :postal_code
312
+ # * [required, String] :country
313
+ # * [optional, String] :email
314
+ # * [required, String] :name_on_account name on the credit card
315
+ # * [required, String] :expiration_date expiration date of the credit card
316
+ # * [required, String] :security_code back panel/front panel (Amex) code on the card
317
+ # * [optional, String] :phone_number
318
+ #
319
+ # ==== Return
320
+ # * [Hash]
321
+ # the href of the payment method added
322
+ # * [ArgumentError]
323
+ # if a required param is not present
324
+ def add_payment_info(user_id, params={})
325
+ #validate_params(params, %w(account_number payment_type address city state postal_code country name_on_account expiration_date security_code recharge_amount email phone_number))
326
+ @tropo_client.put("users/#{user_id}/payment/method", params)
327
+ end
328
+ alias :modify_payment_info :add_payment_info
329
+
330
+ ##
331
+ # Add/modify recurring fund amount and threshold
332
+ #
333
+ # ==== Parameters
334
+ # * [required, String] user_id to add the payment details for
335
+ # * [require, Hash] params the params to add the recurrence
336
+ # * [required, Float] :recharge_amount
337
+ # * [required, Float] :recharge_threshold
338
+ #
339
+ # ==== Return
340
+ # * [Hash]
341
+ def update_recurrence(user_id, params={})
342
+ validate_params(params, %w(recharge_amount threshold_percentage))
343
+
344
+ @tropo_client.put("users/#{user_id}/payment/recurrence", params)
345
+ end
346
+
347
+ ##
348
+ # Add/modify recurring fund amount and threshold
349
+ #
350
+ # ==== Parameters
351
+ # * [required, String] user_id to get the recurrence info for
352
+ #
353
+ # ==== Return
354
+ # * [Hash]
355
+ def get_recurrence(user_id)
356
+ result = @tropo_client.get("users/#{user_id}/payment/recurrence")
357
+ end
358
+
359
+ ##
360
+ # Makes a payment on behalf of a user
361
+ #
362
+ # ==== Parameters
363
+ # * [required, String] the user_id to make the payment for
364
+ # * [required, Float] the amount, in US Dollars to make the payment for
365
+ #
366
+ # ==== Return
367
+ # * [Hash]
368
+ # a message with the success or failure of the payment
369
+ def make_payment(user_id, amount)
370
+ amount.instance_of?(Float) or raise ArgumentError, 'amount must be of type Float'
371
+
372
+ @tropo_client.post("users/#{user_id}/payments", { :amount => amount })
373
+ end
374
+
375
+ ##
376
+ # Creates an address to an existing application
377
+ #
378
+ # ==== Parameters
379
+ # * [required, String] application_id to add the address to
380
+ # * [required, Hash] params the parameters used to request the address
381
+ # * [String] :type this defines the type of address. The possibles types are number (phone numbers), pin (reserved), token, aim, jabber, msn, yahoo, gtalk & skype
382
+ # * [String] :prefix this defines the country code and area code for phone numbers
383
+ # * [String] :username the messaging/IM account's username
384
+ # * [String] :password the messaging/IM account's password
385
+ #
386
+ # ==== Return
387
+ # * [Hash] params the key/values that make up the application
388
+ # * [String] :href identifies the address that was added, refer to address method for details
389
+ # * [String] :address the address that was created
390
+ def create_address(application_id, params={})
391
+ validate_address_parameters(params)
392
+
393
+ result = @tropo_client.post("applications/#{application_id.to_s}/addresses", params)
394
+ result[:address] = get_element(result.href)
395
+ result
396
+ end
397
+
398
+ ##
399
+ # Get a specific application
400
+ #
401
+ # ==== Parameters
402
+ # * [required, String] application_id of the application to get
403
+ #
404
+ # ==== Return
405
+ # * [Hash] params the key/values that make up the application
406
+ # * [String] :href the REST address for the application
407
+ # * [String] :name the name of the application
408
+ # * [String] :voiceUrl the URL that powers voice calls for your application
409
+ # * [String] :messagingUrl the URL that powers the SMS/messaging calls for your session
410
+ # * [String] :platform defines whether the application will use the Scripting API or the Web API
411
+ # * [String] :partition defines whether the application is in staging/development or production
412
+ def application(application_id)
413
+ app = @tropo_client.get("applications/#{application_id.to_s}")
414
+ app.merge!({ :application_id => get_element(app.href) })
415
+ end
416
+
417
+ ##
418
+ # Fetches all of the applications configured for a user
419
+ #
420
+ # ==== Return
421
+ # * [Hash] contains the results of the inquiry with a list of applications for the authenticated user, refer to the application method for details
422
+ def applications
423
+ results = @tropo_client.get("applications")
424
+ result_with_ids = []
425
+ results.each do |app|
426
+ result_with_ids << app.merge!({ :application_id => get_element(app.href) })
427
+ end
428
+ result_with_ids
429
+ end
430
+
431
+ ##
432
+ # Fetches the application(s) with the associated addresses in the hash
433
+ #
434
+ # ==== Parameters
435
+ # * [optional, String] application_id will return a single application with addresses if present
436
+ #
437
+ # ==== Return
438
+ # * [Hash] contains the results of the inquiry with a list of applications for the authenticated user, refer to the application method for details
439
+ def applications_with_addresses(application_id=nil)
440
+ if application_id
441
+ associate_addresses_to_application(application(application_id))
442
+ else
443
+ apps = []
444
+ applications.each do |app|
445
+ apps << associate_addresses_to_application(app)
446
+ end
447
+ apps
448
+ end
449
+ end
450
+ alias :application_with_address :applications_with_addresses
451
+
452
+ ##
453
+ # Create a new application
454
+ #
455
+ # ==== Parameters
456
+ # * [required, Hash] params to create the application
457
+ # * [required, String] :name the name to assign to the application
458
+ # * [required, String] :partition this defines whether the application is in staging/development or production
459
+ # * [required, String] :platform (scripting) whether to use scripting or the webapi
460
+ # * [required, String] :messagingUrl or :messaging_url The URL that powers the SMS/messages sessions for your application
461
+ # * [optional, String] :voiceUrl or :voice_url the URL that powers voices calls for your application
462
+ #
463
+ # ==== Return
464
+ # * [Hash] returns the href of the application created and the application_id of the application created
465
+ def create_application(params={})
466
+ merged_params = DEFAULT_OPTIONS.merge(params)
467
+ validate_application_params(merged_params)
468
+ result = @tropo_client.post("applications", params)
469
+ result[:application_id] = get_element(result.href)
470
+ result
471
+ end
472
+
473
+ ##
474
+ # Deletes an application
475
+ #
476
+ # ==== Parameters
477
+ # * [required, String] application_id to be deleted
478
+ #
479
+ # ==== Return
480
+ # * [Hash] not sure since it does 204 now, need to check with Cervantes, et al
481
+ def delete_application(application_id)
482
+ @tropo_client.delete("applications/#{application_id.to_s}")
483
+ end
484
+
485
+ ##
486
+ # Deletes a address from a specific application
487
+ #
488
+ # ==== Parameters
489
+ # * [String] application_id that the address is associated to
490
+ # * [String] address_id for the address
491
+ def delete_address(application_id, address_id)
492
+ address_to_delete = address(application_id, address_id)
493
+
494
+ @tropo_client.delete("applications/#{application_id.to_s}/addresses/#{address_to_delete['type']}/#{address_id.to_s}")
495
+ end
496
+
497
+ ##
498
+ # Provides a list of available exchanges to obtain Numbers from
499
+ #
500
+ # ==== Return
501
+ # * [Array] the list of available exchanges
502
+ def exchanges
503
+ @tropo_client.get("exchanges")
504
+ end
505
+
506
+ ##
507
+ # Used to move a address between one application and another
508
+ #
509
+ # ==== Parameters
510
+ # * [Hash] params contains a hash of the applications and address to move
511
+ # * [required, String] :from
512
+ # * [required, String] :to
513
+ # * [required, String] :address
514
+ def move_address(params={})
515
+ validate_params(params, %w(from to address))
516
+
517
+ begin
518
+ address_to_move = address(params[:from], params[:address])
519
+ delete_address(params[:from], params[:address])
520
+ @tropo_client.post("applications/#{params[:to]}/addresses/#{address_to_move['type']}/#{params[:address]}")
521
+ rescue
522
+ raise RuntimeError, 'Unable to move the address'
523
+ end
524
+ end
525
+
526
+ ##
527
+ # Get a specific address for an application
528
+ #
529
+ # ==== Parameters
530
+ # * [required, String] application_id to obtain the address for
531
+ # * [required, String] address_id of the address to obtain the details for
532
+ #
533
+ # ==== Return
534
+ # * [Hash] the details of the address
535
+ # * [String] :href the REST address for the application
536
+ # * [String] :name the name of the application
537
+ # * [String] :voiceUrl the URL that powers voices calls for your application
538
+ # * [String] :messagingUrl The URL that powers the SMS/messages sessions for your application
539
+ # * [String] :partition this defines whether the application is in staging/development or production
540
+ # * [String] :type this defines the type of address. The possibles types are number (phone numbers), pin (reserved), token, aim, jabber, msn, yahoo, gtalk & skype
541
+ # * [String] :prefix this defines the country code and area code for phone numbers
542
+ # * [String] :number the phone number assigned to the application
543
+ # * [String] :city the city associated with the assigned phone number
544
+ # * [String] :state the state associated with the assigned phone number
545
+ # * [String] :channel idenifites the type of channel, maybe 'voice' or 'messaging'
546
+ # * [String] :username the messaging/IM account's username
547
+ # * [String] :password the messaging/IM account's password
548
+ # * [String] :token alphanumeric string that identifies your Tropo application, used with the Session API
549
+ def address(application_id, address_id)
550
+ addresses(application_id).each { |address| return address if address['number'] == address_id ||
551
+ address['username'] == address_id ||
552
+ address['pin'] == address_id ||
553
+ address['token'] == address_id }
554
+ raise RuntimeError, 'Address not found with that application.'
555
+ end
556
+
557
+ ##
558
+ # Get all of the configured addresses for an application
559
+ #
560
+ # ==== Parameters
561
+ # * [required, String] application_id to fetch the addresses for
562
+ #
563
+ # ==== Return
564
+ # * [Hash] all of the addresses configured for the application
565
+ def addresses(application_id)
566
+ @tropo_client.get("applications/#{application_id.to_s}/addresses")
567
+ end
568
+
569
+ ##
570
+ # Updated an existing application
571
+ #
572
+ # ==== Parameters
573
+ # * [required, String] the application id to update
574
+ # * [required, Hash] params the parameters used to create the application
575
+ # * [optional, String] :name the name of the application
576
+ # * [optional, String] :voiceUrl the URL that powers voices calls for your application
577
+ # * [optional, String] :messagingUrl The URL that powers the SMS/messages sessions for your application
578
+ # * [optional, String] :partition whether to create in staging or production
579
+ # * [optional, String] :platform whehter to use scripting or the webapi
580
+ #
581
+ # ==== Return
582
+ # * [Hash] returns the href of the application created
583
+ def update_application(application_id, params={})
584
+ @tropo_client.put("applications/#{application_id.to_s}", params )
585
+ end
586
+
587
+ ##
588
+ # Fetch all invitations, or invitations by user
589
+ #
590
+ # @overload def invitations()
591
+ # @overload def user_inivitations(user_id)
592
+ # ==== Parameters
593
+ # * [optional, String] the user_id to fetch the invitations for, if not present, will fetch all invitations
594
+ #
595
+ # ==== Return
596
+ # * [Hash] returns a list of the invitations
597
+ def invitations(user_id = nil)
598
+ if user_id
599
+ @tropo_client.get("users/#{user_id}/invitations")
600
+ else
601
+ @tropo_client.get("invitations")
602
+ end
603
+ end
604
+ alias :user_invitations :invitations
605
+
606
+ ##
607
+ # Fetch an invitation
608
+ #
609
+ # @overload def invitation(invitation_id)
610
+ # ==== Parameters
611
+ # * [required, String] the invitation id to fetch
612
+ # @overload def user_invitation(user_id, invitation_id)
613
+ # ==== Parameters
614
+ # * [required, String] the invitation id to fetch
615
+ # * [optional, String] the user id to fetch the invitation for
616
+ #
617
+ # ==== Return
618
+ # * [Hash] return an invitation
619
+ def invitation(invitation_id, user_id = nil)
620
+ path = user_id.nil? ? "invitations/#{invitation_id}" : "users/#{user_id}/invitations/#{invitation_id}"
621
+
622
+ @tropo_client.get(path)
623
+ end
624
+
625
+ alias :user_invitation :invitation
626
+
627
+ ##
628
+ # Fetch an invitation
629
+ #
630
+ # @overload def delete_invitation(invitation_id)
631
+ # ==== Parameters
632
+ # * [required, String] the invitation id to delete
633
+ # @overload def delete_user_invitation(invitation_id, user_id)
634
+ # ==== Parameters
635
+ # * [required, String] the invitation id to delete
636
+ # * [required, String] the user id to delete
637
+ #
638
+ # ==== Return
639
+ # * [Hash] return an invitation
640
+ def delete_invitation(invitation_id, user_id = nil)
641
+ path = user_id.nil? ? "invitations/#{invitation_id}" : "users/#{user_id}/invitations/#{invitation_id}"
642
+
643
+ @tropo_client.delete(path)
644
+ end
645
+
646
+ alias :delete_user_invitation :delete_invitation
647
+
648
+ ##
649
+ # Create an invitation
650
+ #
651
+ # @overload def create_invitation(options)
652
+ # ==== Parameters
653
+ # * [required, Hash] params the parameters used to create the application
654
+ # * [optional, String] :code the invitation code (defaults to a random alphanum string of length 6 if not specified on POST)
655
+ # * [optional, String] :count the number of accounts that may signup with this code (decrements on each signup)
656
+ # * [optional, String] :credit starting account balance for users who signup with this code (replaces the default for the brand)
657
+ # * [optional, String] :partition whether to create in staging or production
658
+ # * [optional, String] :owner URI identifying the user to which this invite code belongs (optional - null implies this is a "global" code)
659
+ # @overload def create_user_invitation(user_id, options)
660
+ # ==== Parameters
661
+ # * [requried, String] user_id to create the invitation for
662
+ # * [required, Hash] params the parameters used to create the application
663
+ # * [optional, String] :code the invitation code (defaults to a random alphanum string of length 6 if not specified on POST)
664
+ # * [optional, String] :count the number of accounts that may signup with this code (decrements on each signup)
665
+ # * [optional, String] :credit starting account balance for users who signup with this code (replaces the default for the brand)
666
+ # * [optional, String] :partition whether to create in staging or production
667
+ # * [optional, String] :owner URI identifying the user to which this invite code belongs (optional - null implies this is a "global" code)
668
+ #
669
+ # ==== Return
670
+ # * [Hash] returns the href of the invitation created
671
+ def create_invitation(*args)
672
+ if args.length == 1
673
+ @tropo_client.post("invitations", args[0])
674
+ elsif args.length == 2
675
+ @tropo_client.post("users/#{args[0]}/invitations", args[1])
676
+ end
677
+ end
678
+ alias :create_user_invitation :create_invitation
679
+
680
+ ##
681
+ # Update an invitation
682
+ #
683
+ # @overload def update_invitation(invitation_id, options)
684
+ # ==== Parameters
685
+ # * [required, String] id of the invitation to udpate (code)
686
+ # * [required, Hash] params the parameters used to update the application
687
+ # * [optional, String] :count the number of accounts that may signup with this code (decrements on each signup)
688
+ # * [optional, String] :credit starting account balance for users who signup with this code (replaces the default for the brand)
689
+ # * [optional, String] :partition whether to create in staging or production
690
+ # * [optional, String] :owner URI identifying the user to which this invite code belongs (optional - null implies this is a "global" code)
691
+ # @overload def updated_user_invitation(invitation_id, user_id, options)
692
+ # ==== Parameters
693
+ # * [required, String] id of the invitation to udpate (code)
694
+ # * [required, String] id of the user to update the invitation code for
695
+ # * [required, Hash] params the parameters used to update the application
696
+ # * [optional, String] :count the number of accounts that may signup with this code (decrements on each signup)
697
+ # * [optional, String] :credit starting account balance for users who signup with this code (replaces the default for the brand)
698
+ # * [optional, String] :partition whether to create in staging or production
699
+ # * [optional, String] :owner URI identifying the user to which this invite code belongs (optional - null implies this is a "global" code)
700
+ #
701
+ # ==== Return
702
+ # * [Hash] returns the href of the invitation created
703
+ def update_invitation(*args)
704
+ if args.length == 2
705
+ @tropo_client.put("invitations/#{args[0]}", args[1])
706
+ elsif args.length == 3
707
+ @tropo_client.put("users/#{args[1]}/invitations/#{args[0]}", args[2])
708
+ end
709
+ end
710
+ alias :update_user_invitation :update_invitation
711
+
712
+ ##
713
+ # Get the available partitions available
714
+ #
715
+ # ==== Return
716
+ # * [Array]
717
+ # an array of hashes containing the partitions available
718
+ def partitions
719
+ @tropo_client.get("partitions")
720
+ end
721
+
722
+ ##
723
+ # Get the available platforms available under a certain partition
724
+ #
725
+ # ==== Parameters
726
+ #
727
+ # ==== Return
728
+ # * [Array]
729
+ # an array of hashes containing the platforms available
730
+ def platforms(partition)
731
+ @tropo_client.get("partitions/#{partition}/platforms")
732
+ end
733
+
734
+ ##
735
+ # Get the whitelist of the numbers on a particular users list
736
+ #
737
+ # ==== Parameters
738
+ # * [required, String] user_id of the user you would like to update
739
+ #
740
+ # ==== Return
741
+ # * [Hash]
742
+ # the href and value containing the number on the whitelist
743
+ def whitelist(user_id = nil)
744
+ resource = user_id.nil? ? "users/partitions/production/platforms/sms/whitelist" : "users/#{user_id}/partitions/production/platforms/sms/whitelist"
745
+
746
+ @tropo_client.get(resource)
747
+ end
748
+
749
+ ##
750
+ # Add to a whitelist for a particular user
751
+ #
752
+ # ==== Parameters
753
+ # * [Hash] params contains a hash of the user_id and value to add
754
+ # * [optional, String] :user_id if present the user_id to add to, if not it will add to the user logged in as
755
+ # * [required, String] :value the value to add to the whitelist
756
+ #
757
+ # ==== Return
758
+ # * [Hash]
759
+ # the href
760
+ def add_whitelist(params={})
761
+ resource = params.has_key?(:user_id) ?
762
+ "users/#{params[:user_id]}/partitions/production/platforms/sms/whitelist" :
763
+ "users/partitions/production/platforms/sms/whitelist"
764
+ @tropo_client.post(resource, {:value => params[:value]})
765
+ end
766
+
767
+ ##
768
+ # Delete from a whitelist for a particular user
769
+ #
770
+ # * [Hash] params contains a hash of the user_id and value to delete
771
+ # * [optional, String] :user_id if present the user_id to delete from, if not it will add to the user logged in as
772
+ # * [required, String] :value the value to delete from the whitelist
773
+ #
774
+ # ==== Return
775
+ # * [Hash]
776
+ # the href
777
+ def delete_whitelist(params={})
778
+ resource = params.has_key?(:user_id) ? "users/#{params[:user_id]}/partitions/production/platforms/sms/whitelist/" : "users/partitions/production/platforms/sms/whitelist/"
779
+
780
+ @tropo_client.delete("#{resource}#{params[:value]}")
781
+ end
782
+
783
+ private
784
+
785
+ ##
786
+ # Returns the current method name
787
+ #
788
+ # ==== Return
789
+ # * [String] current method name
790
+ def current_method_name
791
+ caller[0] =~ /`([^']*)'/ and $1
792
+ end
793
+
794
+ ##
795
+ # Adds the IDs to an Array of Hashes if no ID is present
796
+ #
797
+ # ==== Parameters
798
+ # * [required, Array] array of hashes to add IDs to
799
+ #
800
+ # ==== Return
801
+ # * [Array]
802
+ # the array of hashes with ID added
803
+ def add_ids(array)
804
+ array.each do |element|
805
+ element[:id].nil? and element[:id] = get_element(element.href)
806
+ end
807
+ array
808
+ end
809
+
810
+ ##
811
+ # Parses the URL and returns the last element
812
+ #
813
+ # ==== Parameters
814
+ # * [required, String] the URL to parse for the application ID
815
+ #
816
+ # ==== Return
817
+ # * [String] the application id parsed from the URL
818
+ def get_element(url)
819
+ url.split('/').last
820
+ end
821
+
822
+ ##
823
+ # Associates the addresses to an application
824
+ #
825
+ # ==== Parameters
826
+ # * [Object] application object to associate the address to
827
+ #
828
+ # ==== Return
829
+ # * [Object] returns the application object with the associated addresses embedded
830
+ def associate_addresses_to_application(app)
831
+ add = addresses(app.application_id)
832
+ app.merge!({ :addresses => add })
833
+ end
834
+
835
+ ##
836
+ # Creates the appropriate request for the temporary Evolution account API
837
+ #
838
+ # ==== Parameters
839
+ # * [required, String] path: URI path
840
+ # ==== Return
841
+ # * [Hash] the result of the request
842
+ def temp_request(path)
843
+ #base_uri = 'http://evolution.voxeo.com/api/account'
844
+ base_uri = 'http://web141.supernonstop.com/api/account'
845
+ uri = URI.parse(base_uri + path)
846
+ http = Net::HTTP.new(uri.host, uri.port)
847
+
848
+ request = Net::HTTP::Delete.new(uri)
849
+ request.initialize_http_header({'Content-Type' => 'application/json'})
850
+
851
+ response = http.request(request)
852
+ raise TropoError, "#{response.code} - #{response.message}" unless response.code == '200'
853
+
854
+ result = ActiveSupport::JSON.decode response.body
855
+ if result.instance_of? Array
856
+ hashie_array(result)
857
+ else
858
+ Hashie::Mash.new(result)
859
+ end
860
+ end
861
+
862
+
863
+ ##
864
+ # Used to validate required params in either underscore or camelCase formats
865
+ #
866
+ # ==== Parameters
867
+ # * [required, Hash] params to be checked
868
+ # * [required, Array] requirements of which fields much be present
869
+ #
870
+ # ==== Returns
871
+ # * ArgumentError
872
+ # if a param is not present that is required
873
+ def validate_params(params, requirements)
874
+ requirements.each do |requirement|
875
+ if params[requirement.to_sym].nil? && params[requirement.to_s.camelize(:lower).to_sym].nil? && params[requirement].nil? && params[requirement.to_s.camelize(:lower)].nil?
876
+ raise ArgumentError, ":#{requirement} is a required parameter"
877
+ break
878
+ end
879
+ end
880
+ end
881
+
882
+ ##
883
+ # Validates that we have all of the appropriate params when creating an application
884
+ #
885
+ # ==== Parameters
886
+ # * [Hash] params to create the application
887
+ # * [required, String] :name the name to assign to the application
888
+ # * [required, String] :partition whether to create in staging or production
889
+ # * [required, String] :platform whehter to use scripting or the webapi
890
+ # * [String] :messagingUrl the Url to use for handiling messaging requests
891
+ # * [String] :voiceUrl the Url to use for handling voice requests
892
+ #
893
+ # ==== Return
894
+ # * nil
895
+ def validate_application_params(params={})
896
+ # Make sure all of the arguments are present
897
+ raise ArgumentError, ':name is a required parameter' unless params[:name] || params['name']
898
+
899
+ # Make sure the arguments have valid values
900
+ raise ArgumentError, ":platform must be #{VALID_PLATFORMS.map{|platform| "\'#{platform}\'"}.join(' or ')}" unless VALID_PLATFORMS.include?(params[:platform]) or VALID_PLATFORMS.include?(params["platform"])
901
+ raise ArgumentError, ":partition must be #{VALID_PARTITIONS.map{|partition| "\'#{partition}\'"}.join(' or ')}" unless VALID_PARTITIONS.include?(params[:partition]) or VALID_PARTITIONS.include?(params["partition"])
902
+ end
903
+
904
+ ##
905
+ # Validates a create address request parameters
906
+ #
907
+ # ==== Parameters
908
+ # * [required, Hash] required parameters values
909
+ #
910
+ # ==== Return
911
+ # * nil if successful validation
912
+ # * ArgumentError is a required parameter is missing
913
+ #
914
+ def validate_address_parameters(params={})
915
+ raise ArgumentError, ":type is a required parameter" unless params[:type] || params['type']
916
+
917
+ case params[:type].downcase
918
+ when 'number'
919
+ raise ArgumentError, ':prefix required to add a number address' unless params[:prefix] || params[:number] || params['prefix'] || params['number']
920
+ when 'aim', 'msn', 'yahoo', 'gtalk'
921
+ raise ArgumentError, ':username is a required parameter' unless params[:username] || params['username']
922
+ raise ArgumentError, ':password is a required parameter' unless params[:password] || params ['password']
923
+ when 'jabber'
924
+ raise ArgumentError, ':username is a required parameter' unless params[:username] || params['username']
925
+ when 'token'
926
+ raise ArgumentError, ':channel is a required parameter' unless params[:channel] || params['channel']
927
+ raise ArgumentError, ':channel must be voice or messaging' unless params[:channel] == 'voice' || params[:channel] == 'messaging' || params['channel'] == 'voice' || params['channel'] == 'messaging'
928
+ end
929
+ end
930
+ end