Skip to content
Snippets Groups Projects
Unverified Commit 0b7a1abf authored by Robert Schaefer's avatar Robert Schaefer
Browse files

Provide ipdata.co as a lookup

parent b00d68bf
No related branches found
No related tags found
No related merge requests found
......@@ -71,6 +71,7 @@ module Geocoder
:maxmind_geoip2,
:ipinfo_io,
:ipapi_com,
:ipdata_co,
:db_ip_com
]
end
......
require 'geocoder/lookups/base'
require 'geocoder/results/ipdata_co'
module Geocoder::Lookup
class IpdataCo < Base
def name
"ipdata.co"
end
def supported_protocols
[:https]
end
def query_url(query)
"#{protocol}://#{host}/#{query.sanitized_text}"
end
private # ---------------------------------------------------------------
def parse_raw_data(raw_data)
raw_data.match(/^<html><title>404/) ? nil : super(raw_data)
end
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?
# note: Ipdata.co returns plain text on bad request
(doc = fetch_data(query)) ? [doc] : []
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 host
"api.ipdata.co"
end
end
end
require 'geocoder/results/base'
module Geocoder::Result
class IpdataCo < Base
def address(format = :full)
s = state_code.to_s == "" ? "" : ", #{state_code}"
"#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "")
end
def city
@data['city']
end
def state
@data['region']
end
def state_code
@data['region_code']
end
def country
@data['country_name']
end
def country_code
@data['country_code']
end
def postal_code
@data['postal']
end
def self.response_attributes
%w[ip asn organisation currency currency_symbol calling_code flag time_zone is_eu]
end
response_attributes.each do |a|
define_method a do
@data[a]
end
end
end
end
{
"ip": "74.200.247.59",
"city": "Jersey City",
"region": "New Jersey",
"region_code": "NJ",
"country_name": "United States",
"country_code": "US",
"continent_name": "North America",
"continent_code": "NA",
"latitude": 40.7209,
"longitude": -74.0468,
"asn": "AS22576",
"organisation": "DataPipe, Inc.",
"postal": "07302",
"currency": "USD",
"currency_symbol": "$",
"calling_code": "1",
"flag": "https://ipdata.co/flags/us.png",
"time_zone": "America/New_York",
"is_eu": false,
"suspicious_factors": {
"is_tor": false
}
}
\ No newline at end of file
0.0.0 does not appear to be an IPv4 or IPv6 address
\ No newline at end of file
......@@ -373,6 +373,14 @@ module Geocoder
end
end
require 'geocoder/lookups/ipdata_co'
class IpdataCo
private
def default_fixture_filename
"ipdata_co_74_200_247_59"
end
end
require 'geocoder/lookups/ban_data_gouv_fr'
class BanDataGouvFr
private
......
......@@ -19,7 +19,7 @@ class ErrorHandlingTest < GeocoderTestCase
def test_always_raise_response_parse_error
Geocoder.configure(:always_raise => [Geocoder::ResponseParseError])
[:freegeoip, :google, :okf].each do |l|
[:freegeoip, :google, :ipdata_co, :okf].each do |l|
lookup = Geocoder::Lookup.get(l)
set_api_key!(l)
assert_raises Geocoder::ResponseParseError do
......@@ -29,7 +29,7 @@ class ErrorHandlingTest < GeocoderTestCase
end
def test_never_raise_response_parse_error
[:freegeoip, :google, :okf].each do |l|
[:freegeoip, :google, :ipdata_co, :okf].each do |l|
lookup = Geocoder::Lookup.get(l)
set_api_key!(l)
silence_warnings do
......
......@@ -24,7 +24,7 @@ class LookupTest < GeocoderTestCase
def test_query_url_contains_values_in_params_hash
Geocoder::Lookup.all_services_except_test.each do |l|
next if [:freegeoip, :maxmind_local, :telize, :pointpin, :geoip2, :maxmind_geoip2, :mapbox, :ipinfo_io, :ipapi_com].include? l # does not use query string
next if [:freegeoip, :maxmind_local, :telize, :pointpin, :geoip2, :maxmind_geoip2, :mapbox, :ipdata_co, :ipinfo_io, :ipapi_com].include? l # does not use query string
set_api_key!(l)
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
"test", :params => {:one_in_the_hand => "two in the bush"}
......
# encoding: utf-8
require 'test_helper'
class IpdataCoTest < GeocoderTestCase
def setup
Geocoder.configure(ip_lookup: :ipdata_co)
end
def test_result_on_ip_address_search
result = Geocoder.search("74.200.247.59").first
assert result.is_a?(Geocoder::Result::IpdataCo)
end
def test_result_components
result = Geocoder.search("74.200.247.59").first
assert_equal "Jersey City, NJ 07302, United States", result.address
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