forked from alexreisner/geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.rb
184 lines (164 loc) · 5.11 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'net/http'
require 'uri'
unless defined?(ActiveSupport::JSON)
begin
require 'rubygems' # for Ruby 1.8
require 'json'
rescue LoadError
raise LoadError, "Please install the 'json' or 'json_pure' gem to parse geocoder results."
end
end
module Geocoder
module Lookup
class Base
##
# Query the geocoding API and return a Geocoder::Result object.
# Returns +nil+ on timeout or error.
#
# Takes a search string (eg: "Mississippi Coast Coliseumf, Biloxi, MS",
# "205.128.54.202") for geocoding, or coordinates (latitude, longitude)
# for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s.
#
def search(query, *args)
# convert coordinates as separate arguments to an array
if query.is_a?(Numeric) and args.first.is_a?(Numeric)
warn "DEPRECATION WARNING: Instead of passing latitude/longitude as separate arguments to the search method, please pass an array: [#{query},#{args.first}]. The old argument format will not be supported in Geocoder v.1.0."
query = [query, args.first]
end
# if coordinates given as string, turn into array
query = query.split(/\s*,\s*/) if coordinates?(query)
if query.is_a?(Array)
reverse = true
query = query.join(',')
else
reverse = false
end
results(query, reverse).map{ |r| result_class.new(r) }
end
##
# Return the URL for a map of the given coordinates.
#
# Not necessarily implemented by all subclasses as only some lookups
# also provide maps.
#
def map_link_url(coordinates)
nil
end
private # -------------------------------------------------------------
##
# Object used to make HTTP requests.
#
def http_client
protocol = "http#{'s' if Geocoder::Configuration.use_https}"
proxy_name = "#{protocol}_proxy"
if proxy = Geocoder::Configuration.send(proxy_name)
proxy_url = protocol + '://' + proxy
begin
uri = URI.parse(proxy_url)
rescue URI::InvalidURIError
raise ConfigurationError,
"Error parsing #{protocol.upcase} proxy URL: '#{proxy_url}'"
end
Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
else
Net::HTTP
end
end
##
# Geocoder::Result object or nil on timeout or other error.
#
def results(query, reverse = false)
fail
end
##
# URL to use for querying the geocoding engine.
#
def query_url(query, reverse = false)
fail
end
##
# Class of the result objects
#
def result_class
eval("Geocoder::Result::#{self.class.to_s.split(":").last}")
end
##
# Returns a parsed search result (Ruby hash).
#
def fetch_data(query, reverse = false)
begin
parse_raw_data fetch_raw_data(query, reverse)
rescue SocketError
warn "Geocoding API connection cannot be established."
rescue TimeoutError
warn "Geocoding API not responding fast enough " +
"(see Geocoder::Configuration.timeout to set limit)."
end
end
##
# Parses a raw search result (returns hash or array).
#
def parse_raw_data(raw_data)
if defined?(ActiveSupport::JSON)
ActiveSupport::JSON.decode(raw_data)
else
begin
JSON.parse(raw_data)
rescue
warn "Geocoding API's response was not valid JSON."
end
end
end
##
# Protocol to use for communication with geocoding services.
# Set in configuration but not available for every service.
#
def protocol
"http" + (Geocoder::Configuration.use_https ? "s" : "")
end
##
# Fetches a raw search result (JSON string).
#
def fetch_raw_data(query, reverse = false)
timeout(Geocoder::Configuration.timeout) do
url = query_url(query, reverse)
unless cache and response = cache[url]
response = http_client.get_response(URI.parse(url)).body
if cache
cache[url] = response
end
end
response
end
end
##
# The working Cache object.
#
def cache
Geocoder.cache
end
##
# Is the given string a loopback IP address?
#
def loopback_address?(ip)
!!(ip == "0.0.0.0" or ip.to_s.match(/^127/))
end
##
# Does the given string look like latitude/longitude coordinates?
#
def coordinates?(value)
value.is_a?(String) and !!value.to_s.match(/^-?[0-9\.]+, *-?[0-9\.]+$/)
end
##
# Simulate ActiveSupport's Object#to_query.
# Removes any keys with nil value.
#
def hash_to_query(hash)
require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
hash.collect{ |p|
p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '='
}.compact.sort * '&'
end
end
end
end