class AdoptAPet::Pet
Base class for Adopt-A-Pet Pet
objects @author Stephen Dolan
@!attribute id
@return [String] the AdoptAPet identifier for the pet
@!attribute pet_code
@return [String] the shelter/organization identifier for the pet
@!attribute age
@return [String] a description of the age of the pet
@!attribute name
@return [String] the name of the pet
@!attribute size
@return [String] a description of the size of the pet
@!attribute species
@return [String] a description of the pet's species
@!attribute primary_breed
@return [String] a description of the pet's primary breed
@!attribute secondary_breed
@return [String] a description of the pet's secondary breed
@!attribute sex
@return [String] the pet's gender
@!attribute state_code
@return [String] the state where the pet resides
@!attribute city
@return [String] the city where the pet resides
@!attribute details_url
@return [String] a link to the details page for the pet on Adopt-A-Pet
@!attribute photos
@return [Array<Photo>] a set of photos for the pet
@!attribute color
@return [String] a description of the color of the pet
@!attribute description
@return [String] a description of the pet
@!attribute act_quickly
@return [String] a string denoting whether action is required quickly for adoption
@!attribute special_needs
@return [String] a string denoting whether or not the pet has special needs
@!attribute good_with_dogs
@return [String] a string denoting whether or not the pet is good with dogs
@!attribute purebred
@return [String] a string denoting whether or not the pet is purebred
@!attribute good_with_kids
@return [String] a string denoting whether or not the pet is good with kids
@!attribute declawed
@return [String] a string denoting whether or not the pet is declawed
@!attribute spayed_neutered
@return [String] a string denoting whether or not the pet is fixed
@!attribute housetrained
@return [String] a string denoting whether or not the pet is housetrained
@!attribute shots_current
@return [String] a string denoting whether or not the pet is current with their shots
Attributes
Public Class Methods
Create multiple instances of Pet
with one response @author Stephen Dolan
@param [Hash] object a JSON response object from the Adopt-A-Pet API
@return [Array<Pet>] Initialize a collection of Pets
# File lib/adopt_a_pet/pet.rb, line 121 def self.multiple(object) pets = [] object.dig('pets').each do |pet| pets = pets.push(Pet.new(pet)) end pets end
Create a single instance of a Pet
@author Stephen Dolan
@param [Hash] object a JSON response object from the Adopt-A-Pet API
@return [Pet] a new Pet
# File lib/adopt_a_pet/pet.rb, line 87 def initialize(object) # rubocop:disable all extract(object, %w[pet_id], 'id') extract(object, %w[pet_code], 'pet_code') extract(object, %w[age], 'age') extract(object, %w[sex], 'sex') extract(object, %w[size], 'size') extract(object, %w[species], 'species') extract(object, %w[act_quickly], 'act_quickly') extract(object, %w[color], 'color') extract(object, %w[special_needs], 'special_needs') extract(object, %w[purebred], 'purebred') extract(object, %w[description], 'description') extract(object, %w[good_with_dogs], 'good_with_dogs') extract(object, %w[good_with_cats], 'good_with_cats') extract(object, %w[declawed], 'declawed') extract(object, %w[spayed_neutered], 'spayed_neutered') extract(object, %w[housetrained], 'housetrained') extract(object, %w[shots_current], 'shots_current') extract(object, %w[good_with_kids], 'good_with_kids') extract(object, %w[primary_breed], 'primary_breed') extract(object, %w[secondary_breed], 'secondary_breed') extract(object, %w[addr_state_code], 'state_code') extract(object, %w[addr_city], 'city') extract(object, %w[name pet_name], 'name') extract(object, %w[details_url pet_details_url], 'details_url') self.photos = ResponseHelper.extract_photos(object) end
Public Instance Methods
Extract a value from a hash, and store it in an object key @author Stephen Dolan
@param [Hash] object a JSON response object from the Adopt-A-Pet API @param [Array<String>] from_keys an array of keys to try and extract a value from @param [String] to_key the name of the key to store the value in on the Pet
@return [void]
# File lib/adopt_a_pet/pet.rb, line 139 def extract(object, from_keys, to_key) response_item = nil # For each source key: # - Search for a value in the object from_keys&.each do |source| response_item = object.dig(source) unless object.dig(source).nil? end # Set the Pet attribute at to_key to the object value send("#{to_key}=", response_item) end