Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement photo size parameter #147

Open
wants to merge 45 commits into
base: master
Choose a base branch
from

Conversation

mtilda
Copy link

@mtilda mtilda commented Apr 9, 2021

This pull request addresses multiple issues around the consistency and control of photo sizing (#117 and #130).

Going forward, users may pass a size parameter that will specify the size of the returned photos. This is achieved by altering the suffix of each img_src in the photos_controller.rb and latest_photos_controller.rb to correspond with the requested size and rover_id. I stored the logic for resizing in photo_helper.rb so it can be shared between both controllers.

Original suffixes (what is stored in the database)

Curiosity Opportunity Spirit Perseverance
.jpg or .JPG -BR.JPG -BR.JPG _1200.jpg

New suffix outputs

Size Parameter Value Curiosity Opportunity Spirit Perseverance
small -thm.jpg -THM.JPG -THM.JPG _320.jpg
medium -br.jpg -BR.JPG -BR.JPG _800.jpg
large or nil .JPG .JPG .JPG _1200.jpg
full N/A N/A N/A .png

Note: Some of these suffixes are case sensitive

Other changes

  • Move logic for searching photos by params and rover from photos_controller.rb and latest_photos_controller.rb to Photo.search method in models/photo.rb
  • Fix non-breaking syntax error in latest_photos_controller.rb (Non-breaking syntax error in latest_photos_controller.rb #145)
  • Add RSpec test file for latest_photos_controller.rb
  • Add RSpec tests for photos_controller.rb and latest_photos_controller.rb with size parameter
  • Change how img_src is sequenced in spec/factories.rb, so it does not conflict with tests involving size parameter

Potential problems for users

  • When not given a size parameter, photos from Opportunity and Spirit rovers will now be served in their large format; whereas they used to be served as medium.

Notes before merge

I have not tested these changes extensively with Opportunity and Spirit photos, because their scrapers are deprecated. Please test on a full development version of the API with Opportunity and Spirit photos before merging.

app/models/photo.rb Outdated Show resolved Hide resolved

def photo_params
params.permit(:camera, :earth_date, :rover_id).merge(sol: @rover.photos.maximum(:sol))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave these parameters in a photo_params method in order to keep the complexity of the index method down and to separate the responsibilities.

validated_params = params
.permit(:rover_id, :camera, :earth_date, :size, :page, :per_page)
.merge(sol: rover.photos.maximum(:sol))
photos = helpers.search_photos rover, validated_params
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add include PhotoHelper to the top of the controller, you can remove helpers. and also the PhotoHelper:: namespace of the InvalidSizeParameter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read this warning about using the include keyword. Is this something we are concerned with?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I don't think it's relevant, because if the config/routes.rb file is configured correctly there won't be URLs that will reach any potential extra controller actions.

@@ -1,13 +1,29 @@
class Api::V1::PhotosController < ApplicationController
before_action :photo_params
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be done as a before_action. You can instead call the photo_params method where it's needed, instead of setting the @params instance variable.

end
end

def resize_photo photo, params
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I would do here is delete the resize_photo method, and instead call resize_photos([photo]) in the case of the show action. What are your thoughts on that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can make that change pretty easily. The only reason I have two different methods is because I don't expect :rover_id to exist in params in the show action. But I can just params.merge that in before calling resize_photos [photo], params.


def replace_photo_suffix photo, old_suffix_length, new_suffix
new_photo = photo.clone;
new_photo.img_src = photo.img_src[0, photo.img_src.length - old_suffix_length] + new_suffix
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This certainly works, but an alternative would be to use photo.image_src.gsub(old_suffix, new_suffix).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants