<% cache(cache_key_for_questionnaires) do %> <%= "id\tapps" %> <% counties = {} amount = 1 @schools.each do |school| 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? redo end lat = results["geometry"]["location"]["lat"] lng = results["geometry"]["location"]["lng"] next if lat.blank? || lng.blank? resp = HTTParty.get("https://geo.fcc.gov/api/census/area?lat=#{lat}&lon=#{lng}&format=json") results = resp.parsed_response["results"] next if results.blank? fips = results[0]["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 %>