petfinder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +31 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/lib/petfinder/client.rb +98 -0
- data/lib/petfinder.rb +22 -0
- data/test/fixtures/auth.xml +17 -0
- data/test/fixtures/breed_list.xml +40 -0
- data/test/fixtures/pet.xml +48 -0
- data/test/fixtures/pet_list.xml +966 -0
- data/test/fixtures/shelter.xml +26 -0
- data/test/fixtures/shelter_list.xml +189 -0
- data/test/helper.rb +25 -0
- data/test/test_petfinder.rb +67 -0
- metadata +134 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Eric Hutzelman
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
= Petfinder
|
2
|
+
|
3
|
+
Ruby gem wrapper for the {Petfinder API}[http://www.petfinder.com/developers/api-docs].
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
sudo gem install petfinder
|
8
|
+
|
9
|
+
== Get your API key
|
10
|
+
|
11
|
+
Get your Petfinder API key at: http://www.petfinder.com/developers/api-key
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
== TODO
|
16
|
+
|
17
|
+
* Implement use of security token when Petfinder requires it
|
18
|
+
|
19
|
+
== Note on Patches/Pull Requests
|
20
|
+
|
21
|
+
* Fork the project.
|
22
|
+
* Make your feature addition or bug fix.
|
23
|
+
* Add tests for it. This is important so I don't break it in a
|
24
|
+
future version unintentionally.
|
25
|
+
* Commit, do not mess with rakefile, version, or history.
|
26
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
27
|
+
* Send me a pull request. Bonus points for topic branches.
|
28
|
+
|
29
|
+
== Copyright
|
30
|
+
|
31
|
+
Copyright (c) 2010 Eric Hutzelman. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "petfinder"
|
8
|
+
gem.summary = %Q{Ruby gem wrapper for the Petfinder API}
|
9
|
+
gem.description = %Q{Ruby gem wrapper for the Petfinder API}
|
10
|
+
gem.email = "ehutzelman@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/ehutzelman/petfinder"
|
12
|
+
gem.authors = ["Eric Hutzelman"]
|
13
|
+
|
14
|
+
gem.add_dependency('hashie', '>= 0.1.3')
|
15
|
+
gem.add_dependency('httparty', '>= 0.5.0')
|
16
|
+
|
17
|
+
gem.add_development_dependency "shoulda", ">= 2.10.1"
|
18
|
+
gem.add_development_dependency 'fakeweb', '>= 1.2.5'
|
19
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'rake/testtask'
|
27
|
+
Rake::TestTask.new(:test) do |test|
|
28
|
+
test.libs << 'lib' << 'test'
|
29
|
+
test.pattern = 'test/**/test_*.rb'
|
30
|
+
test.verbose = true
|
31
|
+
end
|
32
|
+
|
33
|
+
begin
|
34
|
+
require 'rcov/rcovtask'
|
35
|
+
Rcov::RcovTask.new do |test|
|
36
|
+
test.libs << 'test'
|
37
|
+
test.pattern = 'test/**/test_*.rb'
|
38
|
+
test.verbose = true
|
39
|
+
end
|
40
|
+
rescue LoadError
|
41
|
+
task :rcov do
|
42
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
task :test => :check_dependencies
|
47
|
+
|
48
|
+
task :default => :test
|
49
|
+
|
50
|
+
require 'rake/rdoctask'
|
51
|
+
Rake::RDocTask.new do |rdoc|
|
52
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
53
|
+
|
54
|
+
rdoc.rdoc_dir = 'rdoc'
|
55
|
+
rdoc.title = "petfinder #{version}"
|
56
|
+
rdoc.rdoc_files.include('README*')
|
57
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
58
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,98 @@
|
|
1
|
+
module Petfinder
|
2
|
+
|
3
|
+
class Client
|
4
|
+
include HTTParty
|
5
|
+
format :xml
|
6
|
+
base_uri "http://api.petfinder.com"
|
7
|
+
|
8
|
+
def initialize(api_key = Petfinder.api_key, api_secret = Petfinder.api_secret)
|
9
|
+
@api_key, @api_secret = api_key, api_secret
|
10
|
+
raise "API key is required" if @api_key.blank?
|
11
|
+
|
12
|
+
self.class.default_params :key => api_key
|
13
|
+
end
|
14
|
+
|
15
|
+
# Valid animal types: barnyard, bird, cat, dog, horse, pig, reptile, smallfurry
|
16
|
+
def breeds(animal_type)
|
17
|
+
response = perform_get("/breed.list", :query => {:animal => animal_type})
|
18
|
+
return_as_array(response.breeds.breed)
|
19
|
+
end
|
20
|
+
|
21
|
+
def pet(id)
|
22
|
+
response = perform_get("/pet.get", :query => {:id => id})
|
23
|
+
response.pet
|
24
|
+
end
|
25
|
+
|
26
|
+
# Options available: animal, breed, size, sex, location, shelterid
|
27
|
+
def random_pet(options = {})
|
28
|
+
response = perform_get("/pet.getRandom", :query => {:output => 'full'})
|
29
|
+
response.pet
|
30
|
+
end
|
31
|
+
|
32
|
+
# Options available: breed, size, sex, age, offset, count
|
33
|
+
def find_pets(animal_type, location, options = {})
|
34
|
+
query = options.merge(:animal => animal_type, :location => location)
|
35
|
+
response = perform_get("/pet.find", :query => query)
|
36
|
+
return_as_array(response.pets.pet)
|
37
|
+
end
|
38
|
+
|
39
|
+
def shelter(id)
|
40
|
+
response = perform_get("/shelter.get", :query => {:id => id})
|
41
|
+
response.shelter
|
42
|
+
end
|
43
|
+
|
44
|
+
# Options available: name, offset, count
|
45
|
+
def find_shelters(location, options = {})
|
46
|
+
query = options.merge(:location => location)
|
47
|
+
response = perform_get("/shelter.find", :query => query)
|
48
|
+
return_as_array(response.shelters.shelter)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Options available: offset, count
|
52
|
+
def find_shelters_by_breed(animal_type, breed, options = {})
|
53
|
+
query = options.merge(:animal => animal_type, :breed => breed)
|
54
|
+
response = perform_get("/shelter.listByBreed", :query => query)
|
55
|
+
return_as_array(response.shelters.shelter)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Options available: status, offset, count
|
59
|
+
def shelter_pets(id, options = {})
|
60
|
+
query = options.merge(:id => id)
|
61
|
+
response = perform_get("/shelter.getPets", :query => query)
|
62
|
+
return_as_array(response.pets.pet)
|
63
|
+
end
|
64
|
+
|
65
|
+
def token
|
66
|
+
response = perform_get("/auth.getToken", :query => {:sig => digest_key_and_secret})
|
67
|
+
response.auth.token
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def return_as_array(object)
|
73
|
+
object.class == Array ? object : [object]
|
74
|
+
end
|
75
|
+
|
76
|
+
def digest_key_and_secret
|
77
|
+
raise "API secret is required" if @api_secret.blank?
|
78
|
+
Digest::MD5.hexdigest(@api_secret + "key=#{@api_key}")
|
79
|
+
end
|
80
|
+
|
81
|
+
def perform_get(uri, options = {})
|
82
|
+
response = self.class.get(uri, options)
|
83
|
+
raise_errors(response)
|
84
|
+
mash = Hashie::Mash.new(response['petfinder'])
|
85
|
+
end
|
86
|
+
|
87
|
+
def raise_errors(response)
|
88
|
+
if response.code.to_i == 200
|
89
|
+
status = response['petfinder']['header']['status']
|
90
|
+
raise PetfinderError.new("(#{status['code']}) #{status['message']}") if status['code'].to_i != 100
|
91
|
+
else
|
92
|
+
raise RuntimeError.new("Invalid response from server: #{response.code}")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
data/lib/petfinder.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'httparty'
|
3
|
+
require 'hashie'
|
4
|
+
require 'digest/md5'
|
5
|
+
|
6
|
+
module Petfinder
|
7
|
+
|
8
|
+
class PetfinderError < StandardError; end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :api_key, :api_secret
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
yield self
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
directory = File.expand_path(File.dirname(__FILE__))
|
22
|
+
require File.join(directory, 'petfinder', 'client')
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
|
3
|
+
<header>
|
4
|
+
<version>0.1</version>
|
5
|
+
<timestamp>2010-04-10T22:29:07Z</timestamp>
|
6
|
+
<status>
|
7
|
+
<code>100</code>
|
8
|
+
<message/>
|
9
|
+
</status>
|
10
|
+
</header>
|
11
|
+
<auth>
|
12
|
+
<key>83bd1e190a9d09ad8041d2f087684753</key>
|
13
|
+
<token>39038a000f404335d90bab019c0c7e2d</token>
|
14
|
+
<expires>1270942147</expires>
|
15
|
+
<expiresString>2010-04-10T23:29:07Z</expiresString>
|
16
|
+
</auth>
|
17
|
+
</petfinder>
|
@@ -0,0 +1,40 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
|
3
|
+
<header>
|
4
|
+
<version>0.1</version>
|
5
|
+
<timestamp>2010-04-09T06:34:37Z</timestamp>
|
6
|
+
<status>
|
7
|
+
<code>100</code>
|
8
|
+
<message/>
|
9
|
+
</status>
|
10
|
+
</header>
|
11
|
+
<breeds animal="horse">
|
12
|
+
<breed>Appaloosa</breed>
|
13
|
+
<breed>Arabian</breed>
|
14
|
+
<breed>Belgian</breed>
|
15
|
+
<breed>Clydesdale</breed>
|
16
|
+
<breed>Curly Horse</breed>
|
17
|
+
<breed>Donkey/Mule</breed>
|
18
|
+
<breed>Draft</breed>
|
19
|
+
<breed>Gaited</breed>
|
20
|
+
<breed>Grade</breed>
|
21
|
+
<breed>Lipizzan</breed>
|
22
|
+
<breed>Miniature Horse</breed>
|
23
|
+
<breed>Missouri Foxtrotter</breed>
|
24
|
+
<breed>Morgan</breed>
|
25
|
+
<breed>Mustang</breed>
|
26
|
+
<breed>Paint/Pinto</breed>
|
27
|
+
<breed>Palomino</breed>
|
28
|
+
<breed>Paso Fino</breed>
|
29
|
+
<breed>Percheron</breed>
|
30
|
+
<breed>Peruvian Paso</breed>
|
31
|
+
<breed>Pony</breed>
|
32
|
+
<breed>Quarterhorse</breed>
|
33
|
+
<breed>Saddlebred</breed>
|
34
|
+
<breed>Shetland Pony</breed>
|
35
|
+
<breed>Standardbred</breed>
|
36
|
+
<breed>Tennessee Walker</breed>
|
37
|
+
<breed>Thoroughbred</breed>
|
38
|
+
<breed>Warmblood</breed>
|
39
|
+
</breeds>
|
40
|
+
</petfinder>
|
@@ -0,0 +1,48 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
|
3
|
+
<header>
|
4
|
+
<version>0.1</version>
|
5
|
+
<timestamp>2010-04-09T06:14:44Z</timestamp>
|
6
|
+
<status>
|
7
|
+
<code>100</code>
|
8
|
+
<message/>
|
9
|
+
</status>
|
10
|
+
</header>
|
11
|
+
<pet>
|
12
|
+
<id>10141994</id>
|
13
|
+
<shelterId>IL173</shelterId>
|
14
|
+
<shelterPetId>0A10503371</shelterPetId>
|
15
|
+
<name>Charlie</name>
|
16
|
+
<animal>Cat</animal>
|
17
|
+
<breeds>
|
18
|
+
<breed>Domestic Short Hair-black</breed>
|
19
|
+
</breeds>
|
20
|
+
<mix>no</mix>
|
21
|
+
<age>Adult</age>
|
22
|
+
<sex>M</sex>
|
23
|
+
<size>M</size>
|
24
|
+
<options>
|
25
|
+
<option>hasShots</option>
|
26
|
+
<option>altered</option>
|
27
|
+
</options>
|
28
|
+
<description><![CDATA[Charlie is an outgoing cat who just loves to be around all the attention. He runs around and chases anything that moves. We think he'd be great in an active household!
|
29
|
+
<br/><a href="http://www.petfinder.com/petdetail/10141994">View this pet on Petfinder.com</a>
|
30
|
+
]]></description>
|
31
|
+
<lastUpdate>2010-04-07T13:43:33Z</lastUpdate>
|
32
|
+
<status>A</status>
|
33
|
+
<media>
|
34
|
+
<photos>
|
35
|
+
<photo id="1" size="x">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-1-x.jpg</photo>
|
36
|
+
<photo id="1" size="t">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-1-t.jpg</photo>
|
37
|
+
<photo id="1" size="pn">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-1-pn.jpg</photo>
|
38
|
+
<photo id="1" size="pnt">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-1-pnt.jpg</photo>
|
39
|
+
<photo id="1" size="fpm">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-1-fpm.jpg</photo>
|
40
|
+
<photo id="2" size="x">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-2-x.jpg</photo>
|
41
|
+
<photo id="2" size="t">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-2-t.jpg</photo>
|
42
|
+
<photo id="2" size="pn">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-2-pn.jpg</photo>
|
43
|
+
<photo id="2" size="pnt">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-2-pnt.jpg</photo>
|
44
|
+
<photo id="2" size="fpm">http://photocache.petfinder.com/fotos/IL173/IL173.10141994-2-fpm.jpg</photo>
|
45
|
+
</photos>
|
46
|
+
</media>
|
47
|
+
</pet>
|
48
|
+
</petfinder>
|