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,59 @@
1
+ require_relative 'params/widget_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 Widget
17
+
18
+ #
19
+ # Getter for params
20
+ #
21
+ attr_reader :params
22
+
23
+ #
24
+ # @param [String] recipient
25
+ #
26
+ def set(recipient)
27
+ @params = WidgetParams.new
28
+ @params.recipient = recipient
29
+
30
+ # set default allowed_types
31
+ @params.allowed_types ||= %w(sms call)
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
+ response = send_api_call(Entity::ACTION_CREATE, location, post_params)
42
+
43
+ raise response if response.kind_of?(TwizoError)
44
+
45
+ response_to_array(response)
46
+ end
47
+
48
+ private
49
+
50
+ #
51
+ # @return [String]
52
+ #
53
+ def location
54
+ 'widget/session'
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,66 @@
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
+ class Result
15
+
16
+ attr_reader :result
17
+
18
+ #
19
+ # Constructor
20
+ #
21
+ # @param [Array] result
22
+ #
23
+ def initialize(result)
24
+ set_fields(result)
25
+ end
26
+
27
+ #
28
+ # @param [Array] fields
29
+ #
30
+ def set_fields(fields)
31
+ fields.each do |name, value|
32
+ add_attribute_accessor(name, value)
33
+ end
34
+ end
35
+
36
+ #
37
+ # Getter and Setter fields are dynamically created
38
+ #
39
+ # @param [String] attr_name
40
+ # @param [Object] attr_value
41
+ #
42
+ def add_attribute_accessor(attr_name, attr_value)
43
+ self.class.send(:define_method, "#{attr_name}=".to_sym) do |value|
44
+ instance_variable_set('@' + attr_name.to_s, value)
45
+ end
46
+
47
+ self.class.send(:define_method, attr_name.to_sym) do
48
+ instance_variable_get('@' + attr_name.to_s)
49
+ end
50
+
51
+ self.send("#{attr_name}=".to_sym, attr_value)
52
+ end
53
+
54
+ #
55
+ # add an item to parent result
56
+ #
57
+ # @param [Object] item
58
+ #
59
+ def add_result(item)
60
+ @result ||= []
61
+ @result << item
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,32 @@
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 StatusCodes
15
+
16
+ #
17
+ # Status codes for number lookup and sms
18
+ #
19
+ NO_STATUS = 0
20
+ DELIVERED = 1
21
+ REJECTED = 2
22
+ EXPIRED = 3
23
+ ENROUTE = 4
24
+ BUFFERED = 5
25
+ ACCEPTED = 6
26
+ UNDELIVERED = 7
27
+ DELETED = 8
28
+ UNKNOWN = 9
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,26 @@
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
+ class TwizoError < StandardError
15
+
16
+ attr_reader :body
17
+
18
+ def initialize(code, body = nil?)
19
+ super(code)
20
+
21
+ @body = body
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ module Twizo
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,19 @@
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
+ # in the console type: ruby test_all.rb
13
+
14
+ require_relative 'test_number_lookup'
15
+ require_relative 'test_sms'
16
+ require_relative 'test_balance'
17
+ require_relative 'test_backup_codes'
18
+ require_relative 'test_verification'
19
+ require_relative 'test_widget'
@@ -0,0 +1,120 @@
1
+ require_relative 'test_init'
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
+ class TestBackupCodes < TestInit
15
+
16
+ #
17
+ # Initialize Twizo
18
+ #
19
+ def setup
20
+ @twizo = Twizo::Twizo.new(API_KEY, API_HOST)
21
+ end
22
+
23
+ #
24
+ # Test if an error is thrown when an invalid key is provided
25
+ #
26
+ def test_invalid_key
27
+ api_key = 'invalid_key'
28
+
29
+ assert_raise Twizo::TwizoError do
30
+ @twizo = Twizo::Twizo.new(api_key, API_HOST)
31
+ backup_codes = @twizo.create_backup_codes(NUMBER1)
32
+ backup_codes.send
33
+ end
34
+ end
35
+
36
+ #
37
+ # Test creation of backup codes
38
+ #
39
+ def test_create_backup_codes
40
+ test_identifier = rand(1000..9999).to_s
41
+
42
+ backup_codes = @twizo.create_backup_codes(nil)
43
+ backup_codes.params.identifier = test_identifier
44
+
45
+ backup_codes = backup_codes.send
46
+
47
+ assert_equal test_identifier, backup_codes.identifier
48
+ assert_equal 10, backup_codes.amountOfCodesLeft
49
+ assert_equal 10, backup_codes.codes.length
50
+ end
51
+
52
+ #
53
+ # Test verification of backup codes
54
+ #
55
+ def test_verify_backup_codes
56
+ test_identifier = rand(1000..9999).to_s
57
+
58
+ backup_codes = @twizo.create_backup_codes(nil)
59
+ backup_codes.params.identifier = test_identifier
60
+
61
+ backup_code = backup_codes.send
62
+ backup_code = backup_code.codes[0]
63
+
64
+ verification = backup_codes.verify(backup_code)
65
+
66
+ assert_equal test_identifier, verification.identifier
67
+ end
68
+
69
+ #
70
+ # Test retrievement of remaining backup codes
71
+ #
72
+ def test_get_backup_codes
73
+ test_identifier = rand(1000..9999).to_s
74
+
75
+ backup_codes = @twizo.create_backup_codes(nil)
76
+ backup_codes.params.identifier = test_identifier
77
+
78
+ backup_codes.send
79
+
80
+ check = @twizo.get_backup_code(test_identifier)
81
+
82
+ assert_equal test_identifier, check.identifier
83
+ assert_not_nil check.amountOfCodesLeft
84
+ end
85
+
86
+ #
87
+ # Test updating the backup codes
88
+ #
89
+ def test_update_backup_codes
90
+ test_identifier = rand(1000..9999).to_s
91
+
92
+ backup_codes = @twizo.create_backup_codes(nil)
93
+ backup_codes.params.identifier = test_identifier
94
+
95
+ backup_codes = backup_codes.update
96
+
97
+ assert_equal test_identifier, backup_codes.identifier
98
+ assert_equal 10, backup_codes.amountOfCodesLeft
99
+ assert_equal 10, backup_codes.codes.length
100
+ end
101
+
102
+ #
103
+ # Test deletion of backup codes
104
+ #
105
+ def test_delete_backup_codes
106
+ test_identifier = rand(1000..9999).to_s
107
+
108
+ backup_codes = @twizo.create_backup_codes(nil)
109
+ backup_codes.params.identifier = test_identifier
110
+
111
+ backup_codes = backup_codes.delete
112
+
113
+ assert_equal '204', backup_codes.code
114
+ end
115
+
116
+ def teardown
117
+ print "\n#{method_name}"
118
+ end
119
+
120
+ end
@@ -0,0 +1,51 @@
1
+ require_relative 'test_init'
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
+ class TestBalance < TestInit
15
+
16
+ #
17
+ # Initialize Twizo
18
+ #
19
+ def setup
20
+ @twizo = Twizo::Twizo.new(API_KEY, API_HOST)
21
+ end
22
+
23
+ #
24
+ # Test if an error is thrown when an invalid key is provided
25
+ #
26
+ def test_invalid_key
27
+ api_key = 'invalid_key'
28
+
29
+ assert_raise Twizo::TwizoError do
30
+ @twizo = Twizo::Twizo.new(api_key, API_HOST)
31
+ @twizo.get_balance
32
+ end
33
+ end
34
+
35
+ #
36
+ # Test get balance
37
+ #
38
+ def test_get_balance
39
+ balance = @twizo.get_balance
40
+
41
+ assert_not_nil balance.credit
42
+ assert_not_nil balance.currencyCode
43
+ assert_not_nil balance.freeVerifications
44
+ assert_not_nil balance.wallet
45
+ end
46
+
47
+ def teardown
48
+ print "\n#{method_name}"
49
+ end
50
+
51
+ end
@@ -0,0 +1,41 @@
1
+ require 'test/unit'
2
+ require 'twizo'
3
+
4
+ =begin
5
+
6
+ This file is part of the Twizo php api
7
+
8
+ (c) Twizo <info@twizo.com>
9
+
10
+ For the full copyright and license information, please view the LICENSE
11
+ File that was distributed with this source code.
12
+
13
+ =end
14
+
15
+ class TestInit < Test::Unit::TestCase
16
+
17
+ NUMBER1 = '601234567890'
18
+ NUMBER2 = '311234567890'
19
+
20
+ def self.load_config
21
+ filename = 'credentials.rb'
22
+
23
+ unless File.exist?("#{File.dirname(__FILE__)}/#{filename}")
24
+ print 'Enter api key: '
25
+ api_key = gets.chomp
26
+
27
+ print 'Enter api host: '
28
+ api_host = gets.chomp
29
+
30
+ file = File.new("#{File.dirname(__FILE__)}/#{filename}", 'w')
31
+ file.puts("API_KEY = '#{api_key}'")
32
+ file.puts("API_HOST = '#{api_host}'")
33
+ file.close
34
+ end
35
+
36
+ require_relative filename
37
+ end
38
+
39
+ end
40
+
41
+ TestInit.load_config
@@ -0,0 +1,215 @@
1
+ require_relative 'test_init'
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
+ class TestNumberLookup < TestInit
15
+
16
+ #
17
+ # Initialize Twizo
18
+ #
19
+ def setup
20
+ @twizo = Twizo::Twizo.new(API_KEY, API_HOST)
21
+ end
22
+
23
+ #
24
+ # Test if an error is thrown when an invalid key is provided
25
+ #
26
+ def test_invalid_key
27
+ api_key = 'invalid_key'
28
+
29
+ assert_raise Twizo::TwizoError do
30
+ @twizo = Twizo::Twizo.new(api_key, API_HOST)
31
+ number_lookup = @twizo.create_number_lookup(NUMBER1)
32
+ number_lookup.send
33
+ end
34
+ end
35
+
36
+ #
37
+ # Test creation of number lookup with multiple numbers
38
+ #
39
+ def test_create_number_lookup
40
+ number_lookup = @twizo.create_number_lookup(nil)
41
+
42
+ test_numbers = [NUMBER1, NUMBER2]
43
+ test_tag = 'test tag'
44
+ test_validity = 259200
45
+ test_result_type = 0
46
+ test_callback_url = nil
47
+
48
+ number_lookup.params.numbers = test_numbers
49
+ number_lookup.params.tag = test_tag
50
+ number_lookup.params.validity = test_validity
51
+ number_lookup.params.result_type = test_result_type
52
+ number_lookup.params.callback_url = test_callback_url
53
+
54
+ number_lookup = number_lookup.send
55
+
56
+ assert_equal test_numbers[0], number_lookup.result[0].number
57
+ assert_equal test_tag, number_lookup.result[0].tag
58
+ assert_equal 'no status', number_lookup.result[0].status
59
+
60
+ assert_equal test_numbers[1], number_lookup.result[1].number
61
+ assert_equal test_tag, number_lookup.result[1].tag
62
+ assert_equal 'no status', number_lookup.result[1].status
63
+ end
64
+
65
+ #
66
+ # Test creation of number lookup with a single number
67
+ #
68
+ def test_create_number_lookup_single
69
+ number_lookup = @twizo.create_number_lookup(nil)
70
+
71
+ test_number = NUMBER1
72
+ number_lookup.params.numbers = test_number
73
+
74
+ number_lookup = number_lookup.send
75
+
76
+ assert_equal test_number, number_lookup.result[0].number
77
+ assert_equal 'no status', number_lookup.result[0].status
78
+ end
79
+
80
+ #
81
+ # Test creation of number lookup and get status
82
+ #
83
+ def test_number_lookup_status
84
+ test_number = NUMBER1
85
+
86
+ number_lookup = @twizo.create_number_lookup(test_number)
87
+ number_lookup = number_lookup.send
88
+
89
+ test_message_id = number_lookup.result[0].messageId
90
+
91
+ status = @twizo.get_number_lookup_status(test_message_id)
92
+
93
+ assert_equal test_message_id, status.messageId
94
+ assert_equal test_number, status.number
95
+ end
96
+
97
+ #
98
+ # Test creation of number lookup and get results after completion of status
99
+ #
100
+ def test_number_lookup_results
101
+ test_number = NUMBER1
102
+
103
+ number_lookup = @twizo.create_number_lookup(test_number)
104
+ number_lookup.params.result_type = 2
105
+
106
+ number_lookup = number_lookup.send
107
+
108
+ x = false
109
+ until x
110
+ if @twizo.get_number_lookup_status(number_lookup.result[0].messageId).status == 'no status'
111
+ sleep 0.5
112
+ next
113
+ end
114
+ x = true
115
+ end
116
+
117
+ results = @twizo.get_number_lookup_results
118
+
119
+ assert_not_nil results.batchId
120
+
121
+ results.result.each do |item|
122
+ assert_not_nil item.number
123
+ assert_not_nil item.messageId
124
+ end
125
+ end
126
+
127
+ #
128
+ # Test number input normal
129
+ #
130
+ def test_different_numbers1
131
+ test_numbers = '1234567890'
132
+ number_lookup = @twizo.create_number_lookup(test_numbers)
133
+
134
+ number_lookup = number_lookup.send
135
+
136
+ assert_equal '1234567890', number_lookup.result[0].number
137
+ end
138
+
139
+ #
140
+ # Test number input with +
141
+ #
142
+ def test_different_numbers2
143
+ test_numbers = '+1234567890'
144
+ number_lookup = @twizo.create_number_lookup(test_numbers)
145
+
146
+ number_lookup = number_lookup.send
147
+
148
+ assert_equal '1234567890', number_lookup.result[0].number
149
+ end
150
+
151
+ #
152
+ # Test number input with 00
153
+ #
154
+ def test_different_numbers3
155
+ test_numbers = '00001234567890'
156
+ number_lookup = @twizo.create_number_lookup(test_numbers)
157
+
158
+ number_lookup = number_lookup.send
159
+
160
+ assert_equal '1234567890', number_lookup.result[0].number
161
+ end
162
+
163
+ #
164
+ # Test number input with ' '
165
+ #
166
+ def test_different_numbers4
167
+ test_numbers = '1 23 45 67 89 0'
168
+ number_lookup = @twizo.create_number_lookup(test_numbers)
169
+
170
+ number_lookup = number_lookup.send
171
+
172
+ assert_equal '1234567890', number_lookup.result[0].number
173
+ end
174
+
175
+ #
176
+ # Test number input with Characters
177
+ #
178
+ def test_different_numbers5
179
+ test_numbers = '1 23 A5 67 89 o'
180
+
181
+ assert_raise Twizo::TwizoError do
182
+ number_lookup = @twizo.create_number_lookup(test_numbers)
183
+ number_lookup.send
184
+ end
185
+ end
186
+
187
+ #
188
+ # Test number input with ()
189
+ #
190
+ def test_different_numbers6
191
+ test_numbers = '(1)234567890'
192
+ number_lookup = @twizo.create_number_lookup(test_numbers)
193
+
194
+ number_lookup = number_lookup.send
195
+
196
+ assert_equal '1234567890', number_lookup.result[0].number
197
+ end
198
+
199
+ #
200
+ # Test number input with + and ()
201
+ #
202
+ def test_different_numbers7
203
+ test_numbers = '+(1)234567890'
204
+ number_lookup = @twizo.create_number_lookup(test_numbers)
205
+
206
+ number_lookup = number_lookup.send
207
+
208
+ assert_equal '1234567890', number_lookup.result[0].number
209
+ end
210
+
211
+ def teardown
212
+ print "\n#{method_name}"
213
+ end
214
+
215
+ end