Skip to content

Commit

Permalink
Added no_details=2 flag to within_bounding_box endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanTG committed Dec 2, 2024
1 parent f4de536 commit f043422
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/controllers/api/v1/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,25 @@ def closest_by_lat_lon
param :no_details, Integer, desc: 'Omit data that app does not need from pull', required: false
formats %w[json geojson]
def within_bounding_box
except = params[:no_details] ? %i[country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at] : nil
if params[:no_details] == '1'
except = %i[country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at]
except2 = %i[machine_names_first machine_ids num_machines]
elsif params[:no_details] == '2'
except = %i[name street state zip country updated_at location_type_id operator_id country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at]
except2 = []
else
except = []
except2 = %i[machine_names_first machine_ids num_machines]
end

bounds = [params[:swlat], params[:swlon], params[:nelat], params[:nelon]]
if params[:user_faved]
user = User.find(params[:user_faved])
fave_locations = UserFaveLocation.select(:location_id).where(user_id: user)

locations_within = apply_scopes(Location.where(id: fave_locations)).includes(:machines).within_bounding_box(bounds).uniq
elsif params[:no_details] == '2'
locations_within = apply_scopes(Location).within_bounding_box(bounds).uniq
else
locations_within = apply_scopes(Location).includes(:machines).within_bounding_box(bounds).uniq
end
Expand Down Expand Up @@ -243,7 +254,7 @@ def within_bounding_box

if !locations_within.empty?
respond_to do |format|
format.json { return_response(locations_within, 'locations', [], %i[machine_names_first machine_ids num_machines], 200, except) }
format.json { return_response(locations_within, 'locations', [], except2, 200, except) }
format.geojson { render json: container_geojson.to_json }
end
else
Expand Down
34 changes: 34 additions & 0 deletions spec/requests/api/v1/locations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,40 @@
expect(response.body).to include('FeatureCollection')
expect(response.body).to include('Point')
end

it 'respects no_details and shows fewer location fields' do
close_location_one = FactoryBot.create(:location, id: 5555, street: '123 Main St', name: 'Close_1', phone: '111-222-3333', website: 'https://website.gov', region: @region, lat: 45.526112069408704, lon: -122.60884314086321, location_type_id: 1, operator_id: 1)
machine = FactoryBot.create(:machine)
FactoryBot.create(:location_machine_xref, location: close_location_one, machine_id: machine.id)

get '/api/v1/locations/within_bounding_box.json', params: { swlat: 45.478363717877436, swlon: -122.64672405963799, nelat: 45.54521396088108, nelon: -122.56878059990427, no_details: 1}

parsed_body = JSON.parse(response.body)
expect(parsed_body.size).to eq(1)

locations = parsed_body['locations']
expect(response.body).to include('Close_1')
expect(response.body).to include('5555')
expect(response.body).to include('45.5261120')
expect(response.body).to include('-122.608843')
expect(response.body).to include('123 Main St')
expect(response.body).to_not include('111-222-3333')
expect(response.body).to_not include('https://website.gov')

get '/api/v1/locations/within_bounding_box.json', params: { swlat: 45.478363717877436, swlon: -122.64672405963799, nelat: 45.54521396088108, nelon: -122.56878059990427, no_details: 2}

parsed_body = JSON.parse(response.body)
expect(parsed_body.size).to eq(1)

locations = parsed_body['locations']
expect(response.body).to_not include('Close_1')
expect(response.body).to include('5555')
expect(response.body).to include('45.5261120')
expect(response.body).to include('-122.608843')
expect(response.body).to_not include('123 Main St')
expect(response.body).to_not include('111-222-3333')
expect(response.body).to_not include('https://website.gov')
end
end

describe '#machine_details' do
Expand Down

0 comments on commit f043422

Please sign in to comment.