Skip to content

Commit

Permalink
Initiating adapter without base url to send host specific requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadHassan-dev committed Jan 3, 2024
1 parent d9f081a commit 46c8ab5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 116 deletions.
166 changes: 69 additions & 97 deletions lib/ipinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,94 @@ def create(access_token = nil, settings = {})

class IPinfo::IPinfo
include IPinfo
attr_accessor :access_token, :countries, :httpc, :host_type
attr_accessor :access_token, :countries, :httpc

def initialize(access_token = nil, settings = {})
initialize_base(access_token, settings, host_type: :v4)
end
@access_token = access_token
prepare_http_client(settings.fetch('http_client', nil))

def initialize_v6(access_token = nil, settings = {})
initialize_base(access_token, settings, host_type: :v6)
maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
@cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
@countries = settings.fetch('countries', DEFAULT_COUNTRY_LIST)
@eu_countries = settings.fetch('eu_countries', DEFAULT_EU_COUNTRIES_LIST)
@countries_flags = settings.fetch('countries_flags', DEFAULT_COUNTRIES_FLAG_LIST)
@countries_currencies = settings.fetch('countries_currencies', DEFAULT_COUNTRIES_CURRENCIES_LIST)
@continents = settings.fetch('continents', DEFAULT_CONTINENT_LIST)
end

def details(ip_address = nil)
details_base(ip_address, host_type: :v4)
details_base(ip_address, :v4)
end

def details_v6(ip_address = nil)
details_base(ip_address, host_type: :v6)
details_base(ip_address, :v6)
end

def get_map_url(ips)
get_map_url_base(ips, host_type: :v4)
end
if !ips.kind_of?(Array)
return JSON.generate({:error => 'Invalid input. Array required!'})
end
if ips.length > 500000
return JSON.generate({:error => 'No more than 500,000 ips allowed!'})
end

json_ips = JSON.generate({:ips => ips})
res = @httpc.post('/tools/map', json_ips)

def get_map_url_v6(ips)
get_map_url_base(ips, host_type: :v6)
obj = JSON.parse(res.body)
obj['reportUrl']
end

def batch_requests(url_array, api_token)
batch_requests_base(url_array, api_token, host_type: :v4)
result = Hash.new
lookup_ips = []

url_array.each { |url|
ip = @cache.get(cache_key(url))

unless ip.nil?
result.store(url, ip)
else
lookup_ips << url
end
}

if lookup_ips.empty?
return result
end

begin
lookup_ips.each_slice(1000){ |ips|
json_arr = JSON.generate(lookup_ips)
res = @httpc.post("/batch?token=#{api_token}", json_arr, 5)

raise StandardError, "Request Quota Exceeded" if res.status == 429

data = JSON.parse(res.body)
data.each { |key, val|
@cache.set(cache_key(key), val)
}

result.merge!(data)
}

rescue StandardError => e
return e.message
end

result
end

def batch_requests_v6(url_array, api_token)
batch_requests_base(url_array, api_token, host_type: :v6)
protected

def prepare_http_client(httpc = nil)
@httpc = Adapter.new(access_token, httpc || :net_http)
end

private

def request_details(ip_address = nil)
def request_details(ip_address = nil, host_type)
if isBogon(ip_address)
details[:ip] = ip_address
details[:bogon] = true
Expand All @@ -78,7 +129,7 @@ def request_details(ip_address = nil)
res = @cache.get(cache_key(ip_address))
return res unless res.nil?

response = @httpc.get(escape_path(ip_address))
response = @httpc.get(escape_path(ip_address), host_type)

if response.status.eql?(429)
raise RateLimitError,
Expand All @@ -90,30 +141,8 @@ def request_details(ip_address = nil)
details
end

def prepare_http_client(settings = {}, host_type: :v4)
return if @host_type && @host_type == host_type

@host_type = host_type
@httpc = Adapter.new(access_token, settings['http_client'] || :net_http, host_type)
end

def initialize_base(access_token = nil, settings = {}, host_type: :v4)
@access_token = access_token
prepare_http_client(settings, host_type: host_type)

maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
@cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
@countries = settings.fetch('countries', DEFAULT_COUNTRY_LIST)
@eu_countries = settings.fetch('eu_countries', DEFAULT_EU_COUNTRIES_LIST)
@countries_flags = settings.fetch('countries_flags', DEFAULT_COUNTRIES_FLAG_LIST)
@countries_currencies = settings.fetch('countries_currencies', DEFAULT_COUNTRIES_CURRENCIES_LIST)
@continents = settings.fetch('continents', DEFAULT_CONTINENT_LIST)
end

def details_base(ip_address, settings = {}, host_type: :v4)
prepare_http_client(settings, host_type: host_type)
details = request_details(ip_address)
def details_base(ip_address, host_type)
details = request_details(ip_address, host_type)
if details.key? :country
details[:country_name] =
@countries.fetch(details.fetch(:country), nil)
Expand Down Expand Up @@ -142,63 +171,6 @@ def details_base(ip_address, settings = {}, host_type: :v4)
Response.new(details)
end

def get_map_url_base(ips, settings = {}, host_type: :v4)
prepare_http_client(settings, host_type: host_type)
if !ips.kind_of?(Array)
return JSON.generate({:error => 'Invalid input. Array required!'})
end
if ips.length > 500000
return JSON.generate({:error => 'No more than 500,000 ips allowed!'})
end

json_ips = JSON.generate({:ips => ips})
res = @httpc.post('/tools/map', json_ips)

obj = JSON.parse(res.body)
obj['reportUrl']
end

def batch_requests_base(url_array, api_token, settings = {}, host_type: :v4)
prepare_http_client(settings, host_type: host_type)
result = Hash.new
lookup_ips = []

url_array.each { |url|
ip = @cache.get(cache_key(url))

unless ip.nil?
result.store(url, ip)
else
lookup_ips << url
end
}

if lookup_ips.empty?
return result
end

begin
lookup_ips.each_slice(1000){ |ips|
json_arr = JSON.generate(lookup_ips)
res = @httpc.post("/batch?token=#{api_token}", json_arr, 5)

raise StandardError, "Request Quota Exceeded" if res.status == 429

data = JSON.parse(res.body)
data.each { |key, val|
@cache.set(cache_key(key), val)
}

result.merge!(data)
}

rescue StandardError => e
return e.message
end

result
end

def isBogon(ip)
if ip.nil?
return false
Expand Down
18 changes: 9 additions & 9 deletions lib/ipinfo/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
require_relative './version.rb'

class IPinfo::Adapter
HOST = 'ipinfo.io'
HOST_V6 = 'v6.ipinfo.io'
HOST = 'https://ipinfo.io'
HOST_V6 = 'https://v6.ipinfo.io'

attr_reader :conn

def initialize(token = nil, adapter = :net_http, host_type = :v4)
def initialize(token = nil, adapter = :net_http)
@token = token
@host = (host_type == :v6) ? HOST_V6 : HOST
@conn = connection(adapter)
end

def get(uri)
@conn.get(uri) do |req|
def get(uri, host_type= :v4)
host = (host_type == :v6) ? HOST_V6 : HOST
@conn.get(host + uri) do |req|
default_headers.each_pair do |key, value|
req.headers[key] = value
end
Expand All @@ -27,18 +27,18 @@ def get(uri)
end

def post(uri, body, timeout = 2)
@conn.post(uri) do |req|
@conn.post(HOST + uri) do |req|
req.body = body
req.options.timeout = timeout
end
end

private

attr_reader :token, :host
attr_reader :token

def connection(adapter)
Faraday.new(url: "https://#{@host}") do |conn|
Faraday.new() do |conn|
conn.adapter(adapter)
end
end
Expand Down
10 changes: 0 additions & 10 deletions test/ipinfo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,6 @@ def test_set_adapter_v4
assert(ipinfo.httpc = :excon)
end

def test_set_adapter_v6
ipinfo = IPinfo.create(
ENV['IPINFO_TOKEN'],
{ http_client: :excon }
)
ipinfo.initialize_v6

assert(ipinfo.httpc = :excon)
end

def test_lookup_ip6
ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'])

Expand Down

0 comments on commit 46c8ab5

Please sign in to comment.