diff --git a/lib/addressfinder/address_autocomplete.rb b/lib/addressfinder/address_autocomplete.rb index 61d73f2..198c25a 100644 --- a/lib/addressfinder/address_autocomplete.rb +++ b/lib/addressfinder/address_autocomplete.rb @@ -44,9 +44,7 @@ def encoded_params end def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) + response = http.request(request_uri) self.response_body = response.body self.response_status = response.code diff --git a/lib/addressfinder/address_info.rb b/lib/addressfinder/address_info.rb index 0bd5cad..4a82b85 100644 --- a/lib/addressfinder/address_info.rb +++ b/lib/addressfinder/address_info.rb @@ -37,9 +37,7 @@ def encoded_params end def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) + response = http.request(request_uri) self.response_body = response.body self.response_status = response.code diff --git a/lib/addressfinder/address_search.rb b/lib/addressfinder/address_search.rb index 540fdc5..685af80 100644 --- a/lib/addressfinder/address_search.rb +++ b/lib/addressfinder/address_search.rb @@ -37,9 +37,7 @@ def encoded_params end def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) + response = http.request(request_uri) self.response_body = response.body self.response_status = response.code diff --git a/lib/addressfinder/configuration.rb b/lib/addressfinder/configuration.rb index 0e683f7..015bd1f 100644 --- a/lib/addressfinder/configuration.rb +++ b/lib/addressfinder/configuration.rb @@ -14,6 +14,7 @@ class Configuration attr_accessor :domain attr_accessor :retries attr_accessor :retry_delay + attr_accessor :ca def initialize self.hostname = 'api.addressfinder.io' @@ -23,6 +24,7 @@ def initialize self.retry_delay = 5 self.default_country = 'nz' self.verification_version = 'v1' + self.ca = "Ruby/#{AddressFinder::VERSION}" end end end diff --git a/lib/addressfinder/http.rb b/lib/addressfinder/http.rb index e6a9623..c53eb3a 100644 --- a/lib/addressfinder/http.rb +++ b/lib/addressfinder/http.rb @@ -15,11 +15,15 @@ def start(&block) end end - def request(args) + def request(request_uri) retries = 0 begin re_establish_connection if @connection_is_bad - net_http.request(args) + + uri = build_uri(request_uri) + request = Net::HTTP::Get.new(uri) + + net_http.request(request) rescue Net::ReadTimeout, Net::OpenTimeout, SocketError => error if retries < config.retries retries += 1 @@ -40,6 +44,19 @@ def re_establish_connection net_http.start end + def build_uri(request_uri) + uri = URI(request_uri) + encoded_ca = URI.encode_www_form_component(config.ca) + + if uri.query + uri.query += "&ca=#{encoded_ca}" + else + uri.query = "ca=#{encoded_ca}" + end + + uri.to_s + end + def net_http @net_http ||= begin http = Net::HTTP.new(config.hostname, config.port, config.proxy_host, diff --git a/lib/addressfinder/location_info.rb b/lib/addressfinder/location_info.rb index 6737acc..2356aab 100644 --- a/lib/addressfinder/location_info.rb +++ b/lib/addressfinder/location_info.rb @@ -38,9 +38,7 @@ def encoded_params end def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) + response = http.request(request_uri) self.response_body = response.body self.response_status = response.code diff --git a/lib/addressfinder/location_search.rb b/lib/addressfinder/location_search.rb index ad74001..8972be7 100644 --- a/lib/addressfinder/location_search.rb +++ b/lib/addressfinder/location_search.rb @@ -38,9 +38,7 @@ def encoded_params end def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) + response = http.request(request_uri) self.response_body = response.body self.response_status = response.code diff --git a/lib/addressfinder/v1/base.rb b/lib/addressfinder/v1/base.rb index 1db30e4..19f970d 100644 --- a/lib/addressfinder/v1/base.rb +++ b/lib/addressfinder/v1/base.rb @@ -20,12 +20,12 @@ def perform build_request execute_request build_result - + self end private - + attr_reader :request_uri, :params, :http, :path attr_accessor :response_body, :response_status attr_writer :result @@ -33,20 +33,18 @@ def perform def build_request @request_uri = "#{path}?#{encoded_params}" end - + def encoded_params Util.encode_and_join_params(params) end - + def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) - + response = http.request(request_uri) + self.response_body = response.body self.response_status = response.code end - + def build_result case response_status when '200' @@ -55,15 +53,15 @@ def build_result raise AddressFinder::RequestRejectedError.new(response_status, response_body) end end - + def response_hash @_response_hash ||= MultiJson.load(response_body) end - + def config @_config ||= AddressFinder.configuration end - + class Result < OpenStruct end end diff --git a/lib/addressfinder/v2/au/verification.rb b/lib/addressfinder/v2/au/verification.rb index 3649a1c..b1c9429 100644 --- a/lib/addressfinder/v2/au/verification.rb +++ b/lib/addressfinder/v2/au/verification.rb @@ -56,9 +56,7 @@ def build_request end def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) + response = http.request(request_uri) self.response_body = response.body self.response_status = response.code diff --git a/lib/addressfinder/verification.rb b/lib/addressfinder/verification.rb index 07e56da..381c4ee 100644 --- a/lib/addressfinder/verification.rb +++ b/lib/addressfinder/verification.rb @@ -61,9 +61,7 @@ def build_request end def execute_request - request = Net::HTTP::Get.new(request_uri) - - response = http.request(request) + response = http.request(request_uri) self.response_body = response.body self.response_status = response.code diff --git a/spec/lib/addressfinder/configuration_spec.rb b/spec/lib/addressfinder/configuration_spec.rb new file mode 100644 index 0000000..b6ff52f --- /dev/null +++ b/spec/lib/addressfinder/configuration_spec.rb @@ -0,0 +1,8 @@ +require 'spec_helper' + +RSpec.describe AddressFinder::Configuration do + it 'sets the client agent with the gem version' do + config = AddressFinder::Configuration.new + expect(config.ca).to eq("Ruby/#{AddressFinder::VERSION}") + end +end \ No newline at end of file diff --git a/spec/lib/addressfinder/http_spec.rb b/spec/lib/addressfinder/http_spec.rb new file mode 100644 index 0000000..3960171 --- /dev/null +++ b/spec/lib/addressfinder/http_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +RSpec.describe AddressFinder::HTTP do + let(:config) { AddressFinder::Configuration.new } + let(:http) { described_class.new(config) } + let(:request_uri) { "https://api.addressfinder.io?param=value" } + + describe '#build_uri' do + it 'appends the ca parameter to the query string' do + uri_with_ca = http.send(:build_uri, request_uri) + expect(uri_with_ca).to include("ca=Ruby%2F#{AddressFinder::VERSION}") + end + + it 'preserves existing query parameters' do + uri_with_ca = http.send(:build_uri, request_uri) + expect(uri_with_ca).to include("param=value") + end + end +end diff --git a/spec/lib/addressfinder_spec.rb b/spec/lib/addressfinder_spec.rb index 25c4572..be4d634 100644 --- a/spec/lib/addressfinder_spec.rb +++ b/spec/lib/addressfinder_spec.rb @@ -31,7 +31,8 @@ end it "safely passes arguments through" do - stub_request(:get, Addressable::Template.new("https://api.addressfinder.io/api/nz/address/verification{?args*}")).to_return(:status => 200, :body => "{}", :headers => {}) + nz_verification_endpoint = "https://api.addressfinder.io/api/nz/address/verification" + stub_request(:get, /\A#{nz_verification_endpoint}/).to_return(:status => 200, :body => "{}", :headers => {}) subject end end @@ -44,7 +45,8 @@ end it "safely passes arguments through" do - stub_request(:get, Addressable::Template.new("https://api.addressfinder.io/api/au/address/v2/verification{?args*}")).to_return(:status => 200, :body => "{}", :headers => {}) + au_verification_endpoint = "https://api.addressfinder.io/api/au/address/v2/verification" + stub_request(:get, /\A#{au_verification_endpoint}/).to_return(:status => 200, :body => "{}", :headers => {}) subject end end