Skip to content
Merged
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
19 changes: 9 additions & 10 deletions lib/ipinfo_io/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
# frozen_string_literal: true
require 'faraday'
require 'cgi'

module IpinfoIo
class Adapter
HOST = 'ipinfo.io'

def initialize(token=nil)
def initialize(token=nil, conn=Faraday.new(url: "https://#{HOST}"))
@token = token
@conn = conn
end

def get(ip=nil)
Faraday.get(url_builder(ip)) do |req|
@conn.get(uri_builder(ip)) do |req|
default_headers.each_pair do |key, value|
req.headers[key] = value
end
req.params['token'] = CGI::escape(token) if token
end
end

private

attr_reader :token

def url_builder(ip)
if token
"https://#{HOST}/#{ip}?token=#{token}"
else
"https://#{HOST}/#{ip}"
end
def uri_builder(ip)
ip ? "/#{CGI::escape(ip)}" : '/'
end

attr_reader :token

def default_headers
{
'User-Agent' => "ruby/#{::IpinfoIo::VERSION}",
Expand Down