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

Don't try to look up loopback IP address.

Detect this at the Lookup level (instead of the Rack::Request level, for
example) and return the response we know Freegeoip.net will give.
parent 79eb136d
No related branches found
No related tags found
No related merge requests found
......@@ -88,6 +88,13 @@ module Geocoder
end
end
##
# Is the given string a loopback IP address?
#
def loopback_address?(ip)
!!(ip == "0.0.0.0" or ip.match(/^127/))
end
##
# Simulate ActiveSupport's Object#to_query.
#
......
......@@ -7,6 +7,8 @@ module Geocoder::Lookup
private # ---------------------------------------------------------------
def result(query, reverse = false)
# don't look up a loopback address, just return the stored result
return reserved_result(query) if loopback_address?(query)
begin
if doc = fetch_data(query, reverse)
doc
......@@ -16,6 +18,21 @@ module Geocoder::Lookup
end
end
def reserved_result(ip)
{
"ip" => ip,
"city" => "",
"region_code" => "",
"region_name" => "",
"metrocode" => "",
"zipcode" => "",
"latitude" => "0",
"longitude" => "0",
"country_name" => "Reserved",
"country_code" => "RD"
}
end
def query_url(query, reverse = false)
"http://freegeoip.net/json/#{query}"
end
......
......@@ -6,12 +6,7 @@ module Geocoder
def location
unless defined?(@location)
if ip.nil? or ip == "0.0.0.0" or ip.match /^127/ # don't look up loopback
# but return a Geocoder::Result for consistency
@location = Geocoder::Result::Freegeoip.new("ip" => ip)
else
@location = Geocoder.search(ip)
end
@location = Geocoder.search(ip)
end
@location
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment