Sha256: a3249619aa34c07da41c1efe7c75f16bd7986533c8fff03f34304fe4361067d4

Contents?: true

Size: 920 Bytes

Versions: 2

Compression:

Stored size: 920 Bytes

Contents

module Petfinder
  class Client

    include Api

    def initialize(api_key = Petfinder.api_key, api_secret = Petfinder.api_secret)
      @api_key = api_key
      @api_secret = api_secret

      raise Petfinder::Error.new("API key is required") unless @api_key
      raise Petfinder::Error.new("API secret is required") unless @api_secret
    end

    def request(path, params = {})
      response = token.get(path, params: params).parsed

      response

    rescue OAuth2::Error => e
      error_data = JSON.parse(e.response.body)
      raise Petfinder::Error.new(error_data), "#{error_data["title"]}: #{error_data["detail"]}"
    end

    def oauth_client
      @oauth_client ||= OAuth2::Client.new(@api_key, @api_secret, site: Petfinder::API_URL, token_url: 'oauth2/token')
    end

    def token
      @token = nil if @token&.expired?

      @token ||= oauth_client.client_credentials.get_token
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
petfinder-2.0.1 lib/petfinder/client.rb
petfinder-2.0.0 lib/petfinder/client.rb