Skip to content

Commit

Permalink
don’t break when Petfinder API returns an array of options or breeds
Browse files Browse the repository at this point in the history
use friendlier option descriptions, ignore some possible values
  • Loading branch information
substars committed Sep 26, 2014
1 parent 711c6bf commit 4dbf3f9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/cute_pets/pet_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_petfinder_pet()
pic: get_photo(pet_json),
link: "https://www.petfinder.com/petdetail/#{pet_json['id']['$t']}",
name: pet_json['name']['$t'].capitalize,
description: "#{get_petfinder_option(pet_json['options'])} #{get_petfinder_sex(pet_json['sex']['$t'])} #{pet_json['breeds']['breed']['$t']}".downcase
description: [get_petfinder_option(pet_json['options']), get_petfinder_sex(pet_json['sex']['$t']), get_petfinder_breed(pet_json['breeds'])].compact.join(' ').downcase
}
else
raise 'PetFinder api request failed'
Expand Down Expand Up @@ -79,14 +79,33 @@ def get_petharbor_pet_type
ENV.fetch('petharbor_pet_types').split.sample
end

PETFINDER_ADJECTIVES = {
'housebroken' => 'house trained',
'housetrained' => 'house trained',
'noClaws' => 'declawed',
'altered' => 'altered',
'noDogs' => nil,
'noCats' => nil,
'noKids' => nil,
'hasShots' => nil
}.freeze

def get_petfinder_option(option_hash)
if option_hash['option']
option_hash['option']['$t']
[option_hash['option']].flatten.map { |hsh| PETFINDER_ADJECTIVES[hsh['$t']] }.compact.first
else
option_hash['$t']
end
end

def get_petfinder_breed(breeds)
if breeds['breed'].is_a?(Array)
"#{breeds['breed'].map(&:values).flatten.join('/')} mix"
else
breeds['breed']['$t']
end
end

def self.get_photo(pet)
if !pet['media']['photos']['photo'].nil?
pet['media']['photos']['photo'][2]['$t']
Expand Down
36 changes: 36 additions & 0 deletions spec/pet_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,40 @@
stub_request(:get, /^http\:\/\/www\.petharbor\.com\/petoftheday\.asp/).to_return(:status => 500)
lambda { PetFetcher.get_petharbor_pet }.must_raise RuntimeError
end

describe 'get_petfinder_option' do
it 'uses friendly values' do
PetFetcher.send(:get_petfinder_option, {"option" => {"$t" => "housebroken"}}).must_equal 'house trained'
PetFetcher.send(:get_petfinder_option, {"option" => {"$t" => "housetrained"}}).must_equal 'house trained'
PetFetcher.send(:get_petfinder_option, {"option" => {"$t" => "noClaws"}}).must_equal 'declawed'
PetFetcher.send(:get_petfinder_option, {"option" => {"$t" => "altered"}}).must_equal 'altered'
end

it 'handles multiple values in the options hash' do
PetFetcher.send(:get_petfinder_option,
{"option" => [{"$t" => "hasShots"},
{"$t" => "noClaws"}]}).must_equal 'declawed'
end

it 'ignores some possible values' do
PetFetcher.send(:get_petfinder_option,
{"option" => [{"$t" => "hasShots"},
{"$t" => "noCats"},
{"$t" => "noDogs"},
{"$t" => "noKids"},
{"$t" => "totally not in the xsd"},
]}).must_equal nil

end
end

describe 'get_petfinder_breed' do
it 'works with a single hash' do
PetFetcher.send(:get_petfinder_breed, {"breed" => {"$t" => "Spaniel"}}).must_equal 'Spaniel'
end

it 'works with an array of hashes' do
PetFetcher.send(:get_petfinder_breed, {"breed" => [{"$t" => "Spaniel"}, {"$t" => "Pomeranian"}]}).must_equal 'Spaniel/Pomeranian mix'
end
end
end

0 comments on commit 4dbf3f9

Please sign in to comment.