Skip to content

Instantly share code, notes, and snippets.

@gmassanek
Last active April 10, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmassanek/15089d2b373ae7ed3458bc191a52100e to your computer and use it in GitHub Desktop.
Save gmassanek/15089d2b373ae7ed3458bc191a52100e to your computer and use it in GitHub Desktop.
geocode_member_addresses
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'pp'
member_data = JSON.parse(File.read('members.json'))
member_data.each do |leg_data|
need_geo = leg_data["offices"].select { |o| o["latitude"] == "xxx" }
next if need_geo == []
need_geo.each do |a|
pp address = "#{a["line1"]}, #{a["line2"]} #{a["city"]} #{a["state_code"]} #{a["zip"]}"
parsed_url = URI.encode("https://maps.googleapis.com/maps/api/geocode/json?address=#{address}&key=XXX")
uri = URI(parsed_url)
pp resp = JSON.parse(Net::HTTP.get(uri))
if resp["status"] == "ZERO_RESULTS"
a["latitude"] = "ZERO_RESULTS"
a["longitude"] = "ZERO_RESULTS"
else
location = resp["results"][0]["geometry"]["location"]
pp a["latitude"] = location["lat"]
pp a["longitude"] = location["lng"]
end
end
end
File.write("members2.json", JSON.dump(member_data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment