<% cache(cache_key_for_questionnaires) do %> <%= "id\tapps" %> <% counties = {} amount = 1 @schools.each do |school| require 'net/http' if school.fips_code.blank? next if school.city.blank? || school.state.blank? url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape(school.city)}+#{CGI.escape(school.state)}&sensor=true" resp = Net::HTTP.get_response(URI.parse(url)) # get_response takes an URI object results = JSON.parse(resp.body)["results"][0] if results.blank? redo end lat = results["geometry"]["location"]["lat"] lng = results["geometry"]["location"]["lng"] next if lat.blank? || lng.blank? url = "http://data.fcc.gov/api/block/find?format=json&latitude=#{lat}&longitude=#{lng}&showall=true" resp = Net::HTTP.get_response(URI.parse(url)) # get_response takes an URI object fips = JSON.parse(resp.body)["County"]["FIPS"] next if fips.blank? Fips.create(fips_code: fips, city: school.city, state: school.state) else fips = school.fips_code.fips_code end counties[fips] ||= 0 counties[fips] += amount * school.questionnaire_count end output = "" counties.each do |county, value| output += "#{county}\t#{value}\n" end %> <%= output %> <% end %>