Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion lib/ipinfo_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'ipinfo_io/errors'
require 'ipinfo_io/response'
require 'ipinfo_io/adapter'
require 'cgi'

module IpinfoIo
RATE_LIMIT_MESSAGE = "To increase your limits, please review our paid plans at https://ipinfo.io/pricing"
Expand All @@ -12,11 +13,17 @@ class << self
attr_accessor :access_token

def lookup(ip=nil)
response = Adapter.new(access_token).get(ip)
response = Adapter.new(access_token).get(uri_builder(ip))

raise RateLimitError.new(RATE_LIMIT_MESSAGE) if response.status.eql?(429)

Response.from_faraday(response)
end

private

def uri_builder(ip)
ip ? "/#{CGI::escape(ip)}" : '/'
end
end
end
8 changes: 2 additions & 6 deletions lib/ipinfo_io/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def initialize(token=nil, conn=Faraday.new(url: "https://#{HOST}"))
@conn = conn
end

def get(ip=nil)
@conn.get(uri_builder(ip)) do |req|
def get(uri)
@conn.get(uri) do |req|
default_headers.each_pair do |key, value|
req.headers[key] = value
end
Expand All @@ -22,10 +22,6 @@ def get(ip=nil)

private

def uri_builder(ip)
ip ? "/#{CGI::escape(ip)}" : '/'
end

attr_reader :token

def default_headers
Expand Down