twizo 0.1.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.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +126 -0
  6. data/Rakefile +2 -0
  7. data/bin/console +14 -0
  8. data/bin/setup +8 -0
  9. data/examples/backup_codes.rb +31 -0
  10. data/examples/backup_codes_check.rb +29 -0
  11. data/examples/backup_codes_delete.rb +31 -0
  12. data/examples/backup_codes_update.rb +31 -0
  13. data/examples/backup_codes_verify.rb +34 -0
  14. data/examples/balance.rb +18 -0
  15. data/examples/examples_init.rb +43 -0
  16. data/examples/number_lookup.rb +32 -0
  17. data/examples/number_lookup_results.rb +28 -0
  18. data/examples/number_lookup_status.rb +29 -0
  19. data/examples/sms.rb +32 -0
  20. data/examples/sms_advanced.rb +33 -0
  21. data/examples/sms_concat.rb +32 -0
  22. data/examples/sms_multiple.rb +35 -0
  23. data/examples/sms_results.rb +28 -0
  24. data/examples/sms_status.rb +29 -0
  25. data/examples/sms_validation_errors.rb +28 -0
  26. data/examples/verification.rb +31 -0
  27. data/examples/verification_status.rb +29 -0
  28. data/examples/verification_verify_token.rb +39 -0
  29. data/examples/widget.rb +31 -0
  30. data/examples/widget_status.rb +34 -0
  31. data/lib/twizo.rb +190 -0
  32. data/lib/twizo/client.rb +45 -0
  33. data/lib/twizo/client/net_http_client.rb +48 -0
  34. data/lib/twizo/entity.rb +114 -0
  35. data/lib/twizo/modules/backup_codes.rb +100 -0
  36. data/lib/twizo/modules/balance.rb +40 -0
  37. data/lib/twizo/modules/number_lookup.rb +81 -0
  38. data/lib/twizo/modules/params/backup_codes_params.rb +19 -0
  39. data/lib/twizo/modules/params/number_lookup_params.rb +23 -0
  40. data/lib/twizo/modules/params/params.rb +37 -0
  41. data/lib/twizo/modules/params/sms_params.rb +34 -0
  42. data/lib/twizo/modules/params/verification_params.rb +30 -0
  43. data/lib/twizo/modules/params/widget_params.rb +29 -0
  44. data/lib/twizo/modules/sms.rb +130 -0
  45. data/lib/twizo/modules/verification.rb +73 -0
  46. data/lib/twizo/modules/widget.rb +59 -0
  47. data/lib/twizo/result.rb +66 -0
  48. data/lib/twizo/status_codes.rb +32 -0
  49. data/lib/twizo/twizo_error.rb +26 -0
  50. data/lib/twizo/version.rb +3 -0
  51. data/test/test_all.rb +19 -0
  52. data/test/test_backup_codes.rb +120 -0
  53. data/test/test_balance.rb +51 -0
  54. data/test/test_init.rb +41 -0
  55. data/test/test_number_lookup.rb +215 -0
  56. data/test/test_sms.rb +270 -0
  57. data/test/test_verification.rb +106 -0
  58. data/test/test_widget.rb +107 -0
  59. data/twizo.gemspec +34 -0
  60. metadata +154 -0
@@ -0,0 +1,40 @@
1
+ =begin
2
+
3
+ This file is part of the Twizo php api
4
+
5
+ (c) Twizo <info@twizo.com>
6
+
7
+ For the full copyright and license information, please view the LICENSE
8
+ File that was distributed with this source code.
9
+
10
+ =end
11
+
12
+ module Twizo
13
+
14
+ module Balance
15
+
16
+ #
17
+ # Send message to the server and return response
18
+ #
19
+ # @return [Object]
20
+ #
21
+ def send
22
+ response = send_api_call(Entity::ACTION_RETRIEVE, location)
23
+
24
+ raise response if response.kind_of?(TwizoError)
25
+
26
+ response_to_array(response)
27
+ end
28
+
29
+ private
30
+
31
+ #
32
+ # @return [String]
33
+ #
34
+ def location
35
+ 'wallet/getbalance'
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,81 @@
1
+ require_relative 'params/number_lookup_params'
2
+
3
+ =begin
4
+
5
+ This file is part of the Twizo php api
6
+
7
+ (c) Twizo <info@twizo.com>
8
+
9
+ For the full copyright and license information, please view the LICENSE
10
+ File that was distributed with this source code.
11
+
12
+ =end
13
+
14
+ module Twizo
15
+
16
+ module NumberLookup
17
+
18
+ #
19
+ # Getter for params
20
+ #
21
+ attr_reader :params
22
+
23
+ #
24
+ # Bitmasks of which type of result type to send
25
+ #
26
+ RESULT_TYPE_CALLBACK = 1
27
+ RESULT_TYPE_POLL = 2
28
+
29
+ #
30
+ # @param [Array] numbers
31
+ #
32
+ def set(numbers)
33
+ @params = NumberLookupParams.new
34
+ @params.numbers = numbers
35
+ end
36
+
37
+ #
38
+ # Send message to the server and return response
39
+ #
40
+ # @return [Object]
41
+ #
42
+ def send
43
+ post_params = @params.to_json
44
+
45
+ response = send_api_call(Entity::ACTION_CREATE, location, post_params)
46
+
47
+ raise response if response.kind_of?(TwizoError)
48
+
49
+ response_to_array(response, 'items')
50
+ end
51
+
52
+ #
53
+ # @return [Object]
54
+ #
55
+ def poll
56
+ response = send_api_call(Entity::ACTION_RETRIEVE, 'numberlookup/poll')
57
+
58
+ raise response if response.kind_of?(TwizoError)
59
+
60
+ batch_id = response['batchId'] unless response['batchId'].nil?
61
+
62
+ unless batch_id.nil?
63
+ send_api_call(Entity::ACTION_REMOVE, 'numberlookup/poll/' + batch_id)
64
+ end
65
+
66
+ response_to_array(response, 'messages')
67
+ end
68
+
69
+ private
70
+
71
+ #
72
+ # @return [String]
73
+ #
74
+ def location
75
+ 'numberlookup/submit'
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
@@ -0,0 +1,19 @@
1
+ require_relative 'params'
2
+
3
+ module Twizo
4
+
5
+ class BackupCodesParams < Params
6
+
7
+ attr_accessor :identifier
8
+
9
+ def to_json
10
+ json = {
11
+ :identifier => identifier
12
+ }
13
+
14
+ json.to_json
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'params'
2
+
3
+ module Twizo
4
+
5
+ class NumberLookupParams < Params
6
+
7
+ attr_accessor :numbers, :tag, :validity, :result_type, :callback_url
8
+
9
+ def to_json
10
+ json = {
11
+ :numbers => format_to_array(numbers),
12
+ :tag => tag,
13
+ :validity => validity,
14
+ :resultType => result_type,
15
+ :callbackUrl => callback_url,
16
+ }
17
+
18
+ json.to_json
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+
3
+ module Twizo
4
+
5
+ class Params
6
+
7
+ #
8
+ # @param [String|Array] numbers
9
+ #
10
+ # @return [Array]
11
+ #
12
+ def format_to_array(attributes)
13
+ attributes.kind_of?(Array) ? attributes_array = attributes : attributes_array = [attributes]
14
+
15
+ attributes.nil? ? attributes : format_input(attributes_array)
16
+ end
17
+
18
+ #
19
+ # @param [Array] numbers
20
+ #
21
+ # @return [Array]
22
+ #
23
+ def format_input(attributes)
24
+ attributes.map do |attribute|
25
+ attribute.gsub!(/[()+o ]/, '()+ ' => '', 'o' => '0')
26
+ attribute.gsub!(/^0+/, '')
27
+ unless attribute.scan(/\D/).empty?
28
+ raise TwizoError.new(422, 'The number(s) may not contain any characters.')
29
+ end
30
+ end
31
+
32
+ attributes
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'params'
2
+
3
+ module Twizo
4
+
5
+ class SmsParams < Params
6
+
7
+ attr_accessor :recipients, :body, :sender, :sender_ton, :sender_npi, :pid, :scheduled_delivery, :tag, :validity, :result_type, :callback_url, :dcs, :udh
8
+
9
+ def to_json(send_advanced = nil)
10
+ json = {
11
+ :recipients => format_to_array(recipients),
12
+ :body => body,
13
+ :sender => sender,
14
+ :senderTon => sender_ton,
15
+ :senderNpi => sender_npi,
16
+ :pid => pid,
17
+ :scheduledDelivery => scheduled_delivery,
18
+ :tag => tag,
19
+ :validity => validity,
20
+ :resultType => result_type,
21
+ :callbackUrl => callback_url,
22
+ }
23
+
24
+ if send_advanced
25
+ json[:dcs] = dcs
26
+ json[:udh] = udh
27
+ end
28
+
29
+ json.to_json
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'params'
2
+
3
+ module Twizo
4
+
5
+ class VerificationParams < Params
6
+
7
+ attr_accessor :recipient, :type, :token_length, :token_type, :body_template, :session_id, :sender, :sender_ton, :sender_npi, :tag, :validity, :dcs
8
+
9
+ def to_json
10
+ json = {
11
+ :recipient => recipient,
12
+ :type => type,
13
+ :tokenLength => token_length,
14
+ :tokenType => token_type,
15
+ :bodyTemplate => body_template,
16
+ :sessionId => session_id,
17
+ :sender => sender,
18
+ :senderTon => sender_ton,
19
+ :senderNpi => sender_npi,
20
+ :tag => tag,
21
+ :validity => validity,
22
+ :dcs => dcs
23
+ }
24
+
25
+ json.to_json
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'params'
2
+
3
+ module Twizo
4
+
5
+ class WidgetParams < Params
6
+
7
+ attr_accessor :allowed_types, :recipient, :backup_code_identifier, :token_length, :token_type, :body_template, :sender, :sender_ton, :sender_npi, :tag, :dcs
8
+
9
+ def to_json
10
+ json = {
11
+ :allowedTypes => allowed_types,
12
+ :recipient => recipient,
13
+ :backupCodeIdentifier => backup_code_identifier,
14
+ :tokenLength => token_length,
15
+ :tokenType => token_type,
16
+ :bodyTemplate => body_template,
17
+ :sender => sender,
18
+ :senderTon => sender_ton,
19
+ :senderNpi => sender_npi,
20
+ :tag => tag,
21
+ :dcs => dcs
22
+ }
23
+
24
+ json.to_json
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,130 @@
1
+ require_relative 'params/sms_params'
2
+
3
+ =begin
4
+
5
+ This file is part of the Twizo php api
6
+
7
+ (c) Twizo <info@twizo.com>
8
+
9
+ For the full copyright and license information, please view the LICENSE
10
+ File that was distributed with this source code.
11
+
12
+ =end
13
+
14
+ module Twizo
15
+
16
+ module Sms
17
+
18
+ #
19
+ # Getter for params
20
+ #
21
+ attr_reader :params
22
+
23
+ #
24
+ # Bitmasks of which type of result type to send
25
+ #
26
+ RESULT_TYPE_CALLBACK = 1
27
+ RESULT_TYPE_POLL = 2
28
+
29
+ #
30
+ # @param [String] body
31
+ # @param [Array] recipients
32
+ # @param [String] sender
33
+ #
34
+ def set(body, recipients, sender)
35
+ @params = SmsParams.new
36
+ @params.body = body
37
+ @params.recipients = recipients
38
+ @params.sender = sender
39
+ end
40
+
41
+ #
42
+ # Send message to the server and return response
43
+ #
44
+ # @return [Object]
45
+ #
46
+ def send_simple
47
+ post_params = @params.to_json
48
+
49
+ response = send_api_call(Entity::ACTION_CREATE, 'sms/submitsimple', post_params)
50
+
51
+ raise response if response.kind_of?(TwizoError)
52
+
53
+ response_to_array(response, 'items')
54
+ end
55
+
56
+ #
57
+ # Send message to the server and return response
58
+ #
59
+ # @return [Object]
60
+ #
61
+ def send
62
+ # set @dcs to 0 if dcs is not set
63
+ @params.dcs ||= 0
64
+
65
+ @params.body = bin_to_hex(@params.body).upcase if is_binary?
66
+
67
+ post_params = @params.to_json send_advanced: true
68
+
69
+ response = send_api_call(Entity::ACTION_CREATE, location, post_params)
70
+
71
+ raise response if response.kind_of?(TwizoError)
72
+
73
+ response_to_array(response, 'items')
74
+ end
75
+
76
+ #
77
+ # @return [Object]
78
+ #
79
+ def poll
80
+
81
+ response = send_api_call(Entity::ACTION_RETRIEVE, 'sms/poll')
82
+
83
+ raise response if response.kind_of?(TwizoError)
84
+
85
+ @batch_id = response['batchId'] unless response['batchId'].nil?
86
+
87
+ unless @batch_id == ''
88
+ send_api_call(Entity::ACTION_REMOVE, 'sms/poll/' + @batch_id)
89
+ end
90
+
91
+ response_to_array(response, 'messages')
92
+ end
93
+
94
+ private
95
+
96
+ #
97
+ # @return [String]
98
+ #
99
+ def location
100
+ 'sms/submit'
101
+ end
102
+
103
+ #
104
+ # @return [Boolean]
105
+ #
106
+ def is_binary?
107
+ binary = false
108
+
109
+ if (@params.dcs & 200) === 0
110
+ binary = ((@params.dcs & 4) > 0)
111
+ elsif (@params.dcs & 4) > 0
112
+ binary = ((@params.dcs & 4) > 0)
113
+ end
114
+
115
+ binary
116
+ end
117
+
118
+ #
119
+ # @param [Binary] binary_string
120
+ #
121
+ # @return [Hex]
122
+ #
123
+ def bin_to_hex(binary_string)
124
+ binary_string.unpack('H*').first
125
+ end
126
+
127
+ end
128
+
129
+ end
130
+
@@ -0,0 +1,73 @@
1
+ require_relative 'params/verification_params'
2
+
3
+ =begin
4
+
5
+ This file is part of the Twizo php api
6
+
7
+ (c) Twizo <info@twizo.com>
8
+
9
+ For the full copyright and license information, please view the LICENSE
10
+ File that was distributed with this source code.
11
+
12
+ =end
13
+
14
+ module Twizo
15
+
16
+ module Verification
17
+
18
+ #
19
+ # Getter for params
20
+ #
21
+ attr_reader :params
22
+
23
+ #
24
+ # @param [String] recipient
25
+ #
26
+ def set(recipient)
27
+ @params = VerificationParams.new
28
+ @params.recipient = recipient
29
+
30
+ # set default type
31
+ @params.type ||= 'sms'
32
+ end
33
+
34
+ #
35
+ # Send message to the server and return response
36
+ #
37
+ # @return [Object]
38
+ #
39
+ def send
40
+ post_params = @params.to_json
41
+
42
+ response = send_api_call(Entity::ACTION_CREATE, location, post_params)
43
+
44
+ raise response if response.kind_of?(TwizoError)
45
+
46
+ response_to_array(response)
47
+ end
48
+
49
+ #
50
+ # @param [String] token
51
+ #
52
+ # @return [Object]
53
+ #
54
+ def verify(message_id, token)
55
+ response = send_api_call(Entity::ACTION_RETRIEVE, "#{location}/#{message_id}?token=#{token}")
56
+
57
+ raise response if response.kind_of?(TwizoError)
58
+
59
+ response_to_array(response)
60
+ end
61
+
62
+ private
63
+
64
+ #
65
+ # @return [String]
66
+ #
67
+ def location
68
+ 'verification/submit'
69
+ end
70
+
71
+ end
72
+
73
+ end