Skip to content

Instantly share code, notes, and snippets.

@kevinzen
Last active April 19, 2016 20:13
Show Gist options
  • Save kevinzen/36c289bace83c08d39fc958f8287fc5f to your computer and use it in GitHub Desktop.
Save kevinzen/36c289bace83c08d39fc958f8287fc5f to your computer and use it in GitHub Desktop.
require 'csv'
class String
def remove_non_ascii(replacement="")
self.gsub(/[\u0080-\u00ff]/, replacement)
end
end
namespace :test do
desc "fetch addresses from google"
task google_geocode: :environment do
# google api key
# AIzaSyBL86-TdZkNYwnpP4xRJAYImogE9NVzwjg
# Google endpoint call
# https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
BASE_URL = "https://maps.googleapis.com"
ENDPOINT = "/maps/api/geocode/json"
API_KEY = 'TdZkNYwnpP4xRJAYImogE9NVzwjg'
camps = Camp.limit(10)
camps.each do | camp |
# these lines create an 'address' based on fields I have. The fields may be blank.
add1 = camp.address_1.remove_non_ascii
city = camp.city.remove_non_ascii
state = camp.state.remove_non_ascii
zip = camp.zip.remove_non_ascii
address_string = "#{add1},+#{city},+#{state}"
decoder_endpoint = "#{BASE_URL}#{ENDPOINT}?address=#{address_string}&KEY=#{API_KEY}"
begin
res = RestClient.get decoder_endpoint
data = JSON.parse(res.body)
puts data.inspect
end # end begin / rescue block
end # end camp loop
end # end task
end #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment