Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup API: Change endpoint and response #143

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [4.14.0](https://github.com/plivo/plivo-ruby/releases/tag/v4.14.0) (2020-10-30)
- Change lookup API endpoint and response.

## [4.13.0](https://github.com/plivo/plivo-ruby/releases/tag/v4.13.0) (2020-09-30)
- Add support for Lookup API

Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Plivo Ruby SDK makes it simpler to integrate communications into your Ruby a
Add this line to your application's Gemfile:

```ruby
gem 'plivo', '>= 4.13.0'
gem 'plivo', '>= 4.14.0'
```

And then execute:
Expand Down Expand Up @@ -114,10 +114,7 @@ require 'plivo'
include Plivo

client = RestClient.new
resp = client.lookup.get(
'<number-here>',
"carrier"
)
resp = client.lookup.get('<number-here>')
```

### Generate Plivo XML
Expand Down
1 change: 1 addition & 0 deletions lib/plivo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module Base

CALLINSIGHTS_API_URL = 'https://stats.plivo.com'.freeze
PHLO_API_URL = 'https://phlorunner.plivo.com'.freeze
LOOKUP_API_URL = 'https://lookup.plivo.com'.freeze
end
end
24 changes: 23 additions & 1 deletion lib/plivo/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ def send_request(resource_path, method = 'GET', data = {}, timeout = nil, use_mu
else
process_response(method, response.to_hash)
end

elsif options[:is_lookup_request] == true
response = case method
when 'GET' then send_get(resource_path, data, timeout, is_lookup_request: options[:is_lookup_request])
else raise_invalid_request("#{method} not supported by Plivo, yet")
end
process_response(method, response.to_hash)
else
response = case method
when 'GET' then send_get(resource_path, data, timeout)
Expand Down Expand Up @@ -191,6 +196,18 @@ def configure_connection
faraday.adapter Faraday.default_adapter
end

@lookup_conn = Faraday.new(@lookup_base_uri) do |faraday|
faraday.headers = @headers

# DANGER: Basic auth should always come after headers, else
# The headers will replace the basic_auth

faraday.basic_auth(auth_id, auth_token)

faraday.proxy=@proxy_hash if @proxy_hash
faraday.response :json, content_type: /\bjson$/
faraday.adapter Faraday.default_adapter
end
end

def send_get(resource_path, data, timeout, options = nil)
Expand All @@ -210,6 +227,11 @@ def send_get(resource_path, data, timeout, options = nil)
req.url resource_path, data
req.options.timeout = timeout if timeout
end
elsif options[:is_lookup_request] == true
response = @lookup_conn.get do |req|
req.url resource_path, data
req.options.timeout = timeout if timeout
end
end
else
response = @conn.get do |req|
Expand Down
4 changes: 2 additions & 2 deletions lib/plivo/resources/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def initialize(client, resource_list_json = nil)
@_identifier_string = "phone_number"
super
# Override _resource_uri only after calling super
@_resource_uri = "/v1/Lookup/Number/"
@_resource_uri = "/v1/Number/"
end

##
Expand All @@ -80,7 +80,7 @@ def get(number, type = "carrier")
# overridden to ensure 'Account' and extra shash isn't added to URL path
def perform_get(identifier, params = nil)
valid_param?(:identifier, identifier, [String, Symbol], true)
response_json = @_client.send_request(@_resource_uri + identifier.to_s, "GET", params, nil, false, is_voice_request: @_is_voice_request)
response_json = @_client.send_request(@_resource_uri + identifier.to_s, "GET", params, nil, false, is_voice_request: @_is_voice_request, is_lookup_request: true)
@_resource_type.new(@_client, resource_json: response_json)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/plivo/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def configure_base_uri
@voice_base_uri_fallback_1 = Base::API_VOICE_FALLBACK_1
@voice_base_uri_fallback_2 = Base::API_VOICE_FALLBACK_2
@callinsights_base_uri = Base::CALLINSIGHTS_API_URL
@lookup_base_uri = Base::LOOKUP_API_URL
end

def configure_interfaces
Expand Down
2 changes: 1 addition & 1 deletion lib/plivo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Plivo
VERSION = "4.13.0".freeze
VERSION = "4.14.0".freeze
end
8 changes: 4 additions & 4 deletions spec/mocks/lookupGetResponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"phone_number": "+14154305555",
"country": {
"name": "United States",
"code_iso2": "US",
"code_iso3": "USA"
"iso2": "US",
"iso3": "USA"
},
"format": {
"e164": "+14154305555",
Expand All @@ -17,7 +17,7 @@
"mobile_network_code": "150",
"name": "Cingular Wireless",
"type": "mobile",
"ported": true
"ported": "yes"
},
"resource_uri": "/v1/Lookup/Number/+14154305555?type=carrier"
"resource_uri": "/v1/Number/+14154305555?type=carrier"
}
2 changes: 1 addition & 1 deletion spec/resource_lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"carrier"
)
expect(JSON.parse(to_json(response))).to eql(JSON.parse(contents))
compare_requests(uri: "/v1/Lookup/Number/+14154305555",
compare_requests(uri: "/v1/Number/+14154305555",
method: "GET",
data: nil)
expect(response.phone_number).to eql("+14154305555")
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
config.before(:each) do
@api = Plivo::RestClient.new('MAXXXXXXXXXXXXXXXXXX', 'MjEyOWU5MGVlM2NjZDY1ZTNmZTU2NjZhZGNjMTc5')

def @api.send_request(uri, method, data = nil, timeout=nil, use_multipart_conn = false)
def @api.send_request(uri, method, data = nil, timeout=nil, use_multipart_conn = false, is_lookup_request = false)
# puts "Resource URI: #{uri}"
# puts "Method: #{method}"
# puts "Data: #{data}"
Expand Down