nexmo 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 706d683032b1928b5139d95e402e77497b0da9b2
4
- data.tar.gz: f41681589bcc0a827b08fd521f80dbf2a52ee15c
3
+ metadata.gz: 49b51d133374ce76a15e6a597f0f9946651185b4
4
+ data.tar.gz: 94d0243ccf112419f3ebf62cb9c532d775de26ff
5
5
  SHA512:
6
- metadata.gz: ec1f6d2a5d131f4174407276992e54b00ddc99c87081d277a67c798cc8b4e2ac737c1c5b248deee1d697357409d241a469a9ca9c6fbf8a7b2695ea94f6bca6ad
7
- data.tar.gz: d057d84301e06e758b389724d3526cba8c34986118c40fbc4e14ea302bf15bf00542947941298f9361c1e205f146803a469ce59c6653fabf00ee32938a2919ef
6
+ metadata.gz: e3a0b6310e548f87fa93ab582cfe25ed4afdec5264ffeb3cee6988bbe14883112dcd8a31682dd45ee6470d17e9e7ca1ab83fe96b4b30a244e3a962ba3b9ac42c
7
+ data.tar.gz: a07ad566361244c9062a7e4b35fb825b468197f6a5b62c870297f252a584a7c5155cf88675474dfee2f58a6e6633c8ccf518360d5826dc581d65e2f7adc95264
data/README.md CHANGED
@@ -1,20 +1,28 @@
1
1
  Nexmo Client Library for Ruby
2
2
  =============================
3
3
 
4
- [Installation](#installation) | [Usage](#usage) | [Examples](#examples) | [License](#license)
4
+ [![Gem Version](https://badge.fury.io/rb/nexmo.svg)](https://badge.fury.io/rb/nexmo) [![Build Status](https://api.travis-ci.org/Nexmo/nexmo-ruby.svg?branch=master)](https://travis-ci.org/Nexmo/nexmo-ruby)
5
5
 
6
6
  This is the Ruby client library for Nexmo's API. To use it you'll
7
7
  need a Nexmo account. Sign up [for free at nexmo.com][signup].
8
8
 
9
+ * [Installation](#installation)
10
+ * [Usage](#usage)
11
+ * [Examples](#examples)
12
+ * [Coverage](#api-coverage)
13
+ * [License](#license)
14
+
9
15
 
10
16
  Installation
11
17
  ------------
12
18
 
13
19
  To install the Ruby client library using Rubygems:
14
20
 
15
- $ gem install nexmo
21
+ gem install nexmo
22
+
23
+ Alternatively you can clone the repository:
16
24
 
17
- Alternatively you can clone the repo or download the source.
25
+ git clone git@github.com:Nexmo/nexmo-ruby.git
18
26
 
19
27
 
20
28
  Usage
@@ -27,7 +35,7 @@ For example:
27
35
  ```ruby
28
36
  require 'nexmo'
29
37
 
30
- nexmo = Nexmo::Client.new
38
+ client = Nexmo::Client.new
31
39
  ```
32
40
 
33
41
  Alternatively you can specify your credentials directly using the `key`
@@ -36,39 +44,148 @@ and `secret` options:
36
44
  ```ruby
37
45
  require 'nexmo'
38
46
 
39
- nexmo = Nexmo::Client.new(key: 'YOUR-API-KEY', secret: 'YOUR-API-SECRET')
47
+ client = Nexmo::Client.new(key: 'YOUR-API-KEY', secret: 'YOUR-API-SECRET')
40
48
  ```
41
49
 
42
50
 
43
51
  Examples
44
52
  --------
45
53
 
46
- ### Sending A Message
54
+ ### Sending a message
55
+
56
+ To use [Nexmo's SMS API][doc_sms] to send an SMS message, call the Nexmo::Client#send_message
57
+ method with a hash containing the API parameters. For example:
58
+
59
+ ```ruby
60
+ response = client.send_message(from: 'Ruby', to: 'YOUR NUMBER', text: 'Hello world')
61
+
62
+ response = response['messages'].first
63
+
64
+ if response['status'] == '0'
65
+ puts "Sent message #{response['message-id']}"
66
+
67
+ puts "Remaining balance is #{response['remaining-balance']}"
68
+ else
69
+ puts "Error: #{response['error-text']}"
70
+ end
71
+ ```
47
72
 
48
- Use [Nexmo's SMS API][doc_sms] to send an SMS message.
73
+ ### Fetching a message
49
74
 
50
- Call the send_message method with a hash containing the message parameters. For example:
75
+ You can retrieve a message log from the API using the ID of the message:
51
76
 
52
77
  ```ruby
53
- require 'nexmo'
78
+ message = client.get_message('02000000DA7C52E7')
79
+
80
+ puts "The body of the message was: #{message['body']}"
81
+ ```
82
+
83
+ ### Starting a verification
84
+
85
+ Nexmo's [Verify API][doc_verify] makes it easy to prove that a user has provided their
86
+ own phone number during signup, or implement second factor authentication during signin.
54
87
 
55
- nexmo = Nexmo::Client.new(key: 'YOUR API KEY', secret: 'YOUR API SECRET')
88
+ You can start the verification process by calling the start_verification method:
56
89
 
57
- response = nexmo.send_message(from: 'Ruby', to: 'YOUR NUMBER', text: 'Hello world')
90
+ ```ruby
91
+ response = client.start_verification(number: '441632960960', brand: 'MyApp')
58
92
 
59
- if response['messages'][0]['status'].zero?
60
- # success!
93
+ if response['status'] == '0'
94
+ puts "Started verification request_id=#{response['request_id']}"
61
95
  else
62
- # error response
96
+ puts "Error: #{response['error_text']}"
63
97
  end
64
98
  ```
65
99
 
100
+ The response contains a verification request id which you will need to
101
+ store temporarily (in the session, database, url etc).
102
+
103
+ ### Controlling a verification
104
+
105
+ Call the cancel_verification method with the verification request id
106
+ to cancel an in-progress verification:
107
+
108
+ ```ruby
109
+ client.cancel_verification('00e6c3377e5348cdaf567e1417c707a5')
110
+ ```
111
+
112
+ Call the trigger_next_verification_event method with the verification
113
+ request id to trigger the next attempt to send the confirmation code:
114
+
115
+ ```ruby
116
+ client.trigger_next_verification_event('00e6c3377e5348cdaf567e1417c707a5')
117
+ ```
118
+
119
+ The verification request id comes from the call to the start_verification method.
120
+
121
+ ### Checking a verification
122
+
123
+ Call the check_verification method with the verification request id and the
124
+ PIN code to complete the verification process:
125
+
126
+ ```ruby
127
+ response = client.check_verification('00e6c3377e5348cdaf567e1417c707a5', code: '1234')
128
+
129
+ if response['status'] == '0'
130
+ puts "Verification complete, event_id=#{response['event_id']}"
131
+ else
132
+ puts "Error: #{response['error_text']}"
133
+ end
134
+ ```
135
+
136
+ The verification request id comes from the call to the start_verification method.
137
+ The PIN code is entered into your application by the user.
138
+
139
+
140
+ API Coverage
141
+ ------------
142
+
143
+ * Account
144
+ * [X] Balance
145
+ * [X] Pricing
146
+ * [X] Settings
147
+ * [X] Top Up
148
+ * [X] Numbers
149
+ * [X] Search
150
+ * [X] Buy
151
+ * [X] Cancel
152
+ * [X] Update
153
+ * Number Insight
154
+ * [X] Basic
155
+ * [X] Standard
156
+ * [X] Advanced
157
+ * [ ] Webhook Notification
158
+ * Verify
159
+ * [X] Verify
160
+ * [X] Check
161
+ * [X] Search
162
+ * [X] Control
163
+ * Messaging
164
+ * [X] Send
165
+ * [ ] Delivery Receipt
166
+ * [ ] Inbound Messages
167
+ * [X] Search
168
+ * [X] Message
169
+ * [X] Messages
170
+ * [X] Rejections
171
+ * [X] US Short Codes
172
+ * [X] Two-Factor Authentication
173
+ * [X] Event Based Alerts
174
+ * [X] Sending Alerts
175
+ * [X] Campaign Subscription Management
176
+ * Voice
177
+ * [X] Outbound Calls
178
+ * [ ] Inbound Call
179
+ * [X] Text-To-Speech Call
180
+ * [X] Text-To-Speech Prompt
181
+
66
182
 
67
183
  License
68
184
  -------
69
185
 
70
186
  This library is released under the [MIT License][license]
71
187
 
72
- [signup]: http://nexmo.com?src=ruby-client-library
73
- [doc_sms]: https://docs.nexmo.com/messaging/sms-api
188
+ [signup]: https://dashboard.nexmo.com/sign-up?utm_source=DEV_REL&utm_medium=github&utm_campaign=ruby-client-library
189
+ [doc_sms]: https://docs.nexmo.com/messaging/sms-api?utm_source=DEV_REL&utm_medium=github&utm_campaign=ruby-client-library
190
+ [doc_verify]: https://docs.nexmo.com/verify/api-reference?utm_source=DEV_REL&utm_medium=github&utm_campaign=ruby-client-library
74
191
  [license]: LICENSE.txt
data/lib/nexmo.rb CHANGED
@@ -26,143 +26,179 @@ module Nexmo
26
26
  end
27
27
 
28
28
  def send_message(params)
29
- post("https://#@host/sms/json", params)
29
+ post(@host, '/sms/json', params)
30
30
  end
31
31
 
32
32
  def get_balance
33
- get("https://#@host/account/get-balance")
33
+ get(@host, '/account/get-balance')
34
34
  end
35
35
 
36
36
  def get_country_pricing(country_code)
37
- get("https://#@host/account/get-pricing/outbound", country: country_code)
37
+ get(@host, '/account/get-pricing/outbound', country: country_code)
38
38
  end
39
39
 
40
40
  def get_prefix_pricing(prefix)
41
- get("https://#@host/account/get-prefix-pricing/outbound", prefix: prefix)
41
+ get(@host, '/account/get-prefix-pricing/outbound', prefix: prefix)
42
+ end
43
+
44
+ def get_sms_pricing(number)
45
+ get(@host, '/account/get-phone-pricing/outbound/sms', phone: number)
46
+ end
47
+
48
+ def get_voice_pricing(number)
49
+ get(@host, '/account/get-phone-pricing/outbound/voice', phone: number)
42
50
  end
43
51
 
44
52
  def update_settings(params)
45
- post("https://#@host/account/settings", params)
53
+ post(@host, '/account/settings', params)
46
54
  end
47
55
 
48
56
  def topup(params)
49
- post("https://#@host/account/top-up", params)
57
+ post(@host, '/account/top-up', params)
50
58
  end
51
59
 
52
60
  def get_account_numbers(params)
53
- get("https://#@host/account/numbers", params)
61
+ get(@host, '/account/numbers', params)
54
62
  end
55
63
 
56
64
  def get_available_numbers(country_code, params = {})
57
- get("https://#@host/number/search", {country: country_code}.merge(params))
65
+ get(@host, '/number/search', {country: country_code}.merge(params))
58
66
  end
59
67
 
60
68
  def buy_number(params)
61
- post("https://#@host/number/buy", params)
69
+ post(@host, '/number/buy', params)
62
70
  end
63
71
 
64
72
  def cancel_number(params)
65
- post("https://#@host/number/cancel", params)
73
+ post(@host, '/number/cancel', params)
66
74
  end
67
75
 
68
76
  def update_number(params)
69
- post("https://#@host/number/update", params)
77
+ post(@host, '/number/update', params)
70
78
  end
71
79
 
72
80
  def get_message(id)
73
- get("https://#@host/search/message", id: id)
81
+ get(@host, '/search/message', id: id)
74
82
  end
75
83
 
76
84
  def get_message_rejections(params)
77
- get("https://#@host/search/rejections", params)
85
+ get(@host, '/search/rejections', params)
78
86
  end
79
87
 
80
88
  def search_messages(params)
81
- get("https://#@host/search/messages", Hash === params ? params : {ids: Array(params)})
89
+ get(@host, '/search/messages', Hash === params ? params : {ids: Array(params)})
82
90
  end
83
91
 
84
92
  def send_ussd_push_message(params)
85
- post("https://#@host/ussd/json", params)
93
+ post(@host, '/ussd/json', params)
86
94
  end
87
95
 
88
96
  def send_ussd_prompt_message(params)
89
- post("https://#@host/ussd-prompt/json", params)
97
+ post(@host, '/ussd-prompt/json', params)
90
98
  end
91
99
 
92
100
  def send_2fa_message(params)
93
- post("https://#@host/sc/us/2fa/json", params)
101
+ post(@host, '/sc/us/2fa/json', params)
94
102
  end
95
103
 
96
104
  def send_event_alert_message(params)
97
- post("https://#@host/sc/us/alert/json", params)
105
+ post(@host, '/sc/us/alert/json', params)
98
106
  end
99
107
 
100
108
  def send_marketing_message(params)
101
- post("https://#@host/sc/us/marketing/json", params)
109
+ post(@host, '/sc/us/marketing/json', params)
110
+ end
111
+
112
+ def get_event_alert_numbers
113
+ get(@host, '/sc/us/alert/opt-in/query/json')
114
+ end
115
+
116
+ def resubscribe_event_alert_number(params)
117
+ post(@host, '/sc/us/alert/opt-in/manage/json', params)
102
118
  end
103
119
 
104
120
  def initiate_call(params)
105
- post("https://#@host/call/json", params)
121
+ post(@host, '/call/json', params)
106
122
  end
107
123
 
108
124
  def initiate_tts_call(params)
109
- post("https://#@api_host/tts/json", params)
125
+ post(@api_host, '/tts/json', params)
110
126
  end
111
127
 
112
128
  def initiate_tts_prompt_call(params)
113
- post("https://#@api_host/tts-prompt/json", params)
129
+ post(@api_host, '/tts-prompt/json', params)
130
+ end
131
+
132
+ def start_verification(params)
133
+ post(@api_host, '/verify/json', params)
114
134
  end
115
135
 
116
136
  def send_verification_request(params)
117
- post("https://#@api_host/verify/json", params)
137
+ post(@api_host, '/verify/json', params)
138
+ end
139
+
140
+ def check_verification(request_id, params)
141
+ post(@api_host, '/verify/check/json', params.merge(request_id: request_id))
118
142
  end
119
143
 
120
144
  def check_verification_request(params)
121
- post("https://#@api_host/verify/check/json", params)
145
+ post(@api_host, '/verify/check/json', params)
146
+ end
147
+
148
+ def get_verification(request_id)
149
+ get(@api_host, '/verify/search/json', request_id: request_id)
122
150
  end
123
151
 
124
152
  def get_verification_request(id)
125
- get("https://#@api_host/verify/search/json", request_id: id)
153
+ get(@api_host, '/verify/search/json', request_id: id)
154
+ end
155
+
156
+ def cancel_verification(request_id)
157
+ post(@api_host, '/verify/control/json', request_id: request_id, cmd: 'cancel')
158
+ end
159
+
160
+ def trigger_next_verification_event(request_id)
161
+ post(@api_host, '/verify/control/json', request_id: request_id, cmd: 'trigger_next_event')
126
162
  end
127
163
 
128
164
  def control_verification_request(params)
129
- post("https://#@api_host/verify/control/json", params)
165
+ post(@api_host, '/verify/control/json', params)
130
166
  end
131
167
 
132
168
  def get_basic_number_insight(params)
133
- get("https://#@api_host/number/format/json", params)
169
+ get(@api_host, '/number/format/json', params)
134
170
  end
135
171
 
136
172
  def get_number_insight(params)
137
- get("https://#@api_host/number/lookup/json", params)
173
+ get(@api_host, '/number/lookup/json', params)
138
174
  end
139
175
 
140
176
  def request_number_insight(params)
141
- post("https://#@host/ni/json", params)
177
+ post(@host, '/ni/json', params)
142
178
  end
143
179
 
144
180
  private
145
181
 
146
182
  USER_AGENT = "ruby-nexmo/#{VERSION}/#{RUBY_VERSION}"
147
183
 
148
- def get(uri, params = {})
149
- uri = URI(uri)
184
+ def get(host, request_uri, params = {})
185
+ uri = URI('https://' + host + request_uri)
150
186
  uri.query = query_string(params.merge(api_key: @key, api_secret: @secret))
151
187
 
152
188
  message = Net::HTTP::Get.new(uri.request_uri)
153
189
  message['User-Agent'] = USER_AGENT
154
190
 
155
- parse(request(uri, message), uri.host)
191
+ parse(request(uri, message), host)
156
192
  end
157
193
 
158
- def post(uri, params)
159
- uri = URI(uri)
194
+ def post(host, request_uri, params)
195
+ uri = URI('https://' + host + request_uri)
160
196
 
161
197
  message = Net::HTTP::Post.new(uri.request_uri)
162
198
  message.form_data = params.merge(api_key: @key, api_secret: @secret)
163
199
  message['User-Agent'] = USER_AGENT
164
200
 
165
- parse(request(uri, message), uri.host)
201
+ parse(request(uri, message), host)
166
202
  end
167
203
 
168
204
  def request(uri, message)
data/lib/nexmo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nexmo
2
- VERSION = '4.1.0'
2
+ VERSION = '4.2.0'
3
3
  end
data/spec/nexmo_spec.rb CHANGED
@@ -3,299 +3,313 @@ require 'webmock/minitest'
3
3
  require 'nexmo'
4
4
 
5
5
  describe 'Nexmo::Client' do
6
- def json_response_body(content)
7
- {headers: {'Content-Type' => 'application/json;charset=utf-8'}, body: content}
8
- end
9
-
10
6
  before do
7
+ @api_key = 'api_key_xxx'
8
+
9
+ @api_secret = 'api_secret_xxx'
10
+
11
11
  @base_url = 'https://rest.nexmo.com'
12
12
 
13
13
  @api_base_url = 'https://api.nexmo.com'
14
14
 
15
- @form_urlencoded_data = {body: /(.+?)=(.+?)(&(.+?)=(.+?))+/, headers: {'Content-Type' => 'application/x-www-form-urlencoded'}}
16
-
17
- @json_response_body = json_response_body('{"key":"value"}')
15
+ @response_body = {body: '{"key":"value"}', headers: {'Content-Type' => 'application/json;charset=utf-8'}}
18
16
 
19
- @json_response_object = {'key' => 'value'}
17
+ @response_object = {'key' => 'value'}
20
18
 
21
19
  @example_message_hash = {from: 'ruby', to: 'number', text: 'Hey!'}
22
20
 
23
- @client = Nexmo::Client.new(key: 'key', secret: 'secret')
21
+ @client = Nexmo::Client.new(key: @api_key, secret: @api_secret)
24
22
  end
25
23
 
26
24
  describe 'send_message method' do
27
- it 'posts to the sms json resource and returns the response message object' do
28
- response_body = json_response_body('{"messages":[{"status":0,"message-id":"id"}]}')
29
-
30
- stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(response_body)
25
+ it 'posts to the sms resource and returns the response object' do
26
+ expect_post "#@base_url/sms/json", "from=ruby&to=number&text=Hey!&api_key=#@api_key&api_secret=#@api_secret"
31
27
 
32
- @client.send_message(@example_message_hash).must_equal({'messages' => [{'status' => 0, 'message-id' => 'id'}]})
28
+ @client.send_message(@example_message_hash).must_equal(@response_object)
33
29
  end
34
30
  end
35
31
 
36
32
  describe 'get_balance method' do
37
33
  it 'fetches the account balance resource and returns the response object' do
38
- url = "#@base_url/account/get-balance?api_key=key&api_secret=secret"
34
+ expect_get "#@base_url/account/get-balance?api_key=#@api_key&api_secret=#@api_secret"
39
35
 
40
- stub_request(:get, url).to_return(@json_response_body)
41
-
42
- @client.get_balance.must_equal(@json_response_object)
36
+ @client.get_balance.must_equal(@response_object)
43
37
  end
44
38
  end
45
39
 
46
40
  describe 'get_country_pricing method' do
47
41
  it 'fetches the outbound pricing resource for the given country and returns the response object' do
48
- url = "#@base_url/account/get-pricing/outbound?api_key=key&api_secret=secret&country=CA"
49
-
50
- stub_request(:get, url).to_return(@json_response_body)
42
+ expect_get "#@base_url/account/get-pricing/outbound?api_key=#@api_key&api_secret=#@api_secret&country=CA"
51
43
 
52
- @client.get_country_pricing(:CA).must_equal(@json_response_object)
44
+ @client.get_country_pricing(:CA).must_equal(@response_object)
53
45
  end
54
46
  end
55
47
 
56
48
  describe 'get_prefix_pricing method' do
57
49
  it 'fetches the outbound pricing resource for the given prefix and returns the response object' do
58
- url = "#@base_url/account/get-prefix-pricing/outbound?api_key=key&api_secret=secret&prefix=44"
50
+ expect_get "#@base_url/account/get-prefix-pricing/outbound?api_key=#@api_key&api_secret=#@api_secret&prefix=44"
51
+
52
+ @client.get_prefix_pricing(44).must_equal(@response_object)
53
+ end
54
+ end
59
55
 
60
- stub_request(:get, url).to_return(@json_response_body)
56
+ describe 'get_sms_pricing method' do
57
+ it 'fetches the outbound sms pricing resource for the given phone number and returns the response object' do
58
+ expect_get "#@base_url/account/get-phone-pricing/outbound/sms?api_key=#@api_key&api_secret=#@api_secret&phone=447525856424"
61
59
 
62
- @client.get_prefix_pricing(44).must_equal(@json_response_object)
60
+ @client.get_sms_pricing('447525856424').must_equal(@response_object)
61
+ end
62
+ end
63
+
64
+ describe 'get_voice_pricing method' do
65
+ it 'fetches the outbound voice pricing resource for the given phone number and returns the response object' do
66
+ expect_get "#@base_url/account/get-phone-pricing/outbound/voice?api_key=#@api_key&api_secret=#@api_secret&phone=447525856424"
67
+
68
+ @client.get_voice_pricing('447525856424').must_equal(@response_object)
63
69
  end
64
70
  end
65
71
 
66
72
  describe 'update_settings method' do
67
73
  it 'updates the account settings resource with the given parameters and returns the response object' do
68
- url = "#@base_url/account/settings"
69
-
70
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
74
+ expect_post "#@base_url/account/settings", "api_key=#@api_key&api_secret=#@api_secret&moCallBackUrl=http://example.com/callback"
71
75
 
72
- @client.update_settings(moCallBackUrl: 'http://example.com/callback').must_equal(@json_response_object)
76
+ @client.update_settings(moCallBackUrl: 'http://example.com/callback').must_equal(@response_object)
73
77
  end
74
78
  end
75
79
 
76
80
  describe 'topup method' do
77
81
  it 'updates the account top-up resource with the given parameters and returns the response object' do
78
- url = "#@base_url/account/top-up"
82
+ expect_post "#@base_url/account/top-up", "api_key=#@api_key&api_secret=#@api_secret&trx=00X123456Y7890123Z"
79
83
 
80
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
81
-
82
- @client.topup(trx: '00X123456Y7890123Z').must_equal(@json_response_object)
84
+ @client.topup(trx: '00X123456Y7890123Z').must_equal(@response_object)
83
85
  end
84
86
  end
85
87
 
86
88
  describe 'get_account_numbers method' do
87
89
  it 'fetches the account numbers resource with the given parameters and returns the response object' do
88
- url = "#@base_url/account/numbers?api_key=key&api_secret=secret&size=25&pattern=33"
89
-
90
- stub_request(:get, url).to_return(@json_response_body)
90
+ expect_get "#@base_url/account/numbers?api_key=#@api_key&api_secret=#@api_secret&size=25&pattern=33"
91
91
 
92
- @client.get_account_numbers(size: 25, pattern: 33).must_equal(@json_response_object)
92
+ @client.get_account_numbers(size: 25, pattern: 33).must_equal(@response_object)
93
93
  end
94
94
  end
95
95
 
96
96
  describe 'buy_number method' do
97
97
  it 'purchases the number requested with the given parameters and returns the response object' do
98
- url = "#@base_url/number/buy"
99
-
100
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
98
+ expect_post "#@base_url/number/buy", "api_key=#@api_key&api_secret=#@api_secret&country=US&msisdn=number"
101
99
 
102
- @client.buy_number(country: 'US', msisdn: 'number').must_equal(@json_response_object)
100
+ @client.buy_number(country: 'US', msisdn: 'number').must_equal(@response_object)
103
101
  end
104
102
  end
105
103
 
106
104
  describe 'cancel_number method' do
107
105
  it 'cancels the number requested with the given parameters and returns the response object' do
108
- url = "#@base_url/number/cancel"
106
+ expect_post "#@base_url/number/cancel", "api_key=#@api_key&api_secret=#@api_secret&country=US&msisdn=number"
109
107
 
110
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
111
-
112
- @client.cancel_number(country: 'US', msisdn: 'number').must_equal(@json_response_object)
108
+ @client.cancel_number(country: 'US', msisdn: 'number').must_equal(@response_object)
113
109
  end
114
110
  end
115
111
 
116
112
  describe 'update_number method' do
117
113
  it 'updates the number requested with the given parameters and returns the response object' do
118
- url = "#@base_url/number/update"
119
-
120
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
114
+ expect_post "#@base_url/number/update", "api_key=#@api_key&api_secret=#@api_secret&country=US&msisdn=number&moHttpUrl=callback"
121
115
 
122
- @client.update_number(country: 'US', msisdn: 'number', moHttpUrl: 'callback').must_equal(@json_response_object)
116
+ @client.update_number(country: 'US', msisdn: 'number', moHttpUrl: 'callback').must_equal(@response_object)
123
117
  end
124
118
  end
125
119
 
126
120
  describe 'get_message method' do
127
121
  it 'fetches the message search resource for the given message id and returns the response object' do
128
- url = "#@base_url/search/message?api_key=key&api_secret=secret&id=00A0B0C0"
122
+ expect_get "#@base_url/search/message?api_key=#@api_key&api_secret=#@api_secret&id=00A0B0C0"
129
123
 
130
- stub_request(:get, url).to_return(@json_response_body)
131
-
132
- @client.get_message('00A0B0C0').must_equal(@json_response_object)
124
+ @client.get_message('00A0B0C0').must_equal(@response_object)
133
125
  end
134
126
  end
135
127
 
136
128
  describe 'get_message_rejections method' do
137
129
  it 'fetches the message rejections resource with the given parameters and returns the response object' do
138
- url = "#@base_url/search/rejections?api_key=key&api_secret=secret&date=YYYY-MM-DD"
139
-
140
- stub_request(:get, url).to_return(@json_response_body)
130
+ expect_get "#@base_url/search/rejections?api_key=#@api_key&api_secret=#@api_secret&date=YYYY-MM-DD"
141
131
 
142
- @client.get_message_rejections(date: 'YYYY-MM-DD').must_equal(@json_response_object)
132
+ @client.get_message_rejections(date: 'YYYY-MM-DD').must_equal(@response_object)
143
133
  end
144
134
  end
145
135
 
146
136
  describe 'search_messages method' do
147
137
  it 'fetches the search messages resource with the given parameters and returns the response object' do
148
- url = "#@base_url/search/messages?api_key=key&api_secret=secret&date=YYYY-MM-DD&to=1234567890"
149
-
150
- stub_request(:get, url).to_return(@json_response_body)
138
+ expect_get "#@base_url/search/messages?api_key=#@api_key&api_secret=#@api_secret&date=YYYY-MM-DD&to=1234567890"
151
139
 
152
- @client.search_messages(date: 'YYYY-MM-DD', to: 1234567890).must_equal(@json_response_object)
140
+ @client.search_messages(date: 'YYYY-MM-DD', to: 1234567890).must_equal(@response_object)
153
141
  end
154
142
 
155
143
  it 'should encode a non hash argument as a list of ids' do
156
- url = "#@base_url/search/messages?api_key=key&api_secret=secret&ids=id1&ids=id2"
157
-
158
- stub_request(:get, url).to_return(@json_response_body)
144
+ expect_get "#@base_url/search/messages?api_key=#@api_key&api_secret=#@api_secret&ids=id1&ids=id2"
159
145
 
160
146
  @client.search_messages(%w(id1 id2))
161
147
  end
162
148
  end
163
149
 
164
150
  describe 'send_ussd_push_message method' do
165
- it 'posts to the ussd json resource and returns the response object' do
166
- url = "#@base_url/ussd/json"
167
-
168
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
151
+ it 'posts to the ussd resource and returns the response object' do
152
+ expect_post "#@base_url/ussd/json", "api_key=#@api_key&api_secret=#@api_secret&from=MyCompany20&to=447525856424&text=Hello"
169
153
 
170
154
  @client.send_ussd_push_message(from: 'MyCompany20', to: '447525856424', text: 'Hello')
171
155
  end
172
156
  end
173
157
 
174
158
  describe 'send_ussd_prompt_message method' do
175
- it 'posts to the ussd prompt json resource and returns the response object' do
176
- url = "#@base_url/ussd-prompt/json"
159
+ it 'posts to the ussd prompt resource and returns the response object' do
160
+ expect_post "#@base_url/ussd-prompt/json", "api_key=#@api_key&api_secret=#@api_secret&from=virtual-number&to=447525856424&text=Hello"
177
161
 
178
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
179
-
180
- @client.send_ussd_prompt_message(from: 'Nexmo long virtual number', to: '447525856424', text: 'Hello')
162
+ @client.send_ussd_prompt_message(from: 'virtual-number', to: '447525856424', text: 'Hello')
181
163
  end
182
164
  end
183
165
 
184
166
  describe 'send_2fa_message method' do
185
- it 'posts to the short code two factor authentication json resource and returns the response object' do
186
- url = "#@base_url/sc/us/2fa/json"
187
-
188
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
167
+ it 'posts to the short code two factor authentication resource and returns the response object' do
168
+ expect_post "#@base_url/sc/us/2fa/json", "api_key=#@api_key&api_secret=#@api_secret&to=16365553226&pin=1234"
189
169
 
190
170
  @client.send_2fa_message(to: '16365553226', pin: 1234)
191
171
  end
192
172
  end
193
173
 
194
174
  describe 'send_event_alert_message method' do
195
- it 'posts to the short code alert json resource and returns the response object' do
196
- url = "#@base_url/sc/us/alert/json"
197
-
198
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
175
+ it 'posts to the short code alert resource and returns the response object' do
176
+ expect_post "#@base_url/sc/us/alert/json", "api_key=#@api_key&api_secret=#@api_secret&to=16365553226&server=host&link=http://example.com/"
199
177
 
200
178
  @client.send_event_alert_message(to: '16365553226', server: 'host', link: 'http://example.com/')
201
179
  end
202
180
  end
203
181
 
204
182
  describe 'send_marketing_message method' do
205
- it 'posts to the short code marketing json resource and returns the response object' do
206
- url = "#@base_url/sc/us/marketing/json"
207
-
208
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
183
+ it 'posts to the short code marketing resource and returns the response object' do
184
+ expect_post "#@base_url/sc/us/marketing/json", "api_key=#@api_key&api_secret=#@api_secret&from=666&to=16365553226&keyword=NEXMO&text=Hello"
209
185
 
210
186
  @client.send_marketing_message(from: '666', to: '16365553226', keyword: 'NEXMO', text: 'Hello')
211
187
  end
212
188
  end
213
189
 
214
- describe 'initiate_call method' do
215
- it 'posts to the call json resource and returns the response object' do
216
- url = "#@base_url/call/json"
190
+ describe 'get_event_alert_numbers method' do
191
+ it 'fetches the short code alert opt-in query resource and returns the response object' do
192
+ expect_get "#@base_url/sc/us/alert/opt-in/query/json?api_key=#@api_key&api_secret=#@api_secret"
217
193
 
218
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
194
+ @client.get_event_alert_numbers
195
+ end
196
+ end
197
+
198
+ describe 'resubscribe_event_alert_number method' do
199
+ it 'posts to the short code alert opt-in manage resource and returns the response object' do
200
+ expect_post "#@base_url/sc/us/alert/opt-in/manage/json", "api_key=#@api_key&api_secret=#@api_secret&msisdn=441632960960"
201
+
202
+ @client.resubscribe_event_alert_number(msisdn: '441632960960')
203
+ end
204
+ end
205
+
206
+ describe 'initiate_call method' do
207
+ it 'posts to the call resource and returns the response object' do
208
+ expect_post "#@base_url/call/json", "api_key=#@api_key&api_secret=#@api_secret&to=16365553226&answer_url=http://example.com/answer"
219
209
 
220
210
  @client.initiate_call(to: '16365553226', answer_url: 'http://example.com/answer')
221
211
  end
222
212
  end
223
213
 
224
214
  describe 'initiate_tts_call method' do
225
- it 'posts to the tts json resource and returns the response object' do
226
- url = "#@api_base_url/tts/json"
227
-
228
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
215
+ it 'posts to the tts resource and returns the response object' do
216
+ expect_post "#@api_base_url/tts/json", "api_key=#@api_key&api_secret=#@api_secret&to=16365553226&text=Hello"
229
217
 
230
218
  @client.initiate_tts_call(to: '16365553226', text: 'Hello')
231
219
  end
232
220
  end
233
221
 
234
222
  describe 'initiate_tts_prompt_call method' do
235
- it 'posts to the tts prompt json resource and returns the response object' do
236
- url = "#@api_base_url/tts-prompt/json"
237
-
238
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
223
+ it 'posts to the tts prompt resource and returns the response object' do
224
+ expect_post "#@api_base_url/tts-prompt/json", "api_key=#@api_key&api_secret=#@api_secret&to=16365553226&text=Hello&max_digits=4&bye_text=Goodbye"
239
225
 
240
226
  @client.initiate_tts_prompt_call(to: '16365553226', text: 'Hello', max_digits: 4, bye_text: 'Goodbye')
241
227
  end
242
228
  end
243
229
 
244
- describe 'send_verification_request method' do
230
+ describe 'start_verification method' do
245
231
  it 'posts to the verify json resource and returns the response object' do
246
- url = "#@api_base_url/verify/json"
232
+ expect_post "#@api_base_url/verify/json", "api_key=#@api_key&api_secret=#@api_secret&number=447525856424&brand=MyApp"
247
233
 
248
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
234
+ @client.start_verification(number: '447525856424', brand: 'MyApp')
235
+ end
236
+ end
237
+
238
+ describe 'send_verification_request method' do
239
+ it 'posts to the verify resource and returns the response object' do
240
+ expect_post "#@api_base_url/verify/json", "api_key=#@api_key&api_secret=#@api_secret&number=447525856424&brand=MyApp"
249
241
 
250
242
  @client.send_verification_request(number: '447525856424', brand: 'MyApp')
251
243
  end
252
244
  end
253
245
 
254
- describe 'check_verification_request method' do
255
- it 'posts to the verify json resource and returns the response object' do
256
- url = "#@api_base_url/verify/check/json"
246
+ describe 'check_verification method' do
247
+ it 'posts to the verify check resource and returns the response object' do
248
+ expect_post "#@api_base_url/verify/check/json", "api_key=#@api_key&api_secret=#@api_secret&request_id=8g88g88eg8g8gg9g90&code=123445"
249
+
250
+ @client.check_verification('8g88g88eg8g8gg9g90', code: '123445')
251
+ end
252
+ end
257
253
 
258
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
254
+ describe 'check_verification_request method' do
255
+ it 'posts to the verify check resource and returns the response object' do
256
+ expect_post "#@api_base_url/verify/check/json", "api_key=#@api_key&api_secret=#@api_secret&request_id=8g88g88eg8g8gg9g90&code=123445"
259
257
 
260
258
  @client.check_verification_request(request_id: '8g88g88eg8g8gg9g90', code: '123445')
261
259
  end
262
260
  end
263
261
 
264
- describe 'get_verification_request method' do
262
+ describe 'get_verification method' do
265
263
  it 'fetches the verify search resource with the given request id and returns the response object' do
266
- url = "#@api_base_url/verify/search/json?api_key=key&api_secret=secret&request_id=8g88g88eg8g8gg9g90"
264
+ expect_get "#@api_base_url/verify/search/json?api_key=#@api_key&api_secret=#@api_secret&request_id=8g88g88eg8g8gg9g90"
265
+
266
+ @client.get_verification('8g88g88eg8g8gg9g90')
267
+ end
268
+ end
267
269
 
268
- stub_request(:get, url).to_return(@json_response_body)
270
+ describe 'get_verification_request method' do
271
+ it 'fetches the verify search resource with the given request id and returns the response object' do
272
+ expect_get "#@api_base_url/verify/search/json?api_key=#@api_key&api_secret=#@api_secret&request_id=8g88g88eg8g8gg9g90"
269
273
 
270
274
  @client.get_verification_request('8g88g88eg8g8gg9g90')
271
275
  end
272
276
  end
273
277
 
274
- describe 'control_verification_request method' do
275
- it 'posts to the control json resource and returns the response object' do
276
- url = "#@api_base_url/verify/control/json"
278
+ describe 'cancel_verification method' do
279
+ it 'posts to the verify control resource and returns the response object' do
280
+ expect_post "#@api_base_url/verify/control/json", "api_key=#@api_key&api_secret=#@api_secret&request_id=8g88g88eg8g8gg9g90&cmd=cancel"
281
+
282
+ @client.cancel_verification('8g88g88eg8g8gg9g90')
283
+ end
284
+ end
285
+
286
+ describe 'trigger_next_verification_event method' do
287
+ it 'posts to the verify control resource and returns the response object' do
288
+ expect_post "#@api_base_url/verify/control/json", "api_key=#@api_key&api_secret=#@api_secret&request_id=8g88g88eg8g8gg9g90&cmd=trigger_next_event"
289
+
290
+ @client.trigger_next_verification_event('8g88g88eg8g8gg9g90')
291
+ end
292
+ end
277
293
 
278
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
294
+ describe 'control_verification_request method' do
295
+ it 'posts to the verify control resource and returns the response object' do
296
+ expect_post "#@api_base_url/verify/control/json", "api_key=#@api_key&api_secret=#@api_secret&request_id=8g88g88eg8g8gg9g90&cmd=cancel"
279
297
 
280
298
  @client.control_verification_request(request_id: '8g88g88eg8g8gg9g90', cmd: 'cancel')
281
299
  end
282
300
  end
283
301
 
284
302
  describe 'get_basic_number_insight method' do
285
- it 'fetches the number format json resource and returns the response object' do
286
- url = "#@api_base_url/number/format/json?api_key=key&api_secret=secret&number=447525856424"
287
-
288
- stub_request(:get, url).to_return(@json_response_body)
303
+ it 'fetches the number format resource and returns the response object' do
304
+ expect_get "#@api_base_url/number/format/json?api_key=#@api_key&api_secret=#@api_secret&number=447525856424"
289
305
 
290
306
  @client.get_basic_number_insight(number: '447525856424')
291
307
  end
292
308
  end
293
309
 
294
310
  describe 'get_number_insight method' do
295
- it 'fetches the number lookup json resource and returns the response object' do
296
- url = "#@api_base_url/number/lookup/json?api_key=key&api_secret=secret&number=447525856424"
297
-
298
- stub_request(:get, url).to_return(@json_response_body)
311
+ it 'fetches the number lookup resource and returns the response object' do
312
+ expect_get "#@api_base_url/number/lookup/json?api_key=#@api_key&api_secret=#@api_secret&number=447525856424"
299
313
 
300
314
  @client.get_number_insight(number: '447525856424')
301
315
  end
@@ -303,9 +317,7 @@ describe 'Nexmo::Client' do
303
317
 
304
318
  describe 'request_number_insight method' do
305
319
  it 'posts to the number insight resource and returns the response object' do
306
- url = "#@base_url/ni/json"
307
-
308
- stub_request(:post, url).with(@form_urlencoded_data).to_return(@json_response_body)
320
+ expect_post "#@base_url/ni/json", "api_key=#@api_key&api_secret=#@api_secret&number=447525856424&callback=https://example.com"
309
321
 
310
322
  @client.request_number_insight(number: '447525856424', callback: 'https://example.com')
311
323
  end
@@ -314,50 +326,60 @@ describe 'Nexmo::Client' do
314
326
  it 'includes a user-agent header with the library version number and ruby version number' do
315
327
  headers = {'User-Agent' => "ruby-nexmo/#{Nexmo::VERSION}/#{RUBY_VERSION}"}
316
328
 
317
- stub_request(:post, "#@base_url/sms/json").with(headers: headers).to_return(@json_response_body)
329
+ stub_request(:post, "#@base_url/sms/json").with(headers: headers).to_return(@response_body)
318
330
 
319
331
  @client.send_message(@example_message_hash)
320
332
  end
321
333
 
322
334
  it 'raises an authentication error exception if the response code is 401' do
323
- stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(status: 401)
335
+ stub_request(:post, "#@base_url/sms/json").to_return(status: 401)
324
336
 
325
337
  proc { @client.send_message(@example_message_hash) }.must_raise(Nexmo::AuthenticationError)
326
338
  end
327
339
 
328
340
  it 'raises a client error exception if the response code is 4xx' do
329
- stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(status: 400)
341
+ stub_request(:post, "#@base_url/sms/json").to_return(status: 400)
330
342
 
331
343
  proc { @client.send_message(@example_message_hash) }.must_raise(Nexmo::ClientError)
332
344
  end
333
345
 
334
346
  it 'raises a server error exception if the response code is 5xx' do
335
- stub_request(:post, "#@base_url/sms/json").with(@form_urlencoded_data).to_return(status: 500)
347
+ stub_request(:post, "#@base_url/sms/json").to_return(status: 500)
336
348
 
337
349
  proc { @client.send_message(@example_message_hash) }.must_raise(Nexmo::ServerError)
338
350
  end
339
351
 
340
352
  it 'provides an option for specifying a different hostname to connect to' do
341
- url = "https://rest-sandbox.nexmo.com/account/get-balance?api_key=key&api_secret=secret"
342
-
343
- request = stub_request(:get, url).to_return(@json_response_body)
353
+ expect_get "https://rest-sandbox.nexmo.com/account/get-balance?api_key=#@api_key&api_secret=#@api_secret"
344
354
 
345
- @client = Nexmo::Client.new(key: 'key', secret: 'secret', host: 'rest-sandbox.nexmo.com')
355
+ @client = Nexmo::Client.new(key: @api_key, secret: @api_secret, host: 'rest-sandbox.nexmo.com')
346
356
 
347
357
  @client.get_balance
348
-
349
- assert_requested(request)
350
358
  end
351
359
 
352
360
  it 'provides an option for specifying a different api hostname to connect to' do
353
- url = "https://debug.example.com/number/format/json?api_key=key&api_secret=secret&number=447525856424"
354
-
355
- request = stub_request(:get, url).to_return(@json_response_body)
361
+ expect_get "https://debug.example.com/number/format/json?api_key=#@api_key&api_secret=#@api_secret&number=447525856424"
356
362
 
357
- @client = Nexmo::Client.new(key: 'key', secret: 'secret', api_host: 'debug.example.com')
363
+ @client = Nexmo::Client.new(key: @api_key, secret: @api_secret, api_host: 'debug.example.com')
358
364
 
359
365
  @client.get_basic_number_insight(number: '447525856424')
366
+ end
367
+
368
+ private
369
+
370
+ def expect_get(url)
371
+ @request = stub_request(:get, url).to_return(@response_body)
372
+ end
373
+
374
+ def expect_post(url, data)
375
+ body = WebMock::Util::QueryMapper.query_to_values(data)
376
+
377
+ headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
378
+
379
+ @request = stub_request(:post, url).with(body: body, headers: headers).to_return(@response_body)
380
+ end
360
381
 
361
- assert_requested(request)
382
+ after do
383
+ assert_requested(@request) if defined?(@request)
362
384
  end
363
385
  end
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.1.0
4
+ version: 4.2.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-05-05 00:00:00.000000000 Z
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake