Sha256: b9118bda67d053ce8feb2e5493792ff09040f0003b553a3070a59bd102056e0e

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Contactually
  class Interface
    attr_reader :adapter, :api_key, :auth_token, :base_url, :connection, :headers

    def initialize(auth_token: nil, api_key: nil, base_url: nil)
      @auth_token = auth_token
      @api_key = api_key
      @adapter = Contactually.configuration.adapter || :net_http
      @base_url = base_url

      set_headers
    end

    def get(url, params)
      connection.get(build_full_url(url), params)
    end

    def connection
      @connection ||= Faraday.new do |conn|
        conn.adapter  @adapter
        conn.response :json, :content_type => /\bjson$/
        conn.use Contactually::Middleware::ErrorHandler

        conn.headers['Content-type']  = 'application/json',
        conn.headers['Authorization'] = "Bearer #{token}"
      end
    end

    private

    def build_full_url(url)
      if url.downcase.start_with?('http')
        url
      else
        "#{base_url}#{url}"
      end
    end

    def token
      auth_token || api_key
    end

    def set_headers
      @headers = {
        'Content-type' => 'application/json',
        'Authorization' => "Bearer #{token}"
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contactually-rb-0.1.1 lib/contactually/interface.rb