Skip to content
Snippets Groups Projects
Commit 9bab1448 authored by Alex Reisner's avatar Alex Reisner
Browse files

Create Geocoder::Lookup module.

Move all network/lookup-related methods into it.
parent fbca35bc
No related branches found
No related tags found
No related merge requests found
require "geocoder/calculations"
require "geocoder/lookup"
##
# Add geocoding functionality (via Google) to any object.
# Add geocoding functionality to any object.
#
module Geocoder
......@@ -29,7 +30,7 @@ module Geocoder
#
scope :near, lambda{ |location, *args|
latitude, longitude = location.is_a?(Array) ?
location : Geocoder.fetch_coordinates(location)
location : Geocoder::Lookup.coordinates(location)
if latitude and longitude
near_scope_options(latitude, longitude, *args)
else
......@@ -184,7 +185,7 @@ module Geocoder
# coordinates as an array: <tt>[lat, lon]</tt>.
#
def fetch_coordinates(save = false)
coords = Geocoder.fetch_coordinates(
coords = Geocoder::Lookup.coordinates(
send(self.class.geocoder_options[:method_name])
)
unless coords.blank?
......@@ -201,59 +202,6 @@ module Geocoder
def fetch_coordinates!
fetch_coordinates(true)
end
##
# Query Google for geographic information about the given phrase.
# Returns a hash representing a valid geocoder response.
# Returns nil if non-200 HTTP response, timeout, or other error.
#
def self.search(query)
doc = _fetch_parsed_response(query)
doc && doc['status'] == "OK" ? doc : nil
end
##
# Query Google for the coordinates of the given phrase.
# Returns array [lat,lon] if found, nil if not found or if network error.
#
def self.fetch_coordinates(query)
return nil unless doc = self.search(query)
# blindly use the first results (assume they are most accurate)
place = doc['results'].first['geometry']['location']
['lat', 'lng'].map{ |i| place[i] }
end
##
# Returns a parsed Google geocoder search result (hash).
# This method is not intended for general use (prefer Geocoder.search).
#
def self._fetch_parsed_response(query)
if doc = _fetch_raw_response(query)
ActiveSupport::JSON.decode(doc)
end
end
##
# Returns a raw Google geocoder search result (JSON).
# This method is not intended for general use (prefer Geocoder.search).
#
def self._fetch_raw_response(query)
return nil if query.blank?
# build URL
params = { :address => query, :sensor => "false" }
url = "http://maps.google.com/maps/api/geocode/json?" + params.to_query
# query geocoder and make sure it responds quickly
begin
resp = nil
timeout(3) do
Net::HTTP.get_response(URI.parse(url)).body
end
rescue SocketError, TimeoutError
return nil
end
end
end
##
......
module Geocoder
module Lookup
extend self
##
# Query Google for the coordinates of the given phrase.
# Returns array [lat,lon] if found, nil if not found or if network error.
#
def coordinates(query)
return nil unless doc = search(query)
# blindly use the first results (assume they are most accurate)
place = doc['results'].first['geometry']['location']
['lat', 'lng'].map{ |i| place[i] }
end
private # ---------------------------------------------------------------
##
# Query Google for geographic information about the given phrase.
# Returns a hash representing a valid geocoder response.
# Returns nil if non-200 HTTP response, timeout, or other error.
#
def search(query)
doc = fetch_parsed_response(query)
doc && doc['status'] == "OK" ? doc : nil
end
##
# Returns a parsed Google geocoder search result (hash).
# This method is not intended for general use (prefer Geocoder.search).
#
def fetch_parsed_response(query)
if doc = fetch_raw_response(query)
ActiveSupport::JSON.decode(doc)
end
end
##
# Returns a raw Google geocoder search result (JSON).
# This method is not intended for general use (prefer Geocoder.search).
#
def fetch_raw_response(query)
return nil if query.blank?
# build URL
params = { :address => query, :sensor => "false" }
url = "http://maps.google.com/maps/api/geocode/json?" + params.to_query
# query geocoder and make sure it responds quickly
begin
resp = nil
timeout(3) do
Net::HTTP.get_response(URI.parse(url)).body
end
rescue SocketError, TimeoutError
return nil
end
end
end
end
......@@ -35,11 +35,13 @@ end
require 'geocoder'
##
# Mock HTTP request to Google.
# Mock HTTP request to geocoding service.
#
module Geocoder
def self._fetch_raw_response(query)
File.read(File.join("test", "fixtures", "madison_square_garden.json"))
module Lookup
def self.fetch_raw_response(query)
File.read(File.join("test", "fixtures", "madison_square_garden.json"))
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment