lib/petfinder/client.rb in petfinder-wrap-1.0.4 vs lib/petfinder/client.rb in petfinder-wrap-1.0.4.1

- old
+ new

@@ -34,22 +34,11 @@ def find_pets animal, zip_code find_pets_request = API_BASE_URI + "pet.find?key=#{@api_key}&animal=#{animal}&location=#{zip_code}&output=basic&format=json" # p find_pets_request response = open(find_pets_request).read - res = [] - if resp = JSON.parse(response) - begin - resp["petfinder"]["pets"].each do |pet| - res << Petfinder::Pet.new(pet[1][0]) - end - rescue NoMethodError => e - puts e.message - puts "Invalid response received from API. Check your query" - end - end - res + parse_multiple response, "pet" end def get_shelter id get_shelter_request = API_BASE_URI + "shelter.get?key=#{@api_key}&id=#{id}&format=json" response = open(get_shelter_request).read @@ -70,56 +59,27 @@ end def get_shelter_pets shelter get_shelter_pets = API_BASE_URI + "shelter.getPets?key=#{@api_key}&id=#{shelter.id}&output=basic&format=json" response = open(get_shelter_pets).read - res = [] - if resp = JSON.parse(response) - begin - resp["petfinder"]["pets"]["pet"].each do |pet| - res << Pet.new(pet) - end - rescue NoMethodError => e - puts e.message - puts "Invalid response received from API. Check your query" - end - res - end + parse_multiple response, "pet" end def find_shelters location find_shelters_request = API_BASE_URI + "shelter.find?key=#{@api_key}&location=#{location}&format=json" response = open(find_shelters_request).read - res = [] - if resp = JSON.parse(response) - begin - resp["petfinder"]["shelters"]["shelter"].each do |shelter| - res << Shelter.new(shelter) - end - rescue NoMethodError => e - puts e.message - puts "Invalid response received from API. Check your query" - end - end - res + parse_multiple response, "shelter" end def breeds animal list_breeds_request = API_BASE_URI + "breed.list?key=#{@api_key}&animal=#{animal}&format=json" response = open(list_breeds_request).read - res = [] - if resp = JSON.parse(response) - begin - resp["petfinder"]["breeds"]["breed"].each do |breed| - res << Petfinder::Breed.new(breed["$t"], resp["petfinder"]["breeds"]["@animal"]) - end - rescue NoMethodError => e - puts e.message - puts "Invalid response received from API. Check your query." - end - else - raise Petfinder::Error "No valid JSON response from API" + resp = JSON.parse(response) + animal = (resp)["petfinder"]["breeds"]["@animal"] unless resp + res = parse_multiple response, "breed" + res.each do |breed| + breed.animal = animal end res end def list_shelters_by_breed breed @@ -139,7 +99,29 @@ else raise Petfinder::Error "No valid JSON response received from API" end res end + + private + + def parse_multiple response, type + res = [] + types = type.to_s + "s" + if resp = JSON.parse(response) + begin + resp["petfinder"]["#{types}"]["#{type}"].each do |t| + ruby = "Petfinder::#{type.capitalize}.new(t)" + res << eval(ruby) + end + rescue NoMethodError => e + puts e.message + puts "Invalid response received from API. Check your query." + end + else + raise Petfinder::Error "No valid JSON response from API" + end + res + end + end end