Sha256: 59b0116444599a56a40d49c510dd57a55348f561860ad65a1bd5e77053fc6182

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 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
    end

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

    def patch(url, body)
      connection.patch(build_full_url(url), body)
    end

    def post(url, body)
      connection.post(build_full_url(url), body)
    end

    def destroy(url)
      connection.delete(build_full_url(url))
    end

    def connection
      @connection ||= Faraday.new do |conn|
        conn.request :json

        conn.response :json, :content_type => /\bjson$/
        conn.use Contactually::Middleware::ErrorHandler

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

        conn.adapter  @adapter
      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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contactually-rb-0.1.3 lib/contactually/interface.rb
contactually-rb-0.1.2 lib/contactually/interface.rb