diff --git a/README.md b/README.md index d05b0a8..f8fbc0a 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,22 @@ To avoid that error, you can provide your access_token anywhere in initialisatio IpinfoIo.access_token = "your_access_token" ``` +### HTTP libraries +Ruby is notoriously known for having a lot of HTTP libraries. While `Net::HTTP` is a reasonable default, you can set any other that [Faraday supports](https://github.com/lostisland/faraday/tree/29feeb92e3413d38ffc1fd3a3479bb48a0915730#faraday) if you need it. + +``` +IpinfoIo.http_adapter = :excon +``` + +Don't forget to bundle that http library, since they are not included. ### Requirements +One of following: - Ruby 2.0+ +- Jruby +- Rubinius (should work, but not tested) + +It's also framework agnostic and supports all the http adapters [Faraday supports](https://github.com/lostisland/faraday/tree/29feeb92e3413d38ffc1fd3a3479bb48a0915730#faraday). ## Development diff --git a/lib/ipinfo_io.rb b/lib/ipinfo_io.rb index 4591bad..b46691b 100644 --- a/lib/ipinfo_io.rb +++ b/lib/ipinfo_io.rb @@ -11,9 +11,14 @@ module IpinfoIo class << self attr_accessor :access_token + attr_writer :http_adapter def lookup(ip=nil) - response = Adapter.new(access_token).get(uri_builder(ip)) + response = if @http_adapter + Adapter.new(access_token, @http_adapter) + else + Adapter.new(access_token) + end.get(uri_builder(ip)) raise RateLimitError.new(RATE_LIMIT_MESSAGE) if response.status.eql?(429) diff --git a/lib/ipinfo_io/adapter.rb b/lib/ipinfo_io/adapter.rb index 4383311..955961d 100644 --- a/lib/ipinfo_io/adapter.rb +++ b/lib/ipinfo_io/adapter.rb @@ -6,9 +6,11 @@ module IpinfoIo class Adapter HOST = 'ipinfo.io' - def initialize(token=nil, conn=Faraday.new(url: "https://#{HOST}")) + attr_reader :conn + + def initialize(token = nil, adapter = :net_http) @token = token - @conn = conn + @conn = connection(adapter) end def get(uri) @@ -24,9 +26,15 @@ def get(uri) attr_reader :token + def connection(adapter) + Faraday.new(url: "https://#{HOST}") do |faraday| + faraday.adapter adapter + end + end + def default_headers { - 'User-Agent' => "ruby/#{::IpinfoIo::VERSION}", + 'User-Agent' => "ipinfo-ruby/#{::IpinfoIo::VERSION}", 'Accept' => 'application/json' } end diff --git a/test/ipinfo_io_test.rb b/test/ipinfo_io_test.rb index fd2e597..bd9c031 100644 --- a/test/ipinfo_io_test.rb +++ b/test/ipinfo_io_test.rb @@ -9,6 +9,11 @@ def test_that_it_has_a_version_number refute_nil ::IpinfoIo::VERSION end + def test_set_adapter + assert IpinfoIo.http_adapter = :excon + IpinfoIo.http_adapter = nil + end + def test_set_access_token assert IpinfoIo.access_token = 'test_token' diff --git a/test/lib/adapter_test.rb b/test/lib/adapter_test.rb new file mode 100644 index 0000000..14802cc --- /dev/null +++ b/test/lib/adapter_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true +require 'test_helper' +require_relative '../../lib/ipinfo_io/adapter' + +class IpinfoIo::AdapterTest < Minitest::Test + def test_default + adapter = IpinfoIo::Adapter.new + assert(adapter.conn.builder.handlers[0] === Faraday::Adapter::NetHttp) + end + + SUPPORTED_ADAPTERS = { + net_http: Faraday::Adapter::NetHttp, + net_http_persistent: Faraday::Adapter::NetHttpPersistent, + typhoeus: Faraday::Adapter::Typhoeus, + patron: Faraday::Adapter::Patron, + em_synchrony: Faraday::Adapter::EMSynchrony, + em_http: Faraday::Adapter::EMHttp, + excon: Faraday::Adapter::Excon, + httpclient: Faraday::Adapter::HTTPClient + } + + def test_unsupported_adapter + error = assert_raises(Faraday::Error) { IpinfoIo::Adapter.new(nil, :missing_adapter) } + assert_equal error.message, ":missing_adapter is not registered on Faraday::Adapter" + end + + def test_all_possible_adapters + SUPPORTED_ADAPTERS.keys.each do |key| + adapter = IpinfoIo::Adapter.new(nil, key) + assert(adapter.conn.builder.handlers[0] === SUPPORTED_ADAPTERS[key]) + end + end +end