From 9f9d2760fbe1732b9e52a8d1bda8d84c12cbf6be Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Wed, 1 Nov 2017 01:22:51 +0700 Subject: [PATCH 1/2] IpinfoIo::Adapter object created --- lib/ipinfo_io.rb | 27 +++------------------------ lib/ipinfo_io/adapter.rb | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 lib/ipinfo_io/adapter.rb diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index 84d7608..bfba3ab 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -3,43 +3,22 @@ require "ipinfo_io/version" require 'ipinfo_io/errors' require 'ipinfo_io/response' +require 'ipinfo_io/adapter' require 'faraday' require 'json' module IpinfoIo RATE_LIMIT_MESSAGE = "To increase your limits, please review our paid plans at https://ipinfo.io/pricing" - HOST = 'ipinfo.io' class << self attr_accessor :access_token def lookup(ip=nil) - response = Faraday.get(url_builder(ip)) do |req| - default_headers.each_pair do |key, value| - req.headers[key] = value - end - end + response = Adapter.new(access_token).get(ip) raise RateLimitError.new(RATE_LIMIT_MESSAGE) if response.status.eql?(429) - IpinfoIo::Response.from_faraday(response) - end - - private - - def url_builder(ip) - if access_token - "https://#{HOST}/#{ip}?token=#{access_token}" - else - "https://#{HOST}/#{ip}" - end - end - - def default_headers - { - 'User-Agent' => "ruby/#{::IpinfoIo::VERSION}", - 'Accept' => 'application/json' - } + Response.from_faraday(response) end end end diff --git a/lib/ipinfo_io/adapter.rb b/lib/ipinfo_io/adapter.rb new file mode 100644 index 0000000..73e3e79 --- /dev/null +++ b/lib/ipinfo_io/adapter.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module IpinfoIo + class Adapter + HOST = 'ipinfo.io' + + def initialize(token=nil) + @token = token + end + + def get(ip=nil) + Faraday.get(url_builder(ip)) do |req| + default_headers.each_pair do |key, value| + req.headers[key] = value + end + end + end + + private + + attr_reader :token + + def url_builder(ip) + if token + "https://#{HOST}/#{ip}?token=#{token}" + else + "https://#{HOST}/#{ip}" + end + end + + def default_headers + { + 'User-Agent' => "ruby/#{::IpinfoIo::VERSION}", + 'Accept' => 'application/json' + } + end + end +end From 6115042570e60d94c44c7d78e41dce36eded241c Mon Sep 17 00:00:00 2001 From: Stanislav K Date: Wed, 1 Nov 2017 01:26:08 +0700 Subject: [PATCH 2/2] slightly fixes to library requirements --- lib/ipinfo_io.rb | 2 -- lib/ipinfo_io/adapter.rb | 1 + lib/ipinfo_io/response.rb | 3 +++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index bfba3ab..cae2414 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -4,8 +4,6 @@ require 'ipinfo_io/errors' require 'ipinfo_io/response' require 'ipinfo_io/adapter' -require 'faraday' -require 'json' module IpinfoIo RATE_LIMIT_MESSAGE = "To increase your limits, please review our paid plans at https://ipinfo.io/pricing" diff --git a/lib/ipinfo_io/adapter.rb b/lib/ipinfo_io/adapter.rb index 73e3e79..87f0b80 100644 --- a/lib/ipinfo_io/adapter.rb +++ b/lib/ipinfo_io/adapter.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'faraday' module IpinfoIo class Adapter diff --git a/lib/ipinfo_io/response.rb b/lib/ipinfo_io/response.rb index 1182086..1308908 100644 --- a/lib/ipinfo_io/response.rb +++ b/lib/ipinfo_io/response.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true +require 'json' + module IpinfoIo class Response # The data contained by the HTTP body of the response deserialized from