Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dashboard): Switches map to use different geocoder, reenables map #224

Merged
merged 6 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/controllers/manage/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
end

def map_data
@schools = School.where("questionnaire_count", 1..Float::INFINITY).select([:city, :state, :questionnaire_count])
@schools = School.where("questionnaire_count", 1..Float::INFINITY).select([:name, :address, :city, :state, :questionnaire_count])
end

def todays_activity_data
Expand Down
11 changes: 5 additions & 6 deletions app/views/manage/dashboard/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

= render "layouts/manage/page_title", title: "Dashboard"

-#
.row
.col
#map
:javascript
$('#map').initMap();
.row
.col
#map
:javascript
$('#map').initMap();

.row
.col-7
Expand Down
15 changes: 9 additions & 6 deletions app/views/manage/dashboard/map_data.tsv.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ redo_limit = 10
if school.fips_code.blank?
next if school.city.blank? || school.state.blank?

resp = HTTParty.get("https://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape(school.city)}+#{CGI.escape(school.state)}&sensor=true")
results = resp.parsed_response["results"][0]
if results.blank?
resp = HTTParty.get("https://geocoding.geo.census.gov/geocoder/locations/address?street=#{CGI.escape(school.address)}&city=#{CGI.escape(school.city)}&state=#{CGI.escape(school.state)}&benchmark=Public_AR_Current&format=json")
result = resp.parsed_response["result"]
if result.blank?
if redo_count >= redo_limit
raise 'Exceeded maximum number of retries: No results from Google Maps API.'
raise 'Exceeded maximum number of retries: No results from Census.gov.'
end
redo_count += 1
redo
end
redo_count = 0

lat = results["geometry"]["location"]["lat"]
lng = results["geometry"]["location"]["lng"]
addressMatches = result["addressMatches"]
next if addressMatches.blank?

lat = result["addressMatches"][0]["coordinates"]["x"]
lng = result["addressMatches"][0]["coordinates"]["y"]
next if lat.blank? || lng.blank?

resp = HTTParty.get("https://geo.fcc.gov/api/census/area?lat=#{lat}&lon=#{lng}&format=json")
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/manage/dashboard_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class Manage::DashboardControllerTest < ActionController::TestCase
FactoryBot.create_list(:questionnaire, 1, school_id: school2.id, acc_status: status)
end

stub_request(:get, "https://maps.googleapis.com/maps/api/geocode/json?address=Rochester%20NY&sensor=true").
to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 100, "lng": 100}}}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'})
stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=1+Lomb+Memorial+Dr&city=Rochester&state=New+York&benchmark=Public_AR_Current&format=json").
cbaudouinjr marked this conversation as resolved.
Show resolved Hide resolved
to_return(status: 200, body: '{"result":{"addressMatches":[{"coordinates":{"x": -77.67469, "y": 43.09212}}]}}', headers: {'Content-Type' => 'application/json; charset=UTF-8'})
cbaudouinjr marked this conversation as resolved.
Show resolved Hide resolved
stub_request(:get, "https://geo.fcc.gov/api/census/area?format=json&lat=100&lon=100").
to_return(status: 200, body: '{"results":[{"country_fips":1234}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'})

Expand Down