class AdoptAPet::Pet::ResponseHelper
Class for Adopt-A-Pet Pet
API response object helpers @author Stephen Dolan
Public Class Methods
Extracts photos from a hash and pushes them into the Pet's photos @author Stephen Dolan
@param [Hash] object a JSON response object from the Adopt-A-Pet API
@return [Array<Pet::Photo>] an array of Pet
Photos
# File lib/adopt_a_pet/pet.rb, line 161 def self.extract_photos(object) photos = [] # Get the various photos that could be returned for a pet photos = get_results_photo(object, photos) photos = get_large_results_photo(object, photos) get_other_photos(object, photos) end
Pull a large results photo, if it exists in the API return object @author Stephen Dolan
@param [Hash] object a JSON response object from the Adopt-A-Pet API @param [Array<Pet::Photo>] photos the current collection of Pet
Photos
@return [Array<Pet::Photo>] a (possibly expanded) array of Pet
Photos
# File lib/adopt_a_pet/pet.rb, line 198 def self.get_large_results_photo(object, photos) unless object.dig('large_results_photo_url').nil? photos = photos.push( original: Photo.new( object.dig('large_results_photo_height'), object.dig('large_results_photo_width'), object.dig('large_results_photo_url') ) ) end photos end
Pull any additional photos, if they exists in the API return object @author Stephen Dolan
@param [Hash] object a JSON response object from the Adopt-A-Pet API @param [Array<Pet::Photo>] photos the current collection of Pet
Photos
@return [Array<Pet::Photo>] a (possibly expanded) array of Pet
Photos
# File lib/adopt_a_pet/pet.rb, line 219 def self.get_other_photos(object, photos) if (image_objects = object.dig('images')) image_objects.each do |image| photos = photos.push( original: Photo.new( image.dig('original_height'), image.dig('original_width'), image.dig('original_url') ), thumbnail: Photo.new( image.dig('thumbnail_height'), image.dig('thumbnail_width'), image.dig('thumbnail_url') ) ) end end photos end
Pull a results photo, if it exists in the API return object @author Stephen Dolan
@param [Hash] object a JSON response object from the Adopt-A-Pet API @param [Array<Pet::Photo>] photos the current collection of Pet
Photos
@return [Array<Pet::Photo>] a (possibly expanded) array of Pet
Photos
# File lib/adopt_a_pet/pet.rb, line 177 def self.get_results_photo(object, photos) unless object.dig('results_photo_url').nil? photos = photos.push( original: Photo.new( object.dig('results_photo_height'), object.dig('results_photo_width'), object.dig('results_photo_url') ) ) end photos end