Sha256: bb24d038886799b6aa77de281d302b03115f0c078d5cb74c4356234feedc32c4

Contents?: true

Size: 943 Bytes

Versions: 1

Compression:

Stored size: 943 Bytes

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

    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.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.0.1 lib/ipinfo/adapter.rb