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

Handle Ipinfo.io responses correctly.

parent c06cb644
No related branches found
No related tags found
No related merge requests found
......@@ -30,25 +30,18 @@ module Geocoder::Lookup
def results(query)
# don't look up a loopback address, just return the stored result
return [reserved_result(query.text)] if query.loopback_ip_address?
if (doc = fetch_data(query)).nil? or doc['code'] == 401 or empty_result?(doc)
if !(doc = fetch_data(query)).is_a?(Hash) or doc['error']
[]
else
[doc]
end
end
def empty_result?(doc)
!doc.is_a?(Hash) or doc.keys == ["ip"]
end
def reserved_result(ip)
{
"ip" => ip,
"city" => "",
"region" => "",
"country" => "",
"loc" => "0,0",
"postal" => ""
"ip" => ip,
"bogon" => true
}
end
......
......@@ -16,10 +16,10 @@ class IpinfoIoTest < GeocoderTestCase
end
def test_ipinfo_io_lookup_loopback_address
Geocoder.configure(:ip_lookup => :ipinfo_io, :use_https => true)
Geocoder.configure(:ip_lookup => :ipinfo_io)
result = Geocoder.search("127.0.0.1").first
assert_equal 0.0, result.longitude
assert_equal 0.0, result.latitude
assert_nil result.latitude
assert_nil result.longitude
assert_equal "127.0.0.1", result.ip
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