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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion lib/ipinfo_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 11 additions & 3 deletions lib/ipinfo_io/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/ipinfo_io_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
33 changes: 33 additions & 0 deletions test/lib/adapter_test.rb
Original file line number Diff line number Diff line change
@@ -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