Skip to content
Snippets Groups Projects
Commit 162b5915 authored by Piotr Górecki's avatar Piotr Górecki Committed by Thu Trang Pham
Browse files

Use a proper domain name in ipapi_com lookup (#1020)

* Add ip-api.com (ip lookup) support

This service returns better coordinates for Europe than other free ip lookup services available in geocoder.

* Clean the results class

Remove default responses for fields.

* Remove checking fields in url query

API already handles unknown fields, so this check is unnecessary.

* Add a test for using parameters directly in search method

* Use proper domain name

Use pro.ip-api.com domain in case when api key is in use, otherwise just ip-api.com.

* Handle invalid api key

* Add forsaken fixture

* Make uniform an invalid key handler with the other geocoder lookups
parent 7f7d3b03
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,8 @@ module Geocoder::Lookup
end
def query_url(query)
url_ = "#{protocol}://ip-api.com/json/#{query.sanitized_text}"
domain = configuration.api_key ? "pro.ip-api.com" : "ip-api.com"
url_ = "#{protocol}://#{domain}/json/#{query.sanitized_text}"
if (params = url_query_string(query)) && !params.empty?
url_ + "?" + params
......@@ -29,17 +30,32 @@ module Geocoder::Lookup
private
def parse_raw_data(raw_data)
if raw_data == "invalid key\n" || raw_data == "invalid key"
invalid_key_result
else
super(raw_data)
end
end
def results(query)
return [reserved_result(query.text)] if query.loopback_ip_address?
(doc = fetch_data(query)) ? [doc] : []
return [] unless doc = fetch_data(query)
if doc["message"] == "invalid key"
raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "Invalid API key.")
return []
else
return [doc]
end
end
def reserved_result(query)
{
"message" => "reserved range",
"query" => query,
"status" => fail,
"status" => "fail",
"ip" => query,
"city" => "",
"region_code" => "",
......@@ -53,6 +69,12 @@ module Geocoder::Lookup
}
end
def invalid_key_result
{
"message" => "invalid key"
}
end
def query_url_params(query)
params = {}
params.merge!(fields: configuration[:fields]) if configuration.has_key?(:fields)
......
invalid key
......@@ -41,6 +41,16 @@ class IpapiComTest < GeocoderTestCase
assert_equal nil, result.message
end
def test_localhost
result = Geocoder.search("::1").first
assert_equal nil, result.lat
assert_equal nil, result.lon
assert_equal [nil, nil], result.coordinates
assert_equal nil, result.reverse
assert_equal "::1", result.query
assert_equal "fail", result.status
end
def test_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::IpapiCom.new
......@@ -50,7 +60,7 @@ class IpapiComTest < GeocoderTestCase
def test_url_with_api_key_and_fields
Geocoder.configure(:api_key => "MY_KEY", :ipapi_com => {:fields => "lat,lon,xyz"})
g = Geocoder::Lookup::IpapiCom.new
assert_equal "http://ip-api.com/json/74.200.247.59?fields=lat%2Clon%2Cxyz&key=MY_KEY", g.query_url(Geocoder::Query.new("74.200.247.59"))
assert_equal "http://pro.ip-api.com/json/74.200.247.59?fields=lat%2Clon%2Cxyz&key=MY_KEY", g.query_url(Geocoder::Query.new("74.200.247.59"))
end
def test_url_with_fields
......@@ -70,4 +80,16 @@ class IpapiComTest < GeocoderTestCase
assert_equal "http://ip-api.com/json/74.200.247.59?fields=lat%2Czip", g.query_url(q)
end
def test_use_https_with_api_key
Geocoder.configure(:api_key => "MY_KEY", :use_https => true)
g = Geocoder::Lookup::IpapiCom.new
assert_equal "https://pro.ip-api.com/json/74.200.247.59?key=MY_KEY", g.query_url(Geocoder::Query.new("74.200.247.59"))
end
def test_invalid_api_key
Geocoder.configure(:api_key => "MY_KEY")
result = Geocoder.search("74.200.247.60").first
assert_equal nil, result
end
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