Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/api/sally/locations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Locations < Grape::API
end
get ":trip_id/locations" do
trip = current_user.trips.where(id: params[:trip_id]).first
trip.locations
{ locations: trip.locations }
end

desc "Creates a location"
Expand All @@ -31,7 +31,7 @@ class Locations < Grape::API
end
post ":trip_id/locations" do
trip = current_user.trips.find(params[:trip_id])
trip.locations.create!(location_params)
{ location: trip.locations.create!(location_params) }
end

desc "Update a location"
Expand All @@ -52,7 +52,7 @@ class Locations < Grape::API
location.speed = location_params[:speed] unless location_params[:speed].blank?

location.save!
location
{ location: location }
end

desc "Deletes a location"
Expand All @@ -61,7 +61,7 @@ class Locations < Grape::API
end
delete ":trip_id/locations/:id" do
trip = current_user.trips.find(params[:trip_id])
trip.locations.find(location_params[:id]).destroy
{ location: trip.locations.find(location_params[:id]).destroy }
end
end

Expand Down
10 changes: 5 additions & 5 deletions app/api/sally/trips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Trips < Grape::API

desc "Returns all trips."
get do
current_user.trips.to_a
{ trips: current_user.trips.to_a }
end

desc "Return a trip."
params do
requires :id, type: Integer, desc: "Trip id."
end
get ":id" do
current_user.trips.where(id: trip_params[:id]).first
{ trip: current_user.trips.where(id: trip_params[:id]).first }
end

desc "Creates a trip."
Expand All @@ -33,7 +33,7 @@ class Trips < Grape::API
requires :start_at, type: Time, desc: "Trip start time"
end
post do
current_user.trips.create!(trip_params)
{ trip: current_user.trips.create!(trip_params) }
end

desc "Update a trip."
Expand All @@ -54,15 +54,15 @@ class Trips < Grape::API

trip.save!

trip
{ trip: trip }
end

desc "Delete a trip."
params do
requires :id, type: String, desc: "Trip ID."
end
delete ":id" do
current_user.trips.find(trip_params[:id]).destroy
{ trip: current_user.trips.find(trip_params[:id]).destroy }
end
end

Expand Down
30 changes: 27 additions & 3 deletions spec/api/sally/locations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
expect(response.response_code).to eq 200
end

it "should have a 'locations' root node" do
get "api/trips/#{@trip.id}/locations", { auth_token: @token }
body = JSON.parse(response.body)
expect(body['locations']).to_not be_empty
end

it "returns all of the trip's locations" do
get "api/trips/#{@trip.id}/locations", { auth_token: @token }
body = JSON.parse(response.body)
expect(body.count).to eq 1
expect(body['locations'].count).to eq 1
end
end

Expand All @@ -35,10 +41,16 @@
end.to change(Location, :count).by(1)
end

it "should have a 'location' root node" do
post "api/trips/#{@trip.id}/locations", { auth_token: @token }.merge(attrs)
body = JSON.parse(response.body)
expect(body['location']).to_not be_blank
end

it "returns the id of the newly created location" do
post "api/trips/#{@trip.id}/locations", { auth_token: @token }.merge(attrs)
body = JSON.parse(response.body)
expect(body['id']).to_not be_blank
expect(body['location']['id']).to_not be_blank
end
end

Expand Down Expand Up @@ -66,6 +78,12 @@
expect(response).to be_success
end

it "should have a 'location' root node" do
put "api/trips/#{@trip.id}/locations/#{@location2.id}", { auth_token: @token }.merge(attrs)
body = JSON.parse(response.body)
expect(body['location']).to_not be_blank
end

it "updates the location's latitude" do
put "api/trips/#{@trip.id}/locations/#{@location2.id}", { auth_token: @token }.merge(attrs)

Expand Down Expand Up @@ -106,10 +124,16 @@
end.to change(Location, :count).by(-1)
end

it "should have a 'location' root node" do
delete "api/trips/#{@trip.id}/locations/#{@new_location.id}", { auth_token: @token }
body = JSON.parse(response.body)
expect(body['location']).to_not be_blank
end

it "returns the deleted location" do
delete "api/trips/#{@trip.id}/locations/#{@new_location.id}", { auth_token: @token }
body = JSON.parse(response.body)
expect(body['id']).to eq(@new_location.id)
expect(body['location']['id']).to eq(@new_location.id)
end
end

Expand Down
43 changes: 36 additions & 7 deletions spec/api/sally/trips_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@
end

describe 'GET trips' do
it "should have a 'trips' root node" do
get 'api/trips', { auth_token: @token }
body = JSON.parse(response.body)
expect(body['trips']).to_not be_empty
end

it "returns the users trips" do
get 'api/trips', { auth_token: @token }
body = JSON.parse(response.body)
expect(body.count).to eq 2
expect(body['trips'].count).to eq 2
end
end

describe 'GET trips/:id' do
it "returns a specific trip when given a trip id" do
it "should have a 'trip' root node" do
get "api/trips/#{@trip1.id}", { auth_token: @token }
body = JSON.parse(response.body)
expect(body['id']).to eq @trip1.id
expect(body['trip']).to_not be_blank
end

it "returns a specific trip when given a trip id" do
get "api/trips/#{@trip1.id}", { auth_token: @token }
body = JSON.parse(response.body)
expect(body['trip']['id']).to eq @trip1.id
end
end

describe 'POST trips' do
Expand All @@ -43,16 +54,22 @@
end.to change(Trip, :count).by(1)
end

it "should have a 'trip' root node" do
post "api/trips", { auth_token: @token }.merge(attrs)
body = JSON.parse(response.body)
expect(body['trip']).to_not be_blank
end

it "should return the id of the newly created trip" do
post "api/trips", { auth_token: @token }.merge(attrs)
body = JSON.parse(response.body)
expect(body['id']).to_not be_blank
expect(body['trip']['id']).to_not be_blank
end

it "should associate the trip with the current user" do
post "api/trips", { auth_token: @token }.merge(attrs)
body = JSON.parse(response.body)
expect(body['user_id']).to eq(@user.id)
expect(body['trip']['user_id']).to eq(@user.id)
end
end

Expand Down Expand Up @@ -87,6 +104,12 @@
expect(response).to be_success
end

it "should have a 'trip' root node" do
put "api/trips/#{@trip.id}", { auth_token: @token }.merge(attrs)
body = JSON.parse(response.body)
expect(body['trip']).to_not be_blank
end

it "should update the trip's name and description" do
put "api/trips/#{@trip.id}", { auth_token: @token }.merge(attrs)

Expand Down Expand Up @@ -127,10 +150,16 @@
end.to change(Trip, :count).by(-1)
end

it "should have a 'trip' root node" do
delete "api/trips/#{@trip.id}", { auth_token: @token }
body = JSON.parse(response.body)
expect(body['trip']).to_not be_blank
end

it "should return the deleted trip" do
delete "api/trips/#{@trip.id}", { auth_token: @token }
body = JSON.parse(response.body)
expect(body['id']).to eq(@trip.id)
expect(body['trip']['id']).to eq(@trip.id)
end
end

Expand Down Expand Up @@ -167,7 +196,7 @@
it "responds with an 'unauthorized (401)' status" do
get 'api/trips'
expect(response.response_code).to eq 401
end
end
end
end