Skip to content

Commit f7608ab

Browse files
Merge branch 'master' into get_address_1.8.2-sma1970
2 parents 803a3df + 82d8531 commit f7608ab

25 files changed

+703
-11
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ group :development, :test do
77
gem 'rubyzip'
88
gem 'rails', '~>5.1.0'
99
gem 'test-unit' # needed for Ruby >=2.2.0
10+
gem 'ip2location_ruby'
1011

1112
platforms :jruby do
1213
gem 'jruby-openssl'

README_API_GUIDE.md

+43
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Global Street Address Lookups
5252
amazon_location_service: {
5353
index_name: 'YOUR_INDEX_NAME_GOES_HERE',
5454
api_key: {
55+
region: 'YOUR_INDEX_REGION',
5556
access_key_id: 'YOUR_AWS_ACCESS_KEY_ID_GOES_HERE',
5657
secret_access_key: 'YOUR_AWS_SECRET_ACCESS_KEY_GOES_HERE',
5758
}
@@ -499,6 +500,16 @@ Regional Street Address Lookups
499500
* **Documentation**: http://geodata.nationaalgeoregister.nl/
500501
* **Terms of Service**: https://www.pdok.nl/over-pdok - The PDOK services are based on open data and are therefore freely available to everyone.
501502

503+
### pdok NL (`:pdok_nl`)
504+
505+
* **API key**: none
506+
* **Quota**: none
507+
* **Region**: Netherlands
508+
* **SSL support**: yes, required
509+
* **Languages**: Dutch
510+
* **Documentation**: https://api.pdok.nl/bzk/locatieserver/search/v3_1/ui/#/Locatieserver/free
511+
* **Terms of Service**: https://www.pdok.nl/over-pdok - The PDOK services are based on open data and are therefore freely available to everyone.
512+
502513
### Ordnance Survey OpenNames (`:uk_ordnance_survey_names`)
503514

504515
* **API key**: required (sign up at https://developer.ordnancesurvey.co.uk/os-names-api)
@@ -749,6 +760,16 @@ IP Address Lookups
749760
* **Terms of Service**:
750761
* **Limitations**:
751762

763+
### IP2Location.io (`:ip2location_io`)
764+
765+
* **API key**: required (30,000 free queries given on FREE plan)
766+
* **Quota**: up to 600k queries with paid API key
767+
* **Region**: world
768+
* **SSL support**: yes
769+
* **Languages**: English
770+
* **Documentation**: https://www.ip2location.io/ip2location-documentation
771+
* **Terms of Service**: https://www.ip2location.io/terms-of-service
772+
752773

753774
Local IP Address Lookups
754775
------------------------
@@ -817,4 +838,26 @@ You can generate ActiveRecord migrations and download and import data via provid
817838

818839
You can replace `city` with `country` in any of the above tasks, generators, and configurations.
819840

841+
### IP2Location LITE (`:ip2location_lite`)
842+
843+
This lookup provides methods for geocoding IP addresses without making a call to a remote API (improves speed and availability).
844+
845+
* **API key**: none (requires a IP2Location or FREE IP2Location LITE binary database which can be downloaded from [IP2Location LITE](https://lite.ip2location.com/))
846+
* **Quota**: none
847+
* **Region**: world
848+
* **SSL support**: N/A
849+
* **Languages**: English
850+
* **Documentation**: https://lite.ip2location.com/
851+
* **Terms of Service**: https://lite.ip2location.com/
852+
* **Notes**: **You must download a binary database (BIN) file from IP2Location LITE and set the `:file` configuration option.** Set the path to the database file in your configuration:
853+
854+
Geocoder.configure(
855+
ip_lookup: :ip2location_lite,
856+
ip2location_lite: {
857+
file: File.join('folder', 'IP2LOCATION-LITE-DB11.BIN')
858+
}
859+
)
860+
861+
You must add the *[ip2location_ruby](https://rubygems.org/gems/ip2location_ruby)* gem (pure Ruby implementation) to your Gemfile or have it installed in your system.
862+
820863
Copyright (c) 2009-2021 Alex Reisner, released under the MIT license.

geocoder.gemspec

+2
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ Gem::Specification.new do |s|
2222
'source_code_uri' => 'https://github.com/alexreisner/geocoder',
2323
'changelog_uri' => 'https://github.com/alexreisner/geocoder/blob/master/CHANGELOG.md'
2424
}
25+
s.add_dependency("base64", ">= 0.1.0") # for :google_premier lookup
26+
s.add_dependency("csv", ">= 3.0.0") # for :maxmind lookup
2527
end

lib/geocoder/lookup.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def all_services_except_test
2323
# For example, Amazon Location Service uses the AWS gem, not HTTP REST requests, to fetch data.
2424
#
2525
def all_services_with_http_requests
26-
all_services_except_test - [:amazon_location_service]
26+
all_services_except_test - [:amazon_location_service, :maxmind_local, :geoip2, :ip2location_lite]
2727
end
2828

2929
##
@@ -48,6 +48,7 @@ def street_services
4848
:uk_ordnance_survey_names,
4949
:opencagedata,
5050
:pelias,
51+
:pdok_nl,
5152
:pickpoint,
5253
:here,
5354
:baidu,
@@ -95,7 +96,9 @@ def ip_services
9596
:ip2location,
9697
:ipgeolocation,
9798
:ipqualityscore,
98-
:ipbase
99+
:ipbase,
100+
:ip2location_io,
101+
:ip2location_lite
99102
]
100103
end
101104

lib/geocoder/lookups/google_places_search.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ def fields(query)
4949
end
5050

5151
def default_fields
52-
legacy = %w[id reference]
5352
basic = %w[business_status formatted_address geometry icon name
5453
photos place_id plus_code types]
5554
contact = %w[opening_hours]
5655
atmosphere = %W[price_level rating user_ratings_total]
57-
format_fields(legacy, basic, contact, atmosphere)
56+
format_fields(basic, contact, atmosphere)
5857
end
5958

6059
def format_fields(*fields)
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'geocoder/lookups/base'
2+
require 'geocoder/results/ip2location_io'
3+
4+
module Geocoder::Lookup
5+
class Ip2locationIo < Base
6+
7+
def name
8+
"IP2LocationIOApi"
9+
end
10+
11+
def required_api_key_parts
12+
['key']
13+
end
14+
15+
def supported_protocols
16+
[:http, :https]
17+
end
18+
19+
private # ----------------------------------------------------------------
20+
21+
def base_query_url(query)
22+
"#{protocol}://api.ip2location.io/?"
23+
end
24+
25+
def query_url_params(query)
26+
super.merge(
27+
key: configuration.api_key,
28+
ip: query.sanitized_text,
29+
)
30+
end
31+
32+
def results(query)
33+
# don't look up a loopback or private address, just return the stored result
34+
return [reserved_result(query.text)] if query.internal_ip_address?
35+
return [] unless doc = fetch_data(query)
36+
if doc["response"] == "INVALID ACCOUNT"
37+
raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "INVALID ACCOUNT")
38+
return []
39+
else
40+
return [doc]
41+
end
42+
end
43+
44+
def reserved_result(query)
45+
{
46+
"ip" => "-",
47+
"country_code" => "-",
48+
"country_name" => "-",
49+
"region_name" => "-",
50+
"city_name" => "-",
51+
"latitude" => null,
52+
"longitude" => null,
53+
"zip_code" => "-",
54+
"time_zone" => "-",
55+
"asn" => "-",
56+
"as" => "-",
57+
"is_proxy" => false
58+
}
59+
end
60+
61+
end
62+
end
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'geocoder/lookups/base'
2+
require 'geocoder/results/ip2location_lite'
3+
4+
module Geocoder
5+
module Lookup
6+
class Ip2locationLite < Base
7+
attr_reader :gem_name
8+
9+
def initialize
10+
unless configuration[:file].nil?
11+
begin
12+
@gem_name = 'ip2location_ruby'
13+
require @gem_name
14+
rescue LoadError
15+
raise "Could not load IP2Location DB dependency. To use the IP2LocationLite lookup you must add the #{@gem_name} gem to your Gemfile or have it installed in your system."
16+
end
17+
end
18+
super
19+
end
20+
21+
def name
22+
'IP2LocationLite'
23+
end
24+
25+
def required_api_key_parts
26+
[]
27+
end
28+
29+
private
30+
31+
def results(query)
32+
return [] unless configuration[:file]
33+
34+
i2l = Ip2location.new.open(configuration[:file].to_s)
35+
result = i2l.get_all(query.to_s)
36+
result.nil? ? [] : [result]
37+
end
38+
end
39+
end
40+
end

lib/geocoder/lookups/pdok_nl.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'geocoder/lookups/base'
2+
require "geocoder/results/pdok_nl"
3+
4+
module Geocoder::Lookup
5+
class PdokNl < Base
6+
7+
def name
8+
'pdok NL'
9+
end
10+
11+
def supported_protocols
12+
[:https]
13+
end
14+
15+
private # ---------------------------------------------------------------
16+
17+
def cache_key(query)
18+
base_query_url(query) + hash_to_query(query_url_params(query))
19+
end
20+
21+
def base_query_url(query)
22+
"#{protocol}://api.pdok.nl/bzk/locatieserver/search/v3_1/free?"
23+
end
24+
25+
def valid_response?(response)
26+
json = parse_json(response.body)
27+
super(response) if json
28+
end
29+
30+
def results(query)
31+
return [] unless doc = fetch_data(query)
32+
return doc['response']['docs']
33+
end
34+
35+
def query_url_params(query)
36+
{
37+
fl: '*',
38+
q: query.text,
39+
wt: 'json'
40+
}.merge(super)
41+
end
42+
end
43+
end
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'geocoder/results/base'
2+
3+
module Geocoder::Result
4+
class Ip2locationIo < Base
5+
6+
def address(format = :full)
7+
"#{city_name} #{zip_code}, #{country_name}".sub(/^[ ,]*/, '')
8+
end
9+
10+
def self.response_attributes
11+
%w[ip country_code country_name region_name city_name latitude longitude
12+
zip_code time_zone asn as is_proxy]
13+
end
14+
15+
response_attributes.each do |attr|
16+
define_method attr do
17+
@data[attr] || ""
18+
end
19+
end
20+
end
21+
end
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'geocoder/results/base'
2+
3+
module Geocoder::Result
4+
class Ip2locationLite < Base
5+
6+
def coordinates
7+
[@data[:latitude], @data[:longitude]]
8+
end
9+
10+
def city
11+
@data[:city]
12+
end
13+
14+
def state
15+
@data[:region]
16+
end
17+
18+
def state_code
19+
"" # Not available in Maxmind's database
20+
end
21+
22+
def country
23+
@data[:country_long]
24+
end
25+
26+
def country_code
27+
@data[:country_short]
28+
end
29+
30+
def postal_code
31+
@data[:zipcode]
32+
end
33+
34+
def self.response_attributes
35+
%w[country_short country_long region latitude longitude isp
36+
domain netspeed areacode iddcode timezone zipcode weatherstationname
37+
weatherstationcode mcc mnc mobilebrand elevation usagetype addresstype
38+
category district asn as]
39+
end
40+
41+
response_attributes.each do |a|
42+
define_method a do
43+
@data[a] || ""
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)