diff --git a/app/api/sally/locations.rb b/app/api/sally/locations.rb index 21eca2b..1ffab31 100644 --- a/app/api/sally/locations.rb +++ b/app/api/sally/locations.rb @@ -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" @@ -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" @@ -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" @@ -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 diff --git a/app/api/sally/trips.rb b/app/api/sally/trips.rb index 42109b8..9bc734e 100644 --- a/app/api/sally/trips.rb +++ b/app/api/sally/trips.rb @@ -16,7 +16,7 @@ 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." @@ -24,7 +24,7 @@ class Trips < Grape::API 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." @@ -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." @@ -54,7 +54,7 @@ class Trips < Grape::API trip.save! - trip + { trip: trip } end desc "Delete a trip." @@ -62,7 +62,7 @@ class Trips < Grape::API 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 diff --git a/spec/api/sally/locations_spec.rb b/spec/api/sally/locations_spec.rb index fe1aaa7..7109e16 100644 --- a/spec/api/sally/locations_spec.rb +++ b/spec/api/sally/locations_spec.rb @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/spec/api/sally/trips_spec.rb b/spec/api/sally/trips_spec.rb index 13abfcf..efc153d 100644 --- a/spec/api/sally/trips_spec.rb +++ b/spec/api/sally/trips_spec.rb @@ -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 @@ -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 @@ -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) @@ -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 @@ -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