nexmo 3.1.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +22 -7
- data/README.md +10 -7
- data/lib/nexmo.rb +36 -29
- data/lib/nexmo/version.rb +3 -0
- data/nexmo.gemspec +5 -3
- data/spec/nexmo_spec.rb +45 -45
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d795a930aa30a986720f2c457bcc4cd38d25bd0
|
4
|
+
data.tar.gz: dcdb8a13190059e0fd03839f4181cdeed6f368ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05fe7b23621f647f18f97afe4e56702ac75927d2a403c2ed82d94a5961f6a8eec2315404b1af0e4f9531316a21317a50c290652e571ea74eceb8b9ebbada65a1
|
7
|
+
data.tar.gz: 698907b35b947e52e4a380ac8988449886891a8c3dee1da785fae29d6c6d2cff410fcf2b9d25f54a04e9f6b4ae45af8bbbd528ad4024ec73534dec992f0f26a5
|
data/LICENSE.txt
CHANGED
@@ -1,9 +1,24 @@
|
|
1
|
-
|
1
|
+
The MIT License (MIT)
|
2
2
|
|
3
|
-
|
4
|
-
Please see <http://www.gnu.org/licenses/lgpl-3.0.html> for license text.
|
3
|
+
Copyright (c) 2016 Nexmo Inc
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
Permission is hereby granted, free of charge, to any person
|
6
|
+
obtaining a copy of this software and associated documentation
|
7
|
+
files (the "Software"), to deal in the Software without
|
8
|
+
restriction, including without limitation the rights to use,
|
9
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the
|
11
|
+
Software is furnished to do so, subject to the following
|
12
|
+
conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
19
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
21
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
22
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
23
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
24
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -15,21 +15,24 @@ Sending a message
|
|
15
15
|
-----------------
|
16
16
|
|
17
17
|
Construct a Nexmo::Client object with your API credentials and call
|
18
|
-
the #send_message method
|
18
|
+
the #send_message method. For example:
|
19
19
|
|
20
20
|
```ruby
|
21
21
|
require 'nexmo'
|
22
22
|
|
23
23
|
nexmo = Nexmo::Client.new(key: 'YOUR API KEY', secret: 'YOUR API SECRET')
|
24
24
|
|
25
|
-
nexmo.send_message(from: 'Ruby', to: 'YOUR NUMBER', text: 'Hello world')
|
26
|
-
```
|
25
|
+
response = nexmo.send_message(from: 'Ruby', to: 'YOUR NUMBER', text: 'Hello world')
|
27
26
|
|
28
|
-
|
29
|
-
|
27
|
+
if response['messages'][0]['status'].zero?
|
28
|
+
# success!
|
29
|
+
else
|
30
|
+
# error response
|
31
|
+
end
|
32
|
+
```
|
30
33
|
|
31
|
-
The Nexmo documentation contains a [list of
|
32
|
-
which may be useful for debugging
|
34
|
+
The Nexmo documentation contains a [list of response codes](https://docs.nexmo.com/api-ref/sms-api/response/status-codes)
|
35
|
+
which may be useful for debugging errors. Remember that phone numbers
|
33
36
|
should be specified in international format, and other country specific
|
34
37
|
restrictions may apply (e.g. US messages must originate from either a
|
35
38
|
pre-approved long number or short code).
|
data/lib/nexmo.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'nexmo/version'
|
1
2
|
require 'net/http'
|
2
3
|
require 'json'
|
3
4
|
require 'cgi'
|
@@ -5,7 +6,11 @@ require 'cgi'
|
|
5
6
|
module Nexmo
|
6
7
|
class Error < StandardError; end
|
7
8
|
|
8
|
-
class
|
9
|
+
class ClientError < Error; end
|
10
|
+
|
11
|
+
class ServerError < Error; end
|
12
|
+
|
13
|
+
class AuthenticationError < ClientError; end
|
9
14
|
|
10
15
|
class Client
|
11
16
|
attr_accessor :key, :secret
|
@@ -19,15 +24,7 @@ module Nexmo
|
|
19
24
|
end
|
20
25
|
|
21
26
|
def send_message(params)
|
22
|
-
|
23
|
-
|
24
|
-
item = response['messages'].first
|
25
|
-
|
26
|
-
status = item['status'].to_i
|
27
|
-
|
28
|
-
raise Error, "#{item['error-text']} (status=#{status})" unless status.zero?
|
29
|
-
|
30
|
-
return item
|
27
|
+
post('/sms/json', params)
|
31
28
|
end
|
32
29
|
|
33
30
|
def get_balance
|
@@ -35,11 +32,11 @@ module Nexmo
|
|
35
32
|
end
|
36
33
|
|
37
34
|
def get_country_pricing(country_code)
|
38
|
-
get('/account/get-pricing/outbound',
|
35
|
+
get('/account/get-pricing/outbound', country: country_code)
|
39
36
|
end
|
40
37
|
|
41
38
|
def get_prefix_pricing(prefix)
|
42
|
-
get('/account/get-prefix-pricing/outbound',
|
39
|
+
get('/account/get-prefix-pricing/outbound', prefix: prefix)
|
43
40
|
end
|
44
41
|
|
45
42
|
def get_account_numbers(params)
|
@@ -47,13 +44,7 @@ module Nexmo
|
|
47
44
|
end
|
48
45
|
|
49
46
|
def get_available_numbers(country_code, params = {})
|
50
|
-
get('/number/search', {:
|
51
|
-
end
|
52
|
-
|
53
|
-
def number_search(country_code, params = {})
|
54
|
-
Kernel.warn '[nexmo] #number_search is deprecated and will be removed, please use #get_available_numbers instead'
|
55
|
-
|
56
|
-
get_available_numbers(country_code, params)
|
47
|
+
get('/number/search', {country: country_code}.merge(params))
|
57
48
|
end
|
58
49
|
|
59
50
|
def buy_number(params)
|
@@ -69,7 +60,7 @@ module Nexmo
|
|
69
60
|
end
|
70
61
|
|
71
62
|
def get_message(id)
|
72
|
-
get('/search/message',
|
63
|
+
get('/search/message', id: id)
|
73
64
|
end
|
74
65
|
|
75
66
|
def get_message_rejections(params)
|
@@ -77,7 +68,7 @@ module Nexmo
|
|
77
68
|
end
|
78
69
|
|
79
70
|
def search_messages(params)
|
80
|
-
get('/search/messages', Hash === params ? params : {:
|
71
|
+
get('/search/messages', Hash === params ? params : {ids: Array(params)})
|
81
72
|
end
|
82
73
|
|
83
74
|
def send_ussd_push_message(params)
|
@@ -121,44 +112,56 @@ module Nexmo
|
|
121
112
|
end
|
122
113
|
|
123
114
|
def get_verification_request(id)
|
124
|
-
get('https://api.nexmo.com/verify/search/json', :
|
115
|
+
get('https://api.nexmo.com/verify/search/json', request_id: id)
|
125
116
|
end
|
126
117
|
|
127
118
|
def control_verification_request(params)
|
128
119
|
post('https://api.nexmo.com/verify/control/json', params)
|
129
120
|
end
|
130
121
|
|
122
|
+
def get_basic_number_insight(params)
|
123
|
+
get('https://api.nexmo.com/number/format/json', params)
|
124
|
+
end
|
125
|
+
|
126
|
+
def get_number_insight(params)
|
127
|
+
get('https://api.nexmo.com/number/lookup/json', params)
|
128
|
+
end
|
129
|
+
|
131
130
|
def request_number_insight(params)
|
132
131
|
post('/ni/json', params)
|
133
132
|
end
|
134
133
|
|
135
134
|
private
|
136
135
|
|
136
|
+
USER_AGENT = "ruby-nexmo/#{VERSION}/#{RUBY_VERSION}"
|
137
|
+
|
137
138
|
def get(path, params = {})
|
138
139
|
uri = URI.join("https://#{@host}", path)
|
139
|
-
uri.query = query_string(params.merge(:
|
140
|
+
uri.query = query_string(params.merge(api_key: @key, api_secret: @secret))
|
140
141
|
|
141
142
|
get_request = Net::HTTP::Get.new(uri.request_uri)
|
143
|
+
get_request['User-Agent'] = USER_AGENT
|
142
144
|
|
143
145
|
http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
|
144
146
|
http.use_ssl = true
|
145
147
|
|
146
|
-
parse http.request(get_request)
|
148
|
+
parse http.request(get_request), uri.host
|
147
149
|
end
|
148
150
|
|
149
151
|
def post(path, params)
|
150
152
|
uri = URI.join("https://#{@host}", path)
|
151
153
|
|
152
154
|
post_request = Net::HTTP::Post.new(uri.request_uri)
|
153
|
-
post_request.form_data = params.merge(:
|
155
|
+
post_request.form_data = params.merge(api_key: @key, api_secret: @secret)
|
156
|
+
post_request['User-Agent'] = USER_AGENT
|
154
157
|
|
155
158
|
http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
|
156
159
|
http.use_ssl = true
|
157
160
|
|
158
|
-
parse http.request(post_request)
|
161
|
+
parse http.request(post_request), uri.host
|
159
162
|
end
|
160
163
|
|
161
|
-
def parse(http_response)
|
164
|
+
def parse(http_response, host)
|
162
165
|
case http_response
|
163
166
|
when Net::HTTPSuccess
|
164
167
|
if http_response['Content-Type'].split(';').first == 'application/json'
|
@@ -167,9 +170,13 @@ module Nexmo
|
|
167
170
|
http_response.body
|
168
171
|
end
|
169
172
|
when Net::HTTPUnauthorized
|
170
|
-
raise AuthenticationError
|
173
|
+
raise AuthenticationError, "#{http_response.code} response from #{host}"
|
174
|
+
when Net::HTTPClientError
|
175
|
+
raise ClientError, "#{http_response.code} response from #{host}"
|
176
|
+
when Net::HTTPServerError
|
177
|
+
raise ServerError, "#{http_response.code} response from #{host}"
|
171
178
|
else
|
172
|
-
raise Error, "
|
179
|
+
raise Error, "#{http_response.code} response from #{host}"
|
173
180
|
end
|
174
181
|
end
|
175
182
|
|
data/nexmo.gemspec
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
+
require File.expand_path('lib/nexmo/version', File.dirname(__FILE__))
|
2
|
+
|
1
3
|
Gem::Specification.new do |s|
|
2
4
|
s.name = 'nexmo'
|
3
|
-
s.version =
|
4
|
-
s.license = '
|
5
|
+
s.version = Nexmo::VERSION
|
6
|
+
s.license = 'MIT'
|
5
7
|
s.platform = Gem::Platform::RUBY
|
6
8
|
s.authors = ['Tim Craft']
|
7
9
|
s.email = ['mail@timcraft.com']
|
8
|
-
s.homepage = 'http://github.com/
|
10
|
+
s.homepage = 'http://github.com/Nexmo/nexmo'
|
9
11
|
s.description = 'Ruby client for the Nexmo API'
|
10
12
|
s.summary = 'See description'
|
11
13
|
s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE.txt README.md nexmo.gemspec)
|
data/spec/nexmo_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe 'Nexmo::Client' do
|
|
18
18
|
|
19
19
|
@json_response_object = {'key' => 'value'}
|
20
20
|
|
21
|
-
@example_message_hash = {:
|
21
|
+
@example_message_hash = {from: 'ruby', to: 'number', text: 'Hey!'}
|
22
22
|
|
23
23
|
@client = Nexmo::Client.new(key: 'key', secret: 'secret')
|
24
24
|
end
|
@@ -29,17 +29,7 @@ describe 'Nexmo::Client' do
|
|
29
29
|
|
30
30
|
stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(response_body)
|
31
31
|
|
32
|
-
@client.send_message(@example_message_hash).must_equal({'status' => 0, 'message-id' => 'id'})
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'raises an exception if the response body contains an error' do
|
36
|
-
response_body = json_response_body('{"messages":[{"status":2,"error-text":"Missing from param"}]}')
|
37
|
-
|
38
|
-
stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(response_body)
|
39
|
-
|
40
|
-
exception = proc { @client.send_message(@example_message_hash) }.must_raise(Nexmo::Error)
|
41
|
-
|
42
|
-
exception.message.must_include('Missing from param')
|
32
|
+
@client.send_message(@example_message_hash).must_equal({'messages' => [{'status' => 0, 'message-id' => 'id'}]})
|
43
33
|
end
|
44
34
|
end
|
45
35
|
|
@@ -79,31 +69,7 @@ describe 'Nexmo::Client' do
|
|
79
69
|
|
80
70
|
stub_request(:get, url).to_return(@json_response_body)
|
81
71
|
|
82
|
-
@client.get_account_numbers(:
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe 'number_search method' do
|
87
|
-
it 'fetches the number search resource for the given country with the given parameters and returns the response object' do
|
88
|
-
url = "#@base_url/number/search?api_key=key&api_secret=secret&country=CA&size=25"
|
89
|
-
|
90
|
-
stub_request(:get, url).to_return(@json_response_body)
|
91
|
-
|
92
|
-
@client.number_search(:CA, :size => 25).must_equal(@json_response_object)
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'emits a deprecation warning' do
|
96
|
-
message = nil
|
97
|
-
|
98
|
-
url = "#@base_url/number/search?api_key=key&api_secret=secret&country=CA&size=25"
|
99
|
-
|
100
|
-
stub_request(:get, url).to_return(@json_response_body)
|
101
|
-
|
102
|
-
Kernel.stub :warn, proc { |msg| message = msg } do
|
103
|
-
@client.number_search(:CA, :size => 25)
|
104
|
-
end
|
105
|
-
|
106
|
-
message.must_match(/#number_search is deprecated/)
|
72
|
+
@client.get_account_numbers(size: 25, pattern: 33).must_equal(@json_response_object)
|
107
73
|
end
|
108
74
|
end
|
109
75
|
|
@@ -113,7 +79,7 @@ describe 'Nexmo::Client' do
|
|
113
79
|
|
114
80
|
stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
|
115
81
|
|
116
|
-
@client.buy_number(:
|
82
|
+
@client.buy_number(country: 'US', msisdn: 'number').must_equal(@json_response_object)
|
117
83
|
end
|
118
84
|
end
|
119
85
|
|
@@ -123,7 +89,7 @@ describe 'Nexmo::Client' do
|
|
123
89
|
|
124
90
|
stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
|
125
91
|
|
126
|
-
@client.cancel_number(:
|
92
|
+
@client.cancel_number(country: 'US', msisdn: 'number').must_equal(@json_response_object)
|
127
93
|
end
|
128
94
|
end
|
129
95
|
|
@@ -133,7 +99,7 @@ describe 'Nexmo::Client' do
|
|
133
99
|
|
134
100
|
stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
|
135
101
|
|
136
|
-
@client.update_number(:
|
102
|
+
@client.update_number(country: 'US', msisdn: 'number', moHttpUrl: 'callback').must_equal(@json_response_object)
|
137
103
|
end
|
138
104
|
end
|
139
105
|
|
@@ -153,7 +119,7 @@ describe 'Nexmo::Client' do
|
|
153
119
|
|
154
120
|
stub_request(:get, url).to_return(@json_response_body)
|
155
121
|
|
156
|
-
@client.get_message_rejections(:
|
122
|
+
@client.get_message_rejections(date: 'YYYY-MM-DD').must_equal(@json_response_object)
|
157
123
|
end
|
158
124
|
end
|
159
125
|
|
@@ -163,7 +129,7 @@ describe 'Nexmo::Client' do
|
|
163
129
|
|
164
130
|
stub_request(:get, url).to_return(@json_response_body)
|
165
131
|
|
166
|
-
@client.search_messages(:
|
132
|
+
@client.search_messages(date: 'YYYY-MM-DD', to: 1234567890).must_equal(@json_response_object)
|
167
133
|
end
|
168
134
|
|
169
135
|
it 'should encode a non hash argument as a list of ids' do
|
@@ -295,6 +261,26 @@ describe 'Nexmo::Client' do
|
|
295
261
|
end
|
296
262
|
end
|
297
263
|
|
264
|
+
describe 'get_basic_number_insight method' do
|
265
|
+
it 'fetches the number format json resource and returns the response object' do
|
266
|
+
url = "#@api_base_url/number/format/json?api_key=key&api_secret=secret&number=447525856424"
|
267
|
+
|
268
|
+
stub_request(:get, url).to_return(@json_response_body)
|
269
|
+
|
270
|
+
@client.get_basic_number_insight(number: '447525856424')
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
describe 'get_number_insight method' do
|
275
|
+
it 'fetches the number lookup json resource and returns the response object' do
|
276
|
+
url = "#@api_base_url/number/lookup/json?api_key=key&api_secret=secret&number=447525856424"
|
277
|
+
|
278
|
+
stub_request(:get, url).to_return(@json_response_body)
|
279
|
+
|
280
|
+
@client.get_number_insight(number: '447525856424')
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
298
284
|
describe 'request_number_insight method' do
|
299
285
|
it 'posts to the number insight resource and returns the response object' do
|
300
286
|
url = "#@base_url/ni/json"
|
@@ -305,10 +291,12 @@ describe 'Nexmo::Client' do
|
|
305
291
|
end
|
306
292
|
end
|
307
293
|
|
308
|
-
it '
|
309
|
-
|
294
|
+
it 'includes a user-agent header with the library version number and ruby version number' do
|
295
|
+
headers = {'User-Agent' => "ruby-nexmo/#{Nexmo::VERSION}/#{RUBY_VERSION}"}
|
296
|
+
|
297
|
+
stub_request(:post, "#@base_url/sms/json").with(headers: headers).to_return(@json_response_body)
|
310
298
|
|
311
|
-
|
299
|
+
@client.send_message(@example_message_hash)
|
312
300
|
end
|
313
301
|
|
314
302
|
it 'raises an authentication error exception if the response code is 401' do
|
@@ -317,6 +305,18 @@ describe 'Nexmo::Client' do
|
|
317
305
|
proc { @client.send_message(@example_message_hash) }.must_raise(Nexmo::AuthenticationError)
|
318
306
|
end
|
319
307
|
|
308
|
+
it 'raises a client error exception if the response code is 4xx' do
|
309
|
+
stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(status: 400)
|
310
|
+
|
311
|
+
proc { @client.send_message(@example_message_hash) }.must_raise(Nexmo::ClientError)
|
312
|
+
end
|
313
|
+
|
314
|
+
it 'raises a server error exception if the response code is 5xx' do
|
315
|
+
stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(status: 500)
|
316
|
+
|
317
|
+
proc { @client.send_message(@example_message_hash) }.must_raise(Nexmo::ServerError)
|
318
|
+
end
|
319
|
+
|
320
320
|
it 'provides an option for specifying a different hostname to connect to' do
|
321
321
|
url = "https://rest-sandbox.nexmo.com/account/get-balance?api_key=key&api_secret=secret"
|
322
322
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nexmo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Craft
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -62,11 +62,12 @@ files:
|
|
62
62
|
- LICENSE.txt
|
63
63
|
- README.md
|
64
64
|
- lib/nexmo.rb
|
65
|
+
- lib/nexmo/version.rb
|
65
66
|
- nexmo.gemspec
|
66
67
|
- spec/nexmo_spec.rb
|
67
|
-
homepage: http://github.com/
|
68
|
+
homepage: http://github.com/Nexmo/nexmo
|
68
69
|
licenses:
|
69
|
-
-
|
70
|
+
- MIT
|
70
71
|
metadata: {}
|
71
72
|
post_install_message:
|
72
73
|
rdoc_options: []
|
@@ -84,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
85
|
version: '0'
|
85
86
|
requirements: []
|
86
87
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.
|
88
|
+
rubygems_version: 2.5.1
|
88
89
|
signing_key:
|
89
90
|
specification_version: 4
|
90
91
|
summary: See description
|