Created
June 9, 2015 21:31
-
-
Save andymayer/9efbb3755aceb211ad17 to your computer and use it in GitHub Desktop.
Uses Google's API to return longitude and latitude for an address location
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 "json" | |
require "rest-client" | |
class Location | |
def self.lookup(location) | |
safe_location = URI.encode(location) | |
lookup = self.get_request(safe_location) | |
begin | |
first_result = lookup["results"][0]["geometry"]["location"] | |
rescue => e | |
first_result = nil | |
end | |
first_result | |
end | |
private | |
def self.get_request(path) | |
begin | |
response = RestClient.get self.api_path(path) | |
rescue => e | |
response = e.response | |
end | |
JSON.parse(response) | |
end | |
def self.post_request(path, params) | |
begin | |
response = RestClient.post( | |
self.api_path(path), | |
params, | |
:content_type => :json, | |
:accept => :json | |
) | |
rescue => e | |
response = e.response | |
end | |
JSON.parse(response) | |
end | |
def self.api_path(path) | |
"http://maps.googleapis.com/maps/api/geocode/json?address=#{path}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment