class PetfinderV2::Client
Public Class Methods
new(client_id = nil, client_secret = nil)
click to toggle source
# File lib/petfinder_V2/client.rb, line 10 def initialize(client_id = nil, client_secret = nil) @client_id = client_id || Config.instance.read(:client_id) @client_secret = client_secret || Config.instance.read(:client_secret) end
reset_access_token!()
click to toggle source
# File lib/petfinder_V2/client.rb, line 6 def self.reset_access_token! @@access_token = nil end
Public Instance Methods
get_animal(id)
click to toggle source
# File lib/petfinder_V2/client.rb, line 21 def get_animal(id) response_data = base_request(:get, "animals/#{id}") Serializers::Animal.new(response_data['animal']) end
get_animal_breeds(animal_type)
click to toggle source
# File lib/petfinder_V2/client.rb, line 36 def get_animal_breeds(animal_type) response_data = base_request(:get, "types/#{animal_type}/breeds") Serializers::AnimalBreed.process_collection(response_data) end
get_animal_type(name)
click to toggle source
# File lib/petfinder_V2/client.rb, line 31 def get_animal_type(name) response_data = base_request(:get, "types/#{name}") Serializers::AnimalType.new(response_data['type']) end
get_animal_types()
click to toggle source
# File lib/petfinder_V2/client.rb, line 26 def get_animal_types response_data = base_request(:get, 'types') Serializers::AnimalType.process_collection(response_data) end
get_organization(id)
click to toggle source
# File lib/petfinder_V2/client.rb, line 47 def get_organization(id) response_data = base_request(:get, "organizations/#{id}") Serializers::Organization.new(response_data['organization']) end
search_animals(opts = {})
click to toggle source
# File lib/petfinder_V2/client.rb, line 15 def search_animals(opts = {}) validate_animal_options!(opts) response_data = base_request(:get, 'animals', opts) Serializers::Animal.process_collection(response_data) end
search_organizations(opts = {})
click to toggle source
# File lib/petfinder_V2/client.rb, line 41 def search_organizations(opts = {}) validate_organization_options!(opts) response_data = base_request(:get, 'organizations', opts) Serializers::Organization.process_collection(response_data) end
Private Instance Methods
access_token_expired?()
click to toggle source
# File lib/petfinder_V2/client.rb, line 91 def access_token_expired? return true if @@access_token.nil? @@access_token.expires_at < Time.now end
base_request(http_verb, endpoint, opts = {})
click to toggle source
# File lib/petfinder_V2/client.rb, line 54 def base_request(http_verb, endpoint, opts = {}) res = Requests::Request.new(get_token.token) .send(http_verb, endpoint, opts) response_data = JSON.parse(res.body) handle_errors!(response_data) response_data end
get_token()
click to toggle source
# File lib/petfinder_V2/client.rb, line 87 def get_token access_token_expired? ? refresh_access_token : @@access_token end
handle_errors!(data)
click to toggle source
# File lib/petfinder_V2/client.rb, line 76 def handle_errors!(data) return if !data['status'] || data['status'] < 400 msg = if data['invalid-params'] data['invalid-params']['message'] else data['detail'] end raise(PetfinderV2::Error, "STATUS: #{data['status']} - #{msg}") end
invalid_options_error!(errors)
click to toggle source
# File lib/petfinder_V2/client.rb, line 72 def invalid_options_error!(errors) raise(InvalidRequestOptionsError, errors.join("\n")) unless errors.empty? end
refresh_access_token()
click to toggle source
# File lib/petfinder_V2/client.rb, line 97 def refresh_access_token res = Requests::AccessTokenRequest.new( @client_id, @client_secret ).get_access_token response_data = JSON.parse(res.body) handle_errors!(response_data) Serializers::AccessToken.new(response_data) end
validate_animal_options!(opts)
click to toggle source
# File lib/petfinder_V2/client.rb, line 62 def validate_animal_options!(opts) errors = Services::AnimalOptionsValidator.new(opts).run invalid_options_error!(errors) end
validate_organization_options!(opts)
click to toggle source
# File lib/petfinder_V2/client.rb, line 67 def validate_organization_options!(opts) errors = Services::OrganizationOptionsValidator.new(opts).run invalid_options_error!(errors) end