-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'geocoder/lookups/base' | ||
require 'geocoder/results/ip2location_io' | ||
|
||
module Geocoder::Lookup | ||
class Ip2locationIo < Base | ||
|
||
def name | ||
"IP2LocationIOApi" | ||
end | ||
|
||
def required_api_key_parts | ||
['key'] | ||
end | ||
|
||
def supported_protocols | ||
[:http, :https] | ||
end | ||
|
||
private # ---------------------------------------------------------------- | ||
|
||
def base_query_url(query) | ||
"#{protocol}://api.ip2location.io/?" | ||
end | ||
|
||
def query_url_params(query) | ||
super.merge( | ||
key: configuration.api_key, | ||
ip: query.sanitized_text, | ||
) | ||
end | ||
|
||
def results(query) | ||
# don't look up a loopback or private address, just return the stored result | ||
return [reserved_result(query.text)] if query.internal_ip_address? | ||
return [] unless doc = fetch_data(query) | ||
if doc["response"] == "INVALID ACCOUNT" | ||
raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "INVALID ACCOUNT") | ||
return [] | ||
else | ||
return [doc] | ||
end | ||
end | ||
|
||
def reserved_result(query) | ||
{ | ||
"ip" => "-", | ||
"country_code" => "-", | ||
"country_name" => "-", | ||
"region_name" => "-", | ||
"city_name" => "-", | ||
"latitude" => null, | ||
"longitude" => null, | ||
"zip_code" => "-", | ||
"time_zone" => "-", | ||
"asn" => "-", | ||
"as" => "-", | ||
"is_proxy" => false | ||
} | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'geocoder/results/base' | ||
|
||
module Geocoder::Result | ||
class Ip2locationIo < Base | ||
|
||
def address(format = :full) | ||
"#{city_name} #{zip_code}, #{country_name}".sub(/^[ ,]*/, '') | ||
end | ||
|
||
def self.response_attributes | ||
%w[ip country_code country_name region_name city_name latitude longitude | ||
zip_code time_zone asn as is_proxy] | ||
end | ||
|
||
response_attributes.each do |attr| | ||
define_method attr do | ||
@data[attr] || "" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"ip":"8.8.8.8", | ||
"country_code":"US", | ||
"country_name":"United States of America", | ||
"region_name":"California", | ||
"city_name":"Mountain View", | ||
"latitude":37.405992, | ||
"longitude":-122.078515, | ||
"zip_code":"94043", | ||
"time_zone":"-07:00", | ||
"asn":"15169", | ||
"as":"Google LLC", | ||
"is_proxy":false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"error": { | ||
"error_code": 10000, | ||
"error_message": "Invalid API key or insufficient query." | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# encoding: utf-8 | ||
require 'test_helper' | ||
|
||
class Ip2locationIoTest < GeocoderTestCase | ||
|
||
def setup | ||
super | ||
Geocoder.configure(ip_lookup: :ip2location_io) | ||
set_api_key!(:ip2location_io) | ||
end | ||
|
||
def test_ip2location_io_query_url | ||
query = Geocoder::Query.new('8.8.8.8') | ||
assert_equal 'http://api.ip2location.io/?ip=8.8.8.8&key=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', query.url | ||
end | ||
|
||
def test_ip2location_io_lookup_address | ||
result = Geocoder.search("8.8.8.8").first | ||
assert_equal "US", result.country_code | ||
assert_equal "United States of America", result.country_name | ||
assert_equal "California", result.region_name | ||
assert_equal "Mountain View", result.city_name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters