From e7420466a92234422bce92ce1d93c7a500d5fc76 Mon Sep 17 00:00:00 2001 From: SaadHassan-dev Date: Tue, 19 Dec 2023 17:20:13 +0500 Subject: [PATCH] - Added support for IPv6 host - Version bumped to 2.2.0 --- lib/ipinfo.rb | 9 +++++---- lib/ipinfo/adapter.rb | 10 ++++++---- lib/ipinfo/version.rb | 2 +- test/ipinfo_test.rb | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/lib/ipinfo.rb b/lib/ipinfo.rb index c324b2c..317b63f 100644 --- a/lib/ipinfo.rb +++ b/lib/ipinfo.rb @@ -30,10 +30,11 @@ def create(access_token = nil, settings = {}) class IPinfo::IPinfo include IPinfo - attr_accessor :access_token, :countries, :httpc + attr_accessor :access_token, :countries, :httpc, :host_type def initialize(access_token = nil, settings = {}) @access_token = access_token + @host_type = settings.fetch('host_type', :v4) @httpc = prepare_http_client(settings.fetch('http_client', nil)) maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE) @@ -57,7 +58,7 @@ def details(ip_address = nil) @countries_flags.fetch(details.fetch(:country), nil) details[:country_currency] = @countries_currencies.fetch(details.fetch(:country), nil) - details[:continent] = + details[:continent] = @continents.fetch(details.fetch(:country), nil) details[:country_flag_url] = COUNTRY_FLAGS_URL + details.fetch(:country) + ".svg" end @@ -159,9 +160,9 @@ def request_details(ip_address = nil) def prepare_http_client(httpc = nil) @httpc = if httpc - Adapter.new(access_token, httpc) + Adapter.new(access_token, httpc, host_type) else - Adapter.new(access_token) + Adapter.new(access_token, :net_http, host_type) end end diff --git a/lib/ipinfo/adapter.rb b/lib/ipinfo/adapter.rb index 2580bf7..615b7bf 100644 --- a/lib/ipinfo/adapter.rb +++ b/lib/ipinfo/adapter.rb @@ -6,11 +6,13 @@ class IPinfo::Adapter HOST = 'ipinfo.io' + HOST_V6 = 'v6.ipinfo.io' attr_reader :conn - def initialize(token = nil, adapter = :net_http) + def initialize(token = nil, adapter = :net_http, host_type = :v4) @token = token + @host = (host_type == :v6) ? HOST_V6 : HOST @conn = connection(adapter) end @@ -32,17 +34,17 @@ def post(uri, body, timeout = 2) private - attr_reader :token + attr_reader :token, :host def connection(adapter) - Faraday.new(url: "https://#{HOST}") do |conn| + Faraday.new(url: "https://#{@host}") do |conn| conn.adapter(adapter) end end def default_headers headers = { - 'User-Agent' => 'IPinfoClient/Ruby/2.1.0', + 'User-Agent' => 'IPinfoClient/Ruby/2.2.0', 'Accept' => 'application/json' } headers['Authorization'] = "Bearer #{CGI.escape(token)}" if token diff --git a/lib/ipinfo/version.rb b/lib/ipinfo/version.rb index 1d7cd02..caf1317 100644 --- a/lib/ipinfo/version.rb +++ b/lib/ipinfo/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module IPinfo - VERSION = '2.1.0' + VERSION = '2.2.0' end diff --git a/test/ipinfo_test.rb b/test/ipinfo_test.rb index ebebb93..c4c4735 100644 --- a/test/ipinfo_test.rb +++ b/test/ipinfo_test.rb @@ -10,7 +10,7 @@ def test_that_it_has_a_version_number refute_nil ::IPinfo::VERSION end - def test_set_adapter + def test_set_adapter_v4 ipinfo = IPinfo.create( ENV['IPINFO_TOKEN'], { http_client: :excon } @@ -19,6 +19,16 @@ def test_set_adapter assert(ipinfo.httpc = :excon) end + def test_set_adapter_v6 + ipinfo = IPinfo.create( + ENV['IPINFO_TOKEN'], + { http_client: :excon }, + { 'host_type' => :v6 } + ) + + assert(ipinfo.httpc = :excon) + end + def assert_ip6(resp) assert_equal(resp.ip, TEST_IPV6) assert_equal(resp.ip_address, IPAddr.new(TEST_IPV6)) @@ -48,7 +58,7 @@ def assert_ip6(resp) { "name": 'Comcast Cable Communications, LLC', "domain": 'comcast.com', - "type": '' + "type": 'isp' } ) assert_equal( @@ -94,6 +104,16 @@ def test_lookup_ip6 end end + def test_lookup_ip6_on_host_v6 + ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'], { 'host_type' => :v6 }) + + # multiple checks for cache + (0...5).each do |_| + resp = ipinfo.details(TEST_IPV6) + assert_ip6(resp) + end + end + def assert_ip4(resp) assert_equal(resp.ip, TEST_IPV4) assert_equal(resp.ip_address, IPAddr.new(TEST_IPV4)) @@ -149,7 +169,7 @@ def assert_ip4(resp) resp.abuse, { "address": 'US, CA, Mountain View, ' \ - '1600 Amphitheatre Parkway, 94043', + '1600 Amphitheatre Parkway, 94043', "country": 'US', "email": 'network-abuse@google.com', "name": 'Abuse', @@ -171,4 +191,14 @@ def test_lookup_ip4 assert_ip4(resp) end end + + def test_lookup_ip4_on_host_v6 + ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'], { 'host_type' => :v6 }) + + # multiple checks for cache + (0...5).each do |_| + resp = ipinfo.details(TEST_IPV4) + assert_ip4(resp) + end + end end