Sha256: 0ca92e8806294216be9545cd91e81cf8b9c287ea6234817afa6c93bcb74c0d4b

Contents?: true

Size: 975 Bytes

Versions: 1

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true

require 'faraday'
require 'faraday_middleware/multi_json'
require 'codat/faraday_codat_auth'

module Codat
  class Client
    BASE_URLS = {
      production: 'https://api.codat.io/',
      uat: 'https://api-uat.codat.io/'
    }.freeze

    def initialize
      Faraday::Request.register_middleware codat_auth: -> { Codat::FaradayCodatAuth }
    end

    def get(path, params = {})
      connection.get(path, params)
    end

    def post(path, params = {})
      connection.post(path, MultiJson.dump(params))
    end

    def connection
      @connection ||= Faraday.new(url: api_url) do |conn|
        conn.request :codat_auth, Codat.configuration.api_key
        conn.response :multi_json, symbolize_keys: true
        # conn.response :logger # if Codat.configuration.debug?
        conn.adapter Faraday.default_adapter
      end
    end

    private

    def api_url
      BASE_URLS[Codat.configuration.environment.to_sym]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codat-0.1.7 lib/codat/client.rb