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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +126 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/backup_codes.rb +31 -0
- data/examples/backup_codes_check.rb +29 -0
- data/examples/backup_codes_delete.rb +31 -0
- data/examples/backup_codes_update.rb +31 -0
- data/examples/backup_codes_verify.rb +34 -0
- data/examples/balance.rb +18 -0
- data/examples/examples_init.rb +43 -0
- data/examples/number_lookup.rb +32 -0
- data/examples/number_lookup_results.rb +28 -0
- data/examples/number_lookup_status.rb +29 -0
- data/examples/sms.rb +32 -0
- data/examples/sms_advanced.rb +33 -0
- data/examples/sms_concat.rb +32 -0
- data/examples/sms_multiple.rb +35 -0
- data/examples/sms_results.rb +28 -0
- data/examples/sms_status.rb +29 -0
- data/examples/sms_validation_errors.rb +28 -0
- data/examples/verification.rb +31 -0
- data/examples/verification_status.rb +29 -0
- data/examples/verification_verify_token.rb +39 -0
- data/examples/widget.rb +31 -0
- data/examples/widget_status.rb +34 -0
- data/lib/twizo.rb +190 -0
- data/lib/twizo/client.rb +45 -0
- data/lib/twizo/client/net_http_client.rb +48 -0
- data/lib/twizo/entity.rb +114 -0
- data/lib/twizo/modules/backup_codes.rb +100 -0
- data/lib/twizo/modules/balance.rb +40 -0
- data/lib/twizo/modules/number_lookup.rb +81 -0
- data/lib/twizo/modules/params/backup_codes_params.rb +19 -0
- data/lib/twizo/modules/params/number_lookup_params.rb +23 -0
- data/lib/twizo/modules/params/params.rb +37 -0
- data/lib/twizo/modules/params/sms_params.rb +34 -0
- data/lib/twizo/modules/params/verification_params.rb +30 -0
- data/lib/twizo/modules/params/widget_params.rb +29 -0
- data/lib/twizo/modules/sms.rb +130 -0
- data/lib/twizo/modules/verification.rb +73 -0
- data/lib/twizo/modules/widget.rb +59 -0
- data/lib/twizo/result.rb +66 -0
- data/lib/twizo/status_codes.rb +32 -0
- data/lib/twizo/twizo_error.rb +26 -0
- data/lib/twizo/version.rb +3 -0
- data/test/test_all.rb +19 -0
- data/test/test_backup_codes.rb +120 -0
- data/test/test_balance.rb +51 -0
- data/test/test_init.rb +41 -0
- data/test/test_number_lookup.rb +215 -0
- data/test/test_sms.rb +270 -0
- data/test/test_verification.rb +106 -0
- data/test/test_widget.rb +107 -0
- data/twizo.gemspec +34 -0
- metadata +154 -0
data/lib/twizo.rb
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
require "twizo/version"
|
|
2
|
+
require "twizo/client/net_http_client"
|
|
3
|
+
require "twizo/entity"
|
|
4
|
+
require "twizo/status_codes"
|
|
5
|
+
|
|
6
|
+
require "twizo/modules/number_lookup"
|
|
7
|
+
require "twizo/modules/sms"
|
|
8
|
+
require "twizo/modules/balance"
|
|
9
|
+
require "twizo/modules/backup_codes"
|
|
10
|
+
require "twizo/modules/verification"
|
|
11
|
+
require "twizo/modules/widget"
|
|
12
|
+
|
|
13
|
+
=begin
|
|
14
|
+
|
|
15
|
+
This file is part of the Twizo php api
|
|
16
|
+
|
|
17
|
+
(c) Twizo <info@twizo.com>
|
|
18
|
+
|
|
19
|
+
For the full copyright and license information, please view the LICENSE
|
|
20
|
+
File that was distributed with this source code.
|
|
21
|
+
|
|
22
|
+
=end
|
|
23
|
+
|
|
24
|
+
module Twizo
|
|
25
|
+
|
|
26
|
+
class Twizo
|
|
27
|
+
|
|
28
|
+
#
|
|
29
|
+
# Constructor
|
|
30
|
+
#
|
|
31
|
+
# @param [String] api_key
|
|
32
|
+
# @param [String] api_host
|
|
33
|
+
#
|
|
34
|
+
def initialize(api_key, api_host)
|
|
35
|
+
@client = NetHttpClient.new(api_key, api_host)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Number Lookup
|
|
39
|
+
|
|
40
|
+
#
|
|
41
|
+
# @param [String|Array] numbers
|
|
42
|
+
#
|
|
43
|
+
def create_number_lookup(numbers)
|
|
44
|
+
number_lookup = Entity.new(@client).extend(NumberLookup)
|
|
45
|
+
number_lookup.set(numbers)
|
|
46
|
+
number_lookup
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
#
|
|
50
|
+
# @param [String] message_id
|
|
51
|
+
#
|
|
52
|
+
# @return [Object]
|
|
53
|
+
#
|
|
54
|
+
def get_number_lookup_status(message_id)
|
|
55
|
+
number_lookup = Entity.new(@client).extend(NumberLookup)
|
|
56
|
+
number_lookup.populate(message_id)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
#
|
|
60
|
+
# @return [Object]
|
|
61
|
+
#
|
|
62
|
+
def get_number_lookup_results
|
|
63
|
+
number_lookup = Entity.new(@client).extend(NumberLookup)
|
|
64
|
+
number_lookup.poll
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Sms
|
|
68
|
+
|
|
69
|
+
#
|
|
70
|
+
# @param [String] body
|
|
71
|
+
# @param [String|Array] recipients
|
|
72
|
+
# @param [String] sender
|
|
73
|
+
#
|
|
74
|
+
# @return [Object]
|
|
75
|
+
#
|
|
76
|
+
def create_sms(body, recipients, sender)
|
|
77
|
+
sms = Entity.new(@client).extend(Sms)
|
|
78
|
+
sms.set(body, recipients, sender)
|
|
79
|
+
sms
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
#
|
|
83
|
+
# @param [String] message_id
|
|
84
|
+
#
|
|
85
|
+
# @return [Object]
|
|
86
|
+
#
|
|
87
|
+
def get_sms_status(message_id)
|
|
88
|
+
sms = Entity.new(@client).extend(Sms)
|
|
89
|
+
sms.populate(message_id)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
#
|
|
93
|
+
# @return [Object]
|
|
94
|
+
#
|
|
95
|
+
def get_sms_results
|
|
96
|
+
sms = Entity.new(@client).extend(Sms)
|
|
97
|
+
sms.poll
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Balance
|
|
101
|
+
|
|
102
|
+
def get_balance
|
|
103
|
+
balance = Entity.new(@client).extend(Balance)
|
|
104
|
+
balance.send
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Backup Codes
|
|
108
|
+
|
|
109
|
+
#
|
|
110
|
+
# @param [String] identifier
|
|
111
|
+
#
|
|
112
|
+
# @return [Object]
|
|
113
|
+
#
|
|
114
|
+
def create_backup_codes(identifier)
|
|
115
|
+
backup_codes = Entity.new(@client).extend(BackupCodes)
|
|
116
|
+
backup_codes.set(identifier)
|
|
117
|
+
backup_codes
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
#
|
|
121
|
+
# @param [String] identifier
|
|
122
|
+
#
|
|
123
|
+
# @return [Object]
|
|
124
|
+
#
|
|
125
|
+
def get_backup_code(identifier)
|
|
126
|
+
backup_codes = Entity.new(@client).extend(BackupCodes)
|
|
127
|
+
backup_codes.populate(identifier)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Verification
|
|
131
|
+
|
|
132
|
+
#
|
|
133
|
+
# @param [String] recipient
|
|
134
|
+
#
|
|
135
|
+
# @return [Object]
|
|
136
|
+
#
|
|
137
|
+
def create_verification(recipient)
|
|
138
|
+
verification = Entity.new(@client).extend(Verification)
|
|
139
|
+
verification.set(recipient)
|
|
140
|
+
verification
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
#
|
|
144
|
+
# @param [String] message_id
|
|
145
|
+
# @param [String] token
|
|
146
|
+
#
|
|
147
|
+
# @return [Object]
|
|
148
|
+
#
|
|
149
|
+
def verify_token(message_id, token)
|
|
150
|
+
verification = Entity.new(@client).extend(Verification)
|
|
151
|
+
verification.verify(message_id, token)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
#
|
|
155
|
+
# @param [String] message_id
|
|
156
|
+
#
|
|
157
|
+
# @return [Object]
|
|
158
|
+
#
|
|
159
|
+
def get_verification_status(message_id)
|
|
160
|
+
verification = Entity.new(@client).extend(Verification)
|
|
161
|
+
verification.populate(message_id)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Verification widget
|
|
165
|
+
|
|
166
|
+
#
|
|
167
|
+
# @param [String] recipient
|
|
168
|
+
#
|
|
169
|
+
# @return [Object]
|
|
170
|
+
#
|
|
171
|
+
def create_widget(recipient)
|
|
172
|
+
widget = Entity.new(@client).extend(Widget)
|
|
173
|
+
widget.set(recipient)
|
|
174
|
+
widget
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
#
|
|
178
|
+
# @param [String] session_token
|
|
179
|
+
# @param [String|null] recipient
|
|
180
|
+
# @param [String|null] backup_code_identifier
|
|
181
|
+
#
|
|
182
|
+
# @return [Object]
|
|
183
|
+
#
|
|
184
|
+
def get_widget_status(session_token, recipient = nil, backup_code_identifier = nil)
|
|
185
|
+
widget = Entity.new(@client).extend(Widget)
|
|
186
|
+
widget.populate(session_token, recipient, backup_code_identifier)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
end
|
|
190
|
+
end
|
data/lib/twizo/client.rb
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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 Client
|
|
15
|
+
|
|
16
|
+
API_VERSION = 'v1'
|
|
17
|
+
API_USERNAME = 'twizo'
|
|
18
|
+
LIB_NAME = 'Twizo-ruby-lib'
|
|
19
|
+
LIB_VERSION = '0.1.0'
|
|
20
|
+
|
|
21
|
+
#
|
|
22
|
+
# Constructor
|
|
23
|
+
#
|
|
24
|
+
# @param [String] api_key
|
|
25
|
+
# @param [String] api_host
|
|
26
|
+
#
|
|
27
|
+
def initialize(api_key, api_host)
|
|
28
|
+
@api_key, @api_host = api_key, api_host
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
#
|
|
32
|
+
# @return [String]
|
|
33
|
+
#
|
|
34
|
+
def user_agent
|
|
35
|
+
sprintf('%s/%s Ruby/%s/%s',
|
|
36
|
+
LIB_NAME,
|
|
37
|
+
LIB_VERSION,
|
|
38
|
+
RUBY_VERSION,
|
|
39
|
+
RUBY_PLATFORM
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
require_relative "../client"
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'uri'
|
|
5
|
+
|
|
6
|
+
module Twizo
|
|
7
|
+
|
|
8
|
+
class NetHttpClient < Client
|
|
9
|
+
|
|
10
|
+
#
|
|
11
|
+
# @param [String] method
|
|
12
|
+
# @param [String] location
|
|
13
|
+
#
|
|
14
|
+
# @return [Object]
|
|
15
|
+
#
|
|
16
|
+
def send_request(method, location, post_params)
|
|
17
|
+
uri = URI.parse("https://#{@api_host}/#{Client::API_VERSION}/#{location}")
|
|
18
|
+
|
|
19
|
+
case method
|
|
20
|
+
when 'GET'
|
|
21
|
+
request = Net::HTTP::Get.new(uri)
|
|
22
|
+
when 'POST'
|
|
23
|
+
request = Net::HTTP::Post.new(uri)
|
|
24
|
+
when 'PUT'
|
|
25
|
+
request = Net::HTTP::Put.new(uri)
|
|
26
|
+
when 'DELETE'
|
|
27
|
+
request = Net::HTTP::Delete.new(uri)
|
|
28
|
+
else
|
|
29
|
+
raise RuntimeError.new('No method provided')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
request.add_field('user-agent', user_agent)
|
|
33
|
+
request.basic_auth(API_USERNAME, @api_key)
|
|
34
|
+
request.content_type = 'application/json; charset=utf8'
|
|
35
|
+
request['accept'] = 'application/json'
|
|
36
|
+
|
|
37
|
+
request.body = post_params unless post_params.nil?
|
|
38
|
+
|
|
39
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
|
40
|
+
https.use_ssl = true
|
|
41
|
+
|
|
42
|
+
https.request(request)
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
data/lib/twizo/entity.rb
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
require "twizo/result"
|
|
2
|
+
require "twizo/twizo_error"
|
|
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
|
+
module Twizo
|
|
16
|
+
|
|
17
|
+
class Entity
|
|
18
|
+
|
|
19
|
+
#
|
|
20
|
+
# http method actions
|
|
21
|
+
#
|
|
22
|
+
ACTION_CREATE = 'POST'
|
|
23
|
+
ACTION_UPDATE = 'PUT'
|
|
24
|
+
ACTION_RETRIEVE = 'GET'
|
|
25
|
+
ACTION_REMOVE = 'DELETE'
|
|
26
|
+
|
|
27
|
+
#
|
|
28
|
+
# Constructor
|
|
29
|
+
#
|
|
30
|
+
# @param [Client] client
|
|
31
|
+
#
|
|
32
|
+
def initialize(client)
|
|
33
|
+
@client = client
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
#
|
|
37
|
+
# @param [String] id
|
|
38
|
+
# @param [String|null] widget_recipient
|
|
39
|
+
# @param [String|null] widget_backup_code_id
|
|
40
|
+
#
|
|
41
|
+
# @return [Object]
|
|
42
|
+
#
|
|
43
|
+
def populate(id, widget_recipient = nil, widget_backup_code_id = nil)
|
|
44
|
+
raise 'Error: id not provided' unless id
|
|
45
|
+
|
|
46
|
+
params = id
|
|
47
|
+
params += "?recipient=#{widget_recipient}" if widget_recipient
|
|
48
|
+
params += "&backupCodeIdentifier=#{widget_backup_code_id}" if widget_backup_code_id
|
|
49
|
+
|
|
50
|
+
response = send_api_call(ACTION_RETRIEVE, "#{location}/#{params}")
|
|
51
|
+
|
|
52
|
+
raise response if response.kind_of?(TwizoError)
|
|
53
|
+
|
|
54
|
+
response_to_array(response)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
#
|
|
60
|
+
# @param [String] method
|
|
61
|
+
# @param [String] location
|
|
62
|
+
# @param [Object|null] post_params
|
|
63
|
+
#
|
|
64
|
+
# @return [Object]
|
|
65
|
+
#
|
|
66
|
+
def send_api_call(method, location, post_params = nil)
|
|
67
|
+
response = @client.send_request(method, location, post_params)
|
|
68
|
+
|
|
69
|
+
case Integer(response.code)
|
|
70
|
+
when 200, 201
|
|
71
|
+
JSON.parse(response.body)
|
|
72
|
+
when 204
|
|
73
|
+
response
|
|
74
|
+
else
|
|
75
|
+
raise TwizoError.new(response.code, JSON.parse(response.body))
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
#
|
|
80
|
+
# @param [Object] response
|
|
81
|
+
# @param [String|null] field
|
|
82
|
+
#
|
|
83
|
+
# @return [Object]
|
|
84
|
+
#
|
|
85
|
+
def response_to_array(response, field = nil)
|
|
86
|
+
|
|
87
|
+
parent_items = response.reject { |key| key == '_embedded' }
|
|
88
|
+
|
|
89
|
+
results = Result.new(parent_items)
|
|
90
|
+
|
|
91
|
+
if field
|
|
92
|
+
child_items = response['_embedded'][field]
|
|
93
|
+
|
|
94
|
+
if child_items.kind_of?(Array)
|
|
95
|
+
|
|
96
|
+
child_items.each do |item|
|
|
97
|
+
results.add_result(Result.new(item))
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
else
|
|
101
|
+
|
|
102
|
+
results.add_result(Result.new(child_items))
|
|
103
|
+
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
results
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
end
|
|
114
|
+
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
require_relative 'params/backup_codes_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 BackupCodes
|
|
17
|
+
|
|
18
|
+
#
|
|
19
|
+
# Getter for params
|
|
20
|
+
#
|
|
21
|
+
attr_reader :params
|
|
22
|
+
|
|
23
|
+
#
|
|
24
|
+
# @param [String] identifier
|
|
25
|
+
#
|
|
26
|
+
def set(identifier)
|
|
27
|
+
@params = BackupCodesParams.new
|
|
28
|
+
@params.identifier = identifier
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
#
|
|
32
|
+
# Send message to the server and return response
|
|
33
|
+
#
|
|
34
|
+
# @return [Object]
|
|
35
|
+
#
|
|
36
|
+
def send
|
|
37
|
+
post_params = @params.to_json
|
|
38
|
+
|
|
39
|
+
response = send_api_call(Entity::ACTION_CREATE, location, post_params)
|
|
40
|
+
|
|
41
|
+
raise response if response.kind_of?(TwizoError)
|
|
42
|
+
|
|
43
|
+
response_to_array(response)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
#
|
|
47
|
+
# @param [String] backup_code
|
|
48
|
+
#
|
|
49
|
+
# @return [Object]
|
|
50
|
+
#
|
|
51
|
+
def verify(backup_code)
|
|
52
|
+
response = send_api_call(Entity::ACTION_RETRIEVE, "#{location}/#{@params.identifier}?token=#{backup_code}")
|
|
53
|
+
|
|
54
|
+
raise response if response.kind_of?(TwizoError)
|
|
55
|
+
|
|
56
|
+
response_to_array(response)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
#
|
|
60
|
+
# Update de backup codes
|
|
61
|
+
#
|
|
62
|
+
# @return [Object]
|
|
63
|
+
#
|
|
64
|
+
def update
|
|
65
|
+
post_params = @params.to_json
|
|
66
|
+
|
|
67
|
+
response = send_api_call(Entity::ACTION_UPDATE, "#{location}/#{@params.identifier}", post_params)
|
|
68
|
+
|
|
69
|
+
raise response if response.kind_of?(TwizoError)
|
|
70
|
+
|
|
71
|
+
response_to_array(response)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
#
|
|
75
|
+
# Delete de backup codes
|
|
76
|
+
#
|
|
77
|
+
# @return [Object]
|
|
78
|
+
#
|
|
79
|
+
def delete
|
|
80
|
+
response = send_api_call(Entity::ACTION_REMOVE, "#{location}/#{@params.identifier}")
|
|
81
|
+
|
|
82
|
+
raise response if response.kind_of?(TwizoError)
|
|
83
|
+
|
|
84
|
+
# return 204 No Content
|
|
85
|
+
response
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
#
|
|
91
|
+
# @return [String]
|
|
92
|
+
#
|
|
93
|
+
def location
|
|
94
|
+
'backupcode'
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
|
100
|
+
|