Skip to content

Commit

Permalink
Merge pull request #1 from mxkay/photo-size-parameter
Browse files Browse the repository at this point in the history
Implement photo size parameter
  • Loading branch information
mtilda authored Apr 10, 2021
2 parents 5227a5b + ac1cdce commit cc1a228
Show file tree
Hide file tree
Showing 16 changed files with 426 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.1
3.0.0
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby

rvm:
- 2.7.1
- 3.0.0

before_script:
- psql -c 'create database mars_curiosity_test;' -U postgres
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

ruby '2.7.1'
ruby '3.0.0'

gem 'pg', '~> 1.0'
gem 'rake'
Expand Down
130 changes: 64 additions & 66 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (6.1.2.1)
actionpack (= 6.1.2.1)
activesupport (= 6.1.2.1)
actioncable (6.1.3.1)
actionpack (= 6.1.3.1)
activesupport (= 6.1.3.1)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.2.1)
actionpack (= 6.1.2.1)
activejob (= 6.1.2.1)
activerecord (= 6.1.2.1)
activestorage (= 6.1.2.1)
activesupport (= 6.1.2.1)
actionmailbox (6.1.3.1)
actionpack (= 6.1.3.1)
activejob (= 6.1.3.1)
activerecord (= 6.1.3.1)
activestorage (= 6.1.3.1)
activesupport (= 6.1.3.1)
mail (>= 2.7.1)
actionmailer (6.1.2.1)
actionpack (= 6.1.2.1)
actionview (= 6.1.2.1)
activejob (= 6.1.2.1)
activesupport (= 6.1.2.1)
actionmailer (6.1.3.1)
actionpack (= 6.1.3.1)
actionview (= 6.1.3.1)
activejob (= 6.1.3.1)
activesupport (= 6.1.3.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.2.1)
actionview (= 6.1.2.1)
activesupport (= 6.1.2.1)
actionpack (6.1.3.1)
actionview (= 6.1.3.1)
activesupport (= 6.1.3.1)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.2.1)
actionpack (= 6.1.2.1)
activerecord (= 6.1.2.1)
activestorage (= 6.1.2.1)
activesupport (= 6.1.2.1)
actiontext (6.1.3.1)
actionpack (= 6.1.3.1)
activerecord (= 6.1.3.1)
activestorage (= 6.1.3.1)
activesupport (= 6.1.3.1)
nokogiri (>= 1.8.5)
actionview (6.1.2.1)
activesupport (= 6.1.2.1)
actionview (6.1.3.1)
activesupport (= 6.1.3.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_model_serializers (0.9.8)
activemodel (>= 3.2)
concurrent-ruby (~> 1.0)
activejob (6.1.2.1)
activesupport (= 6.1.2.1)
activejob (6.1.3.1)
activesupport (= 6.1.3.1)
globalid (>= 0.3.6)
activemodel (6.1.2.1)
activesupport (= 6.1.2.1)
activerecord (6.1.2.1)
activemodel (= 6.1.2.1)
activesupport (= 6.1.2.1)
activestorage (6.1.2.1)
actionpack (= 6.1.2.1)
activejob (= 6.1.2.1)
activerecord (= 6.1.2.1)
activesupport (= 6.1.2.1)
marcel (~> 0.3.1)
mimemagic (~> 0.3.2)
activesupport (6.1.2.1)
activemodel (6.1.3.1)
activesupport (= 6.1.3.1)
activerecord (6.1.3.1)
activemodel (= 6.1.3.1)
activesupport (= 6.1.3.1)
activestorage (6.1.3.1)
actionpack (= 6.1.3.1)
activejob (= 6.1.3.1)
activerecord (= 6.1.3.1)
activesupport (= 6.1.3.1)
marcel (~> 1.0.0)
mini_mime (~> 1.0.2)
activesupport (6.1.3.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand All @@ -82,7 +82,7 @@ GEM
redis (~> 4.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.8.8)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
json (2.3.0)
kaminari (1.2.1)
Expand All @@ -102,19 +102,17 @@ GEM
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
marcel (1.0.1)
method_source (1.0.0)
mimemagic (0.3.5)
mini_mime (1.0.2)
mini_mime (1.0.3)
mini_portile2 (2.5.0)
minitest (5.14.3)
minitest (5.14.4)
newrelic_rpm (6.11.0.365)
nio4r (2.5.5)
nokogiri (1.11.1)
nio4r (2.5.7)
nokogiri (1.11.2)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
nokogiri (1.11.1-x64-mingw32)
nokogiri (1.11.2-x64-mingw32)
racc (~> 1.4)
pg (1.2.3)
pg (1.2.3-x64-mingw32)
Expand All @@ -129,20 +127,20 @@ GEM
rack (>= 2.0.0)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.1.2.1)
actioncable (= 6.1.2.1)
actionmailbox (= 6.1.2.1)
actionmailer (= 6.1.2.1)
actionpack (= 6.1.2.1)
actiontext (= 6.1.2.1)
actionview (= 6.1.2.1)
activejob (= 6.1.2.1)
activemodel (= 6.1.2.1)
activerecord (= 6.1.2.1)
activestorage (= 6.1.2.1)
activesupport (= 6.1.2.1)
rails (6.1.3.1)
actioncable (= 6.1.3.1)
actionmailbox (= 6.1.3.1)
actionmailer (= 6.1.3.1)
actionpack (= 6.1.3.1)
actiontext (= 6.1.3.1)
actionview (= 6.1.3.1)
activejob (= 6.1.3.1)
activemodel (= 6.1.3.1)
activerecord (= 6.1.3.1)
activestorage (= 6.1.3.1)
activesupport (= 6.1.3.1)
bundler (>= 1.15.0)
railties (= 6.1.2.1)
railties (= 6.1.3.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
Expand All @@ -154,9 +152,9 @@ GEM
rails_stdout_logging
rails_serve_static_assets (0.0.5)
rails_stdout_logging (0.0.5)
railties (6.1.2.1)
actionpack (= 6.1.2.1)
activesupport (= 6.1.2.1)
railties (6.1.3.1)
actionpack (= 6.1.3.1)
activesupport (= 6.1.3.1)
method_source
rake (>= 0.8.7)
thor (~> 1.0)
Expand Down Expand Up @@ -229,7 +227,7 @@ DEPENDENCIES
uglifier

RUBY VERSION
ruby 2.7.1p83
ruby 3.0.0p0

BUNDLED WITH
2.1.4
2.2.3
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Along with querying by date, results can also be filtered by the camera with whi
FRONT_HAZCAM_RIGHT_A|Front Hazard Avoidance Camera - Right
REAR_HAZCAM_LEFT|Rear Hazard Avoidance Camera - Left
REAR_HAZCAM_RIGHT|Rear Hazard Avoidance Camera - Right
SKYCAM|MEDA Skycam
SHERLOC_WATSON|SHERLOC WATSON Camera

#### Other rovers

Expand Down Expand Up @@ -81,6 +83,21 @@ If you just want to receive photo data for the most recent Sol for which photos

https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/latest_photos?api_key=DEMO_KEY

#### Size parameter:

Specify the size of the photo you would like to receive. Default value is `large`. Notice only Perseverance photos have a guaranteed width.

https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?api_key=DEMO_KEY&size=small

| Size Parameter Value | Format | Curiosity | Opportunity | Spirit | Perseverance |
|----------------------|--------|:-----------:|:-----------:|:------:|:------------:|
| `small` | JPEG |||||
| `medium` | JPEG |||||
| `large` | JPEG |||||
| `full` | PNG | N/A | N/A | N/A ||

*Note: Only **Perseverance** `small`, `medium`, and `large` size images have guaranteed widths of `300px`, `800px`, and `1200px` respectively. All other dimensions are variable.*

### Mission Manifest Endpoint

A mission manifest is available for each Rover at the `/manifests/<rover_name>`. This manifest will list details of the Rover's mission to help narrow down photo queries to the API. The information in the manifest includes:
Expand Down
33 changes: 14 additions & 19 deletions app/controllers/api/v1/latest_photos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
class Api::V1::LatestPhotosController < ApplicationController
def index
def index
@rover = Rover.find_by name: params[:rover_id].titleize
if @rover
render json: search_photos, each_serializer: PhotoSerializer, root: :latest_photos
else
render json: { errors: "Invalid Rover Name" }, status: :bad_request
end
end
end
@rover = Rover.find_by name: params[:rover_id].titleize

private
if @rover then
validated_params = params
.permit(:rover_id, :camera, :earth_date, :size, :page, :per_page)
.merge(sol: @rover.photos.maximum(:sol))
photos = @rover.photos.search validated_params, @rover
photos = helpers.resize_photos photos, validated_params

def photo_params
params.permit(:camera, :earth_date).merge(sol: @rover.photos.maximum(:sol))
end

def search_photos
photos = @rover.photos.order(:camera_id, :id).search photo_params, @rover
if params[:page]
photos = photos.page(params[:page]).per params[:per_page]
if photos != 'size error'
render json: photos, each_serializer: PhotoSerializer, root: :latest_photos
else
render json: { errors: "Invalid size parameter '#{validated_params[:size]}' for #{@rover.name.titleize}" }, status: :bad_request
end
else
render json: { errors: "Invalid Rover Name" }, status: :bad_request
end
photos
end
end
38 changes: 25 additions & 13 deletions app/controllers/api/v1/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
class Api::V1::PhotosController < ApplicationController
before_action :photo_params

def show
photo = Photo.find params[:id]
render json: photo, serializer: PhotoSerializer, root: :photo
photo = Photo.find @params[:id]
resized_photo = helpers.resize_photo photo, @params

if resized_photo != 'size error'
render json: resized_photo, serializer: PhotoSerializer, root: :photo
else
render json: {
errors: "Invalid size parameter '#{@params[:size]}' for #{photo.rover.name.titleize}"
}, status: :bad_request
end
end

def index
rover = Rover.find_by name: params[:rover_id].titleize
rover = Rover.find_by name: @params[:rover_id].titleize

if rover
render json: photos(rover), each_serializer: PhotoSerializer, root: :photos
photos = rover.photos.search @params, rover
photos = helpers.resize_photos photos, @params

if photos != 'size error'
render json: photos, each_serializer: PhotoSerializer, root: :photos
else
render json: {
errors: "Invalid size parameter '#{@params[:size]}' for #{@params[:rover_id].titleize}"
}, status: :bad_request
end
else
render json: { errors: "Invalid Rover Name" }, status: :bad_request
end
Expand All @@ -16,14 +36,6 @@ def index
private

def photo_params
params.permit :sol, :camera, :earth_date
end

def photos(rover)
photos = rover.photos.order(:camera_id, :id).search photo_params, rover
if params[:page]
photos = photos.page(params[:page]).per params[:per_page]
end
photos
@params = params.permit :id, :rover_id, :sol, :camera, :earth_date, :size, :page, :per_page
end
end
Loading

0 comments on commit cc1a228

Please sign in to comment.