Skip to content

Commit

Permalink
Merge pull request #75 from chrisccerami/latest_photos
Browse files Browse the repository at this point in the history
Add latest_photos endpoint
  • Loading branch information
Chris C Cerami committed Mar 15, 2018
2 parents e4ffebe + 8ca7f80 commit 42265bf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -56,6 +56,12 @@ https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=1000&camera=

https://api.nasa.gov/mars-photos/api/v1/rovers/opportunity/photos?earth_date=2015-6-3&camera=pancam

#### Query For Latest Photos

If you just want to receive photo data for the most recent Sol for which photos exist for a particular rover, you can visit the `/latest_photos` endpoint.

https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/latest_photos

### 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
26 changes: 26 additions & 0 deletions app/controllers/api/v1/latest_photos_controller.rb
@@ -0,0 +1,26 @@
class Api::V1::LatestPhotosController < ApplicationController
def index
def index
@rover = Rover.find_by name: params[:rover_id].titleize
if @rover
render json: search_photos
else
render json: { errors: "Invalid Rover Name" }, status: :bad_request
end
end
end

private

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]
end
photos
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -7,6 +7,7 @@
namespace :v1 do
resources :rovers, only: [:show, :index] do
resources :photos, only: :index
resources :latest_photos, only: :index
end
resources :photos, only: :show
resources :manifests, only: :show
Expand Down

0 comments on commit 42265bf

Please sign in to comment.