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

Merge pull request #684 from coralmade/master

Add support for Pointp.in ip geolocation
parents 5423959f 3b17a869
No related branches found
No related tags found
No related merge requests found
...@@ -584,6 +584,18 @@ Data Science Toolkit provides an API whose reponse format is like Google's but w ...@@ -584,6 +584,18 @@ Data Science Toolkit provides an API whose reponse format is like Google's but w
* **Limitations**: ? * **Limitations**: ?
* **Notes**: If you are [running your own local instance of the FreeGeoIP service](https://github.com/fiorix/freegeoip) you can configure the host like this: `Geocoder.configure(freegeoip: {host: "..."})`. * **Notes**: If you are [running your own local instance of the FreeGeoIP service](https://github.com/fiorix/freegeoip) you can configure the host like this: `Geocoder.configure(freegeoip: {host: "..."})`.
#### Pointpin (`:pointpin`)
* **API key**: required
* **Quota**: 50,000/mo for €9 through 1m/mo for €49
* **Region**: world
* **SSL support**: yes
* **Languages**: English
* **Documentation**: https://pointp.in/docs/get-started
* **Terms of Service**: https://pointp.in/terms
* **Limitations**: ?
* **Notes**: To use Pointpin set `Geocoder.configure(:ip_lookup => :pointpin, :api_key => "your_pointpin_api_key")`.
#### Telize (`:telize`) #### Telize (`:telize`)
* **API key**: none * **API key**: none
......
...@@ -51,7 +51,8 @@ module Geocoder ...@@ -51,7 +51,8 @@ module Geocoder
:freegeoip, :freegeoip,
:maxmind, :maxmind,
:maxmind_local, :maxmind_local,
:telize :telize,
:pointpin
] ]
end end
......
require 'geocoder/lookups/base'
require 'geocoder/results/pointpin'
module Geocoder::Lookup
class Pointpin < Base
def name
"Pointpin"
end
def required_api_key_parts
["key"]
end
def query_url(query)
"#{ protocol }://geo.pointp.in/#{ api_key }/json/#{ query.sanitized_text }"
end
private
def results(query)
# don't look up a loopback address, just return the stored result
return [] if query.loopback_ip_address?
doc = fetch_data(query)
if doc and doc.is_a?(Hash)
if !data_contains_error?(doc)
return [doc]
elsif doc['error']
case doc['error']
when "Invalid IP address"
raise_error(Geocoder::InvalidRequest) || warn("Invalid Pointpin request.")
when "Invalid API key"
raise_error(Geocoder::InvalidApiKey) || warn("Invalid Pointpin API key.")
when "Address not found"
warn("Address not found.")
end
else
raise_error(Geocoder::Error) || warn("Pointpin server error")
end
end
return []
end
def data_contains_error?(parsed_data)
parsed_data.keys.include?('error')
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 api_key
configuration.api_key
end
end
end
require 'geocoder/results/base'
module Geocoder::Result
class Pointpin < Base
def address
[ city_name, state, postal_code, country ].select{ |i| i.to_s != "" }.join(", ")
end
def city
@data['city_name']
end
def state
@data['region_name']
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['postcode']
end
def self.response_attributes
%w[continent_code ip country_code country_name region_name city_name postcode latitude longitude time_zone languages]
end
response_attributes.each do |a|
define_method a do
@data[a]
end
end
end
end
{"error":"Address not found"}
\ No newline at end of file
{"error":"Invalid IP address"}
\ No newline at end of file
{"ip":"80.111.555.555","continent_code":"EU","country_code":"IE","country_name":"Ireland","region_name":"Dublin City","region_code":"D8","city_name":"Dublin","postcode":"8","latitude":53.3331,"longitude":-6.2489,"time_zone":"Europe/Dublin"}
\ No newline at end of file
{"error":"Address not found"}
\ No newline at end of file
...@@ -150,6 +150,13 @@ module Geocoder ...@@ -150,6 +150,13 @@ module Geocoder
end end
end end
class Pointpin
private
def default_fixture_filename
"pointpin_80_111_555_555"
end
end
class Maxmind class Maxmind
private private
def default_fixture_filename def default_fixture_filename
......
...@@ -23,7 +23,7 @@ class LookupTest < GeocoderTestCase ...@@ -23,7 +23,7 @@ class LookupTest < GeocoderTestCase
def test_query_url_contains_values_in_params_hash def test_query_url_contains_values_in_params_hash
Geocoder::Lookup.all_services_except_test.each do |l| Geocoder::Lookup.all_services_except_test.each do |l|
next if [:freegeoip, :maxmind_local, :telize].include? l # does not use query string next if [:freegeoip, :maxmind_local, :telize, :pointpin].include? l # does not use query string
set_api_key!(l) set_api_key!(l)
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new( url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
"test", :params => {:one_in_the_hand => "two in the bush"} "test", :params => {:one_in_the_hand => "two in the bush"}
...@@ -119,6 +119,12 @@ class LookupTest < GeocoderTestCase ...@@ -119,6 +119,12 @@ class LookupTest < GeocoderTestCase
assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States")) assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
end end
def test_pointpin_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Pointpin.new
assert_match "/MY_KEY/", g.query_url(Geocoder::Query.new("232.65.123.94"))
end
def test_google_api_key def test_google_api_key
Geocoder.configure(:api_key => "MY_KEY") Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Google.new g = Geocoder::Lookup::Google.new
......
# encoding: utf-8
$: << File.join(File.dirname(__FILE__), "..", "..")
require 'test_helper'
class PointpinTest < GeocoderTestCase
def setup
Geocoder.configure(ip_lookup: :pointpin, api_key: "abc123")
end
def test_result_on_ip_address_search
result = Geocoder.search("80.111.555.555").first
assert result.is_a?(Geocoder::Result::Pointpin)
end
def test_result_components
result = Geocoder.search("80.111.555.555").first
assert_equal "Dublin, Dublin City, 8, Ireland", result.address
end
def test_no_results
results = Geocoder.search("10.10.10.10")
assert_equal 0, results.length
end
def test_invalid_address
results = Geocoder.search("555.555.555.555")
assert_equal 0, results.length
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