diff --git a/app/controllers/manage/dashboard_controller.rb b/app/controllers/manage/dashboard_controller.rb index 75d5be460..7b2f822e0 100644 --- a/app/controllers/manage/dashboard_controller.rb +++ b/app/controllers/manage/dashboard_controller.rb @@ -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 diff --git a/app/views/manage/dashboard/index.html.haml b/app/views/manage/dashboard/index.html.haml index c0a070828..7291650c8 100644 --- a/app/views/manage/dashboard/index.html.haml +++ b/app/views/manage/dashboard/index.html.haml @@ -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 diff --git a/app/views/manage/dashboard/map_data.tsv.erb b/app/views/manage/dashboard/map_data.tsv.erb index 8723d5df5..147f637e2 100644 --- a/app/views/manage/dashboard/map_data.tsv.erb +++ b/app/views/manage/dashboard/map_data.tsv.erb @@ -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") diff --git a/test/controllers/manage/dashboard_controller_test.rb b/test/controllers/manage/dashboard_controller_test.rb index 021def962..f1dad23c1 100644 --- a/test/controllers/manage/dashboard_controller_test.rb +++ b/test/controllers/manage/dashboard_controller_test.rb @@ -45,10 +45,10 @@ 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://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'}) + stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=123+Fake+Street&city=Rochester&state=NY&benchmark=Public_AR_Current&format=json") + .to_return(status: 200, body: '{ "result":{ "addressMatches":[{ "coordinates":{ "x": 100, "y": 100 } }] } }', headers: { 'Content-Type' => 'application/json; charset=UTF-8' }) + 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' }) paths = [ :todays_activity_data,