Sha256: 689ce88bb84708d334e496a18007ba89ea4ea875afa5e957d5bd3d9ec84a32d0

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require "trupanion/authentication_headers"

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

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

    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

2 entries across 2 versions & 1 rubygems

Version Path
trupanion-0.1.1 lib/trupanion/client.rb
trupanion-0.1.0 lib/trupanion/client.rb