Class: AdoptAPet::Pet::ResponseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/adopt_a_pet/pet.rb

Overview

Class for Adopt-A-Pet Pet API response object helpers

Author:

  • Stephen Dolan

Class Method Summary collapse

Class Method Details

.extract_photos(object) ⇒ Array<Pet::Photo>

Extracts photos from a hash and pushes them into the Pet’s photos

Parameters:

  • object (Hash)

    a JSON response object from the Adopt-A-Pet API

Returns:

Author:

  • Stephen Dolan



159
160
161
162
163
164
165
166
# File 'lib/adopt_a_pet/pet.rb', line 159

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

.get_large_results_photo(object, photos) ⇒ Array<Pet::Photo>

Pull a large results photo, if it exists in the API return object

Parameters:

  • object (Hash)

    a JSON response object from the Adopt-A-Pet API

  • photos (Array<Pet::Photo>)

    the current collection of Pet Photos

Returns:

  • (Array<Pet::Photo>)

    a (possibly expanded) array of Pet Photos

Author:

  • Stephen Dolan



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/adopt_a_pet/pet.rb', line 196

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

.get_other_photos(object, photos) ⇒ Array<Pet::Photo>

Pull any additional photos, if they exists in the API return object

Parameters:

  • object (Hash)

    a JSON response object from the Adopt-A-Pet API

  • photos (Array<Pet::Photo>)

    the current collection of Pet Photos

Returns:

  • (Array<Pet::Photo>)

    a (possibly expanded) array of Pet Photos

Author:

  • Stephen Dolan



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/adopt_a_pet/pet.rb', line 217

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

.get_results_photo(object, photos) ⇒ Array<Pet::Photo>

Pull a results photo, if it exists in the API return object

Parameters:

  • object (Hash)

    a JSON response object from the Adopt-A-Pet API

  • photos (Array<Pet::Photo>)

    the current collection of Pet Photos

Returns:

  • (Array<Pet::Photo>)

    a (possibly expanded) array of Pet Photos

Author:

  • Stephen Dolan



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/adopt_a_pet/pet.rb', line 175

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