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

Merge branch 'master' of git://github.com/roschaefer/geocoder into roschaefer-master

parents 379c6c9a 385aafa0
No related branches found
No related tags found
No related merge requests found
......@@ -856,6 +856,18 @@ This uses the PostcodeAnywhere UK Geocode service, this will geocode any string
* **Documentation**: https://db-ip.com/api/doc.php
* **Terms of Service**: https://db-ip.com/tos.php
#### Ipdata.co (`:ipdata_co`)
* **API key**: optional, see: https://ipdata.co/pricing.html
* **Quota**: 1500/day (up to 600k with paid API keys)
* **Region**: world
* **SSL support**: yes
* **Languages**: English
* **Documentation**: https://ipdata.co/docs.html
* **Terms of Service**: https://ipdata.co/terms.html
* **Limitations**: ?
### IP Address Local Database Services
#### MaxMind Local (`:maxmind_local`) - EXPERIMENTAL
......
......@@ -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 results(query)
Geocoder.configure(:ipdata_co => {:http_headers => { "api-key" => configuration.api_key }}) if configuration.api_key
# 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
def check_response_for_errors!(response)
if response.code.to_i == 403
raise_error(Geocoder::RequestDenied) ||
Geocoder.log(:warn, "Geocoding API error: 403 API key does not exist")
else
super(response)
end
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
8.8.8 does not appear to be an IPv4 or IPv6 address
\ 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_invalid_json
Geocoder.configure(:always_raise => [Geocoder::ResponseParseError])
assert_raise Geocoder::ResponseParseError do
Geocoder.search("8.8.8", ip_address: true)
end
end
def test_result_components
result = Geocoder.search("74.200.247.59").first
assert_equal "Jersey City, NJ 07302, United States", result.address
end
def test_not_authorized
Geocoder.configure(always_raise: [Geocoder::RequestDenied])
lookup = Geocoder::Lookup.get(:ipdata_co)
assert_raises Geocoder::RequestDenied do
response = MockHttpResponse.new(code: 403)
lookup.send(:check_response_for_errors!, response)
end
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