Skip to content

Commit

Permalink
Merge branch 'master' into get_address_1.8.2-sma1970
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-young-atg authored Jan 9, 2024
2 parents 803a3df + 82d8531 commit f7608ab
Show file tree
Hide file tree
Showing 25 changed files with 703 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ group :development, :test do
gem 'rubyzip'
gem 'rails', '~>5.1.0'
gem 'test-unit' # needed for Ruby >=2.2.0
gem 'ip2location_ruby'

platforms :jruby do
gem 'jruby-openssl'
Expand Down
43 changes: 43 additions & 0 deletions README_API_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Global Street Address Lookups
amazon_location_service: {
index_name: 'YOUR_INDEX_NAME_GOES_HERE',
api_key: {
region: 'YOUR_INDEX_REGION',
access_key_id: 'YOUR_AWS_ACCESS_KEY_ID_GOES_HERE',
secret_access_key: 'YOUR_AWS_SECRET_ACCESS_KEY_GOES_HERE',
}
Expand Down Expand Up @@ -499,6 +500,16 @@ Regional Street Address Lookups
* **Documentation**: http://geodata.nationaalgeoregister.nl/
* **Terms of Service**: https://www.pdok.nl/over-pdok - The PDOK services are based on open data and are therefore freely available to everyone.

### pdok NL (`:pdok_nl`)

* **API key**: none
* **Quota**: none
* **Region**: Netherlands
* **SSL support**: yes, required
* **Languages**: Dutch
* **Documentation**: https://api.pdok.nl/bzk/locatieserver/search/v3_1/ui/#/Locatieserver/free
* **Terms of Service**: https://www.pdok.nl/over-pdok - The PDOK services are based on open data and are therefore freely available to everyone.

### Ordnance Survey OpenNames (`:uk_ordnance_survey_names`)

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

### IP2Location.io (`:ip2location_io`)

* **API key**: required (30,000 free queries given on FREE plan)
* **Quota**: up to 600k queries with paid API key
* **Region**: world
* **SSL support**: yes
* **Languages**: English
* **Documentation**: https://www.ip2location.io/ip2location-documentation
* **Terms of Service**: https://www.ip2location.io/terms-of-service


Local IP Address Lookups
------------------------
Expand Down Expand Up @@ -817,4 +838,26 @@ You can generate ActiveRecord migrations and download and import data via provid

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

### IP2Location LITE (`:ip2location_lite`)

This lookup provides methods for geocoding IP addresses without making a call to a remote API (improves speed and availability).

* **API key**: none (requires a IP2Location or FREE IP2Location LITE binary database which can be downloaded from [IP2Location LITE](https://lite.ip2location.com/))
* **Quota**: none
* **Region**: world
* **SSL support**: N/A
* **Languages**: English
* **Documentation**: https://lite.ip2location.com/
* **Terms of Service**: https://lite.ip2location.com/
* **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:

Geocoder.configure(
ip_lookup: :ip2location_lite,
ip2location_lite: {
file: File.join('folder', 'IP2LOCATION-LITE-DB11.BIN')
}
)

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.

Copyright (c) 2009-2021 Alex Reisner, released under the MIT license.
2 changes: 2 additions & 0 deletions geocoder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ Gem::Specification.new do |s|
'source_code_uri' => 'https://github.com/alexreisner/geocoder',
'changelog_uri' => 'https://github.com/alexreisner/geocoder/blob/master/CHANGELOG.md'
}
s.add_dependency("base64", ">= 0.1.0") # for :google_premier lookup
s.add_dependency("csv", ">= 3.0.0") # for :maxmind lookup
end
7 changes: 5 additions & 2 deletions lib/geocoder/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def all_services_except_test
# For example, Amazon Location Service uses the AWS gem, not HTTP REST requests, to fetch data.
#
def all_services_with_http_requests
all_services_except_test - [:amazon_location_service]
all_services_except_test - [:amazon_location_service, :maxmind_local, :geoip2, :ip2location_lite]
end

##
Expand All @@ -48,6 +48,7 @@ def street_services
:uk_ordnance_survey_names,
:opencagedata,
:pelias,
:pdok_nl,
:pickpoint,
:here,
:baidu,
Expand Down Expand Up @@ -95,7 +96,9 @@ def ip_services
:ip2location,
:ipgeolocation,
:ipqualityscore,
:ipbase
:ipbase,
:ip2location_io,
:ip2location_lite
]
end

Expand Down
3 changes: 1 addition & 2 deletions lib/geocoder/lookups/google_places_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ def fields(query)
end

def default_fields
legacy = %w[id reference]
basic = %w[business_status formatted_address geometry icon name
photos place_id plus_code types]
contact = %w[opening_hours]
atmosphere = %W[price_level rating user_ratings_total]
format_fields(legacy, basic, contact, atmosphere)
format_fields(basic, contact, atmosphere)
end

def format_fields(*fields)
Expand Down
62 changes: 62 additions & 0 deletions lib/geocoder/lookups/ip2location_io.rb
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
40 changes: 40 additions & 0 deletions lib/geocoder/lookups/ip2location_lite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'geocoder/lookups/base'
require 'geocoder/results/ip2location_lite'

module Geocoder
module Lookup
class Ip2locationLite < Base
attr_reader :gem_name

def initialize
unless configuration[:file].nil?
begin
@gem_name = 'ip2location_ruby'
require @gem_name
rescue LoadError
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."
end
end
super
end

def name
'IP2LocationLite'
end

def required_api_key_parts
[]
end

private

def results(query)
return [] unless configuration[:file]

i2l = Ip2location.new.open(configuration[:file].to_s)
result = i2l.get_all(query.to_s)
result.nil? ? [] : [result]
end
end
end
end
43 changes: 43 additions & 0 deletions lib/geocoder/lookups/pdok_nl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'geocoder/lookups/base'
require "geocoder/results/pdok_nl"

module Geocoder::Lookup
class PdokNl < Base

def name
'pdok NL'
end

def supported_protocols
[:https]
end

private # ---------------------------------------------------------------

def cache_key(query)
base_query_url(query) + hash_to_query(query_url_params(query))
end

def base_query_url(query)
"#{protocol}://api.pdok.nl/bzk/locatieserver/search/v3_1/free?"
end

def valid_response?(response)
json = parse_json(response.body)
super(response) if json
end

def results(query)
return [] unless doc = fetch_data(query)
return doc['response']['docs']
end

def query_url_params(query)
{
fl: '*',
q: query.text,
wt: 'json'
}.merge(super)
end
end
end
21 changes: 21 additions & 0 deletions lib/geocoder/results/ip2location_io.rb
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
47 changes: 47 additions & 0 deletions lib/geocoder/results/ip2location_lite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'geocoder/results/base'

module Geocoder::Result
class Ip2locationLite < Base

def coordinates
[@data[:latitude], @data[:longitude]]
end

def city
@data[:city]
end

def state
@data[:region]
end

def state_code
"" # Not available in Maxmind's database
end

def country
@data[:country_long]
end

def country_code
@data[:country_short]
end

def postal_code
@data[:zipcode]
end

def self.response_attributes
%w[country_short country_long region latitude longitude isp
domain netspeed areacode iddcode timezone zipcode weatherstationname
weatherstationcode mcc mnc mobilebrand elevation usagetype addresstype
category district asn as]
end

response_attributes.each do |a|
define_method a do
@data[a] || ""
end
end
end
end
Loading

0 comments on commit f7608ab

Please sign in to comment.