Sha256: 3d3d6596d54d27814e1e27be95e9c3813fc205aea385bb18135b7615c84912fc

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require "trupanion/authentication_headers"

require "trupanion/api/breeds"
require "trupanion/api/quotes"
require "trupanion/api/policies"

module Trupanion
  class Client
    API_BASE = "https://api.trupanion.com".freeze
    SANDBOX_API_BASE = "https://sandbox.api.trupanion.com".freeze

    attr_reader :api_key, :api_secret

    def initialize(api_key:, api_secret:, sandbox: false)
      @api_key = api_key
      @api_secret = api_secret
      @sandbox = sandbox
    end

    def get(url, params: {})
      conn.get(url, params) do |req|
        req.headers = AuthorizationHeaders.new(self, req).call
      end
    end

    def post(url, body: {})
      conn.post(url, body) do |req|
        req.headers = AuthorizationHeaders.new(self, req).call
      end
    end

    def delete(url)
      conn.delete(url)
    end

    def put(url, body: {})
      conn.put(url, body)
    end

    def breeds; API::Breeds.new(self); end

    def quotes; API::Quotes.new(self); end

    def policies; API::Policies.new(self); end

    private

    attr_reader :sandbox

    def conn
      @conn ||= Faraday.new(api_url) do |f|
        f.request :json
        f.response :json, content_type: /\bjson$/
        f.adapter Faraday.default_adapter
      end
    end

    def api_url
      @_api_url ||= sandbox ? SANDBOX_API_BASE : API_BASE
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trupanion-0.1.2 lib/trupanion/client.rb