Sha256: e34374a0fcd73af260f34ac0423215387da3f8764cc830c113403f792057969d

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'faraday'
require 'cgi'
require 'ipinfo/mod'

class IPinfo::Adapter
    HOST = 'ipinfo.io'

    attr_reader :conn

    def initialize(token = nil, adapter = :net_http)
        @token = token
        @conn = connection(adapter)
    end

    def get(uri)
        @conn.get(uri) do |req|
            default_headers.each_pair do |key, value|
                req.headers[key] = value
            end
            req.params['token'] = CGI.escape(token) if token
        end
    end

    def post(uri, body, timeout = 2)
        @conn.post(uri) do |req|
            req.body = body
            req.options.timeout = timeout
        end
    end

    private

    attr_reader :token

    def connection(adapter)
        Faraday.new(url: "https://#{HOST}") do |conn|
            conn.adapter(adapter)
        end
    end

    def default_headers
        headers = {
            'User-Agent' => 'IPinfoClient/Ruby/1.1.0',
            'Accept' => 'application/json'
        }
        headers['Authorization'] = "Bearer #{CGI.escape(token)}" if token
        headers
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
IPinfo-1.1.0 lib/ipinfo/adapter.rb