Sha256: b885db7c2c2190a66e2983b794e12fbd5b16aac164335d9153f1b206b833cbfa

Contents?: true

Size: 1014 Bytes

Versions: 2

Compression:

Stored size: 1014 Bytes

Contents

module Breathe
  class Client
    BASE_URL = "https://api.breathehr.com/v1/"

    def initialize(api_key:)
      @api_key = api_key
    end

    def absences
      @_absences ||= Absences.new(self)
    end

    def get(url, url_opts = {})
      request(:get, url, url_opts)
    end

    private

    attr_reader :api_key, :environment

    def connection
      Faraday.new(url: BASE_URL) do |faraday|
        faraday.use Faraday::Response::RaiseError
        faraday.adapter Faraday.default_adapter
      end
    end

    def request(method, url, url_opts = {})
      connection.send(method) do |req|
        req.url BASE_URL + url, url_opts
        req.headers["Content-Type"] = "application/json"
        req.headers["X-Api-Key"] = api_key
      end
    rescue Faraday::ClientError => e
      case e.message
      when /401/
        raise Breathe::AuthenticationError, "The BreatheHR API returned a 401 error - are you sure you've set the correct API key?"
      else
        raise e
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
breathe-0.1.3 lib/breathe/client.rb
breathe-0.1.2 lib/breathe/client.rb