Skip to content

Commit

Permalink
Merge pull request #239 from Vonage/dev
Browse files Browse the repository at this point in the history
Release 7.7.2
  • Loading branch information
superchilled authored Jul 25, 2022
2 parents 8984d02 + 4169c8a commit 4e43353
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 7.7.2

* Fixes bug with auto-pagination for Numbers. See PR [#236](https://github.com/Vonage/vonage-ruby-sdk/pull/236).

* Adds support for `PATCH` requests to be passed to the `Logger#log_request_info` method. See PR [#237](https://github.com/Vonage/vonage-ruby-sdk/pull/237).

# 7.7.1

* Adds support for `PATCH` requests to be passed to the `JSON::update` method See PR [#230](https://github.com/Vonage/vonage-ruby-sdk/pull/230).
Expand Down
2 changes: 1 addition & 1 deletion lib/vonage/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(logger)
def_delegator :@logger, name, name
end

sig { params(request: T.any(Net::HTTP::Post, Net::HTTP::Get, Net::HTTP::Delete, Net::HTTP::Put)).void }
sig { params(request: T.any(Net::HTTP::Post, Net::HTTP::Get, Net::HTTP::Delete, Net::HTTP::Put, Net::HTTP::Patch)).void }
def log_request_info(request)
@logger = T.let(@logger, T.nilable(T.any(::Logger, Vonage::Logger)))

Expand Down
4 changes: 2 additions & 2 deletions lib/vonage/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ def request(path, params: nil, type: Get, response_class: Response, &block)
response = make_request!(request, &block)

if auto_advance
iterable_request(path, response: response, response_class: response_class, &block)
iterable_request(path, response: response, response_class: response_class, params: params, &block)
else
return if block

parse(response, response_class)
end
end

def iterable_request(path, response: nil, response_class: nil, &block)
def iterable_request(path, response: nil, response_class: nil, params: {}, &block)
json_response = ::JSON.parse(response.body)
response = parse(response, response_class)
remainder = remaining_count(json_response)
Expand Down
30 changes: 27 additions & 3 deletions lib/vonage/numbers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class Numbers < Namespace
#
# @option params [Integer] :index
# Page index.
#
#
# @option params [Boolean] :auto_advance
# Set this to `true` to auto-advance through all the pages in the record
# and collect all the data. The default is `false`.
#
#
# @param [Hash] params
#
# @return [ListResponse]
Expand Down Expand Up @@ -92,7 +92,7 @@ def list(params = nil)
# @option params [Boolean] :auto_advance
# Set this to `true` to auto-advance through all the pages in the record
# and collect all the data. The default is `false`.
#
#
# @param [Hash] params
#
# @return [ListResponse]
Expand Down Expand Up @@ -200,5 +200,29 @@ def cancel(params)
def update(params)
request('/number/update', params: camelcase(params), type: Post, response_class: Response)
end

private

# A specific implementation of iterable_request for Numbers, because the Numbers API
# handles pagination differently to other Vonage APIs
def iterable_request(path, response: nil, response_class: nil, params: {}, &block)
response = parse(response, response_class)
params[:index] = 1 unless params.has_key?(:index)
size = params.fetch(:size, 10)
count_from_index = response[:count] - ((params[:index] - 1) * size)

while count_from_index > response.numbers.size
params[:index] += 1
request = build_request(path: path, type: Get, params: params)

# Make request...
paginated_response = make_request!(request)
next_response = parse(paginated_response, response_class)

response.numbers.push(*next_response.numbers)
end

response
end
end
end
2 changes: 1 addition & 1 deletion lib/vonage/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# typed: strong

module Vonage
VERSION = '7.7.1'
VERSION = '7.7.2'
end
28 changes: 28 additions & 0 deletions test/vonage/numbers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ def test_search_method
assert_kind_of Vonage::Numbers::ListResponse, numbers.search(params)
end

def test_search_method_with_auto_advance
uri = 'https://rest.nexmo.com/number/search'

params = {country: country}

stub_request(:get, uri).with(query: params.merge(api_key_and_secret)).to_return(numbers_response_paginated_page_1)

stub_request(:get, uri).with(query: params.merge(api_key_and_secret.merge(index: 2))).to_return(numbers_response_paginated_page_2)

response = numbers.search(params.merge(auto_advance: true))

assert_kind_of Vonage::Numbers::ListResponse, response
assert_equal 14, response.numbers.length
end

def test_search_method_with_auto_advance_with_index_offset_by_one_page
uri = 'https://rest.nexmo.com/number/search'

params = {country: country}

stub_request(:get, uri).with(query: params.merge(api_key_and_secret.merge(index: 2))).to_return(numbers_response_paginated_page_2)

response = numbers.search(params.merge(auto_advance: true, index: 2))

assert_kind_of Vonage::Numbers::ListResponse, response
assert_equal 4, response.numbers.length
end

def test_buy_method
uri = 'https://rest.nexmo.com/number/buy'

Expand Down
184 changes: 180 additions & 4 deletions test/vonage/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,204 @@ def request(body: nil, query: nil, headers: {})

def voice_response
{
body: '{"_embedded": {"calls":[]}}',
body: '{"_embedded": {"calls":[]}}',
headers: response_headers
}
end

def applications_response
{
body: '{"_embedded": {"applications":[]}}',
body: '{"_embedded": {"applications":[]}}',
headers: response_headers
}
end

def secrets_response
{
body: '{"_embedded": {"secrets":[]}}',
body: '{"_embedded": {"secrets":[]}}',
headers: response_headers
}
end

def numbers_response
{
body: '{"numbers":[]}',
body: '{"numbers":[]}',
headers: response_headers
}
end

def numbers_response_paginated_page_1
{
body: '{
"count": 14,
"numbers":[
{
"country": "GB",
"msisdn": "447700900000",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900001",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900002",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900003",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900004",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900005",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900006",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900007",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900008",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900009",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
}
]
}',
headers: response_headers
}
end

def numbers_response_paginated_page_2
{
body: '{
"count": 14,
"numbers":[
{
"country": "GB",
"msisdn": "447700900010",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900011",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900012",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
},
{
"country": "GB",
"msisdn": "447700900013",
"type": "mobile-lvn",
"cost": "1.25",
"features": [
"VOICE",
"SMS",
"MMS"
]
}
]
}',
headers: response_headers
}
end
Expand Down

0 comments on commit 4e43353

Please sign in to comment.