Class: Petfinder::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/petfinder_ruby.rb

Class Method Summary collapse

Class Method Details

.check_tokenObject



48
49
50
51
52
# File 'lib/petfinder_ruby.rb', line 48

def self.check_token
  if Petfinder.configuration.access_token == nil || Petfinder.configuration.token_expires_at < Time.now
    get_token
  end
end

.get_adoptable_animals_by_organization(org_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/petfinder_ruby.rb', line 30

def self.get_adoptable_animals_by_organization(org_id)
  check_token
  response = Unirest.get("https://api.petfinder.com/v2/animals",
                        headers: {"Authorization" => "Bearer #{Petfinder.configuration.access_token}"},
                        parameters: { "organization" => org_id,
                                      "status" => "adoptable"
                                    }
                        )
  response.body
end

.get_organization(org_id) ⇒ Object



41
42
43
44
45
46
# File 'lib/petfinder_ruby.rb', line 41

def self.get_organization(org_id)
  check_token
  response = Unirest.get("https://api.petfinder.com/v2/organizations/#{org_id}",
    headers: {"Authorization" => "Bearer #{Petfinder.configuration.access_token}"})
  response.body
end

.get_tokenObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/petfinder_ruby.rb', line 18

def self.get_token
  response = Unirest.post("https://api.petfinder.com/v2/oauth2/token", 
                          parameters: {
                            "grant_type" => "client_credentials", 
                            "client_id" => "#{Petfinder.configuration.client_id}", 
                            "client_secret" => "#{Petfinder.configuration.client_secret}"
                          }
                        )
  Petfinder.configuration.access_token = response.body["access_token"]
  Petfinder.configuration.token_expires_at = Time.now + 3560
end