Last active
April 19, 2016 20:13
-
-
Save kevinzen/36c289bace83c08d39fc958f8287fc5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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