Skip to content

Commit

Permalink
- Added support for IPv6 host
Browse files Browse the repository at this point in the history
- Version bumped to 2.2.0
  • Loading branch information
SaadHassan-dev committed Dec 19, 2023
1 parent fa00489 commit e742046
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
9 changes: 5 additions & 4 deletions lib/ipinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions lib/ipinfo/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ipinfo/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module IPinfo
VERSION = '2.1.0'
VERSION = '2.2.0'
end
36 changes: 33 additions & 3 deletions test/ipinfo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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))
Expand Down Expand Up @@ -48,7 +58,7 @@ def assert_ip6(resp)
{
"name": 'Comcast Cable Communications, LLC',
"domain": 'comcast.com',
"type": ''
"type": 'isp'
}
)
assert_equal(
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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": '[email protected]',
"name": 'Abuse',
Expand All @@ -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

0 comments on commit e742046

Please sign in to comment.