Skip to content
Snippets Groups Projects
Commit 47229849 authored by Christian Bøtker Høj's avatar Christian Bøtker Høj
Browse files

Add support for pure Ruby access to MaxMind database through maxminddb gem

parent ccbfc7fe
No related branches found
No related tags found
No related merge requests found
...@@ -731,10 +731,14 @@ This lookup provides methods for geocoding IP addresses without making a call to ...@@ -731,10 +731,14 @@ This lookup provides methods for geocoding IP addresses without making a call to
* **Limitations**: ? * **Limitations**: ?
* **Notes**: **You must download a database from MaxMind and set the `:file` configuration option for local lookups to work.** The GeoLite2 CSV format is not yet supported since it is still in alpha stage. * **Notes**: **You must download a database from MaxMind and set the `:file` configuration option for local lookups to work.** The GeoLite2 CSV format is not yet supported since it is still in alpha stage.
**To use the binary database** you must add the *[hive_geoip2](https://rubygems.org/gems/hive_geoip2)* gem to your Gemfile or have it installed in your system, and specify the path of the MaxMind database in your configuration. For example: **To use the binary database** you must add either the *[hive_geoip2](https://rubygems.org/gems/hive_geoip2)* gem (native extension that relies on libmaxminddb) or the *[maxminddb](http://rubygems.org/gems/maxminddb)* gem (pure Ruby implementation) to your Gemfile or have it installed in your system.
Then specify which gem to use with the `:maxminddb_gem` configuration option, and specify the path of the MaxMind database in your configuration. The pure Ruby gem (maxminddb) will be used as default. For example to use the maxminddb gem:
Geocoder.configure(ip_lookup: :geolite2, geolite2: { file: File.join('folder', 'GeoLite2-City.mmdb') }) Geocoder.configure(ip_lookup: :geolite2, geolite2: { file: File.join('folder', 'GeoLite2-City.mmdb') })
To use the hive_geoip2 gem:
Geocoder.configure(ip_lookup: :geolite2, geolite2: { maxminddb_gem: 'hive_geoip2', file: File.join('folder', 'GeoLite2-City.mmdb') })
Caching Caching
------- -------
......
...@@ -7,10 +7,10 @@ module Geocoder ...@@ -7,10 +7,10 @@ module Geocoder
def initialize def initialize
unless configuration[:file].nil? unless configuration[:file].nil?
begin begin
gem_name = 'hive_geoip2' @gem_name = configuration[:maxminddb_gem] || 'maxminddb'
require gem_name require @gem_name
rescue LoadError rescue LoadError
raise "Could not load maxminddb dependency. To use GeoLite2 lookup you must add the #{gem_name} gem to your Gemfile or have it installed in your system." raise "Could not load Maxmind DB dependency. To use GeoLite2 lookup you must add the #{@gem_name} gem to your Gemfile or have it installed in your system."
end end
end end
super super
...@@ -28,7 +28,11 @@ module Geocoder ...@@ -28,7 +28,11 @@ module Geocoder
def results(query) def results(query)
return [] unless configuration[:file] return [] unless configuration[:file]
result = Hive::GeoIP2.lookup(query.to_s, configuration[:file].to_s) if @gem_name == 'hive_geoip2'
result = Hive::GeoIP2.lookup(query.to_s, configuration[:file].to_s)
else
result = MaxMindDB.new(configuration[:file].to_s).lookup(query.to_s)
end
result.nil? ? [] : [result] result.nil? ? [] : [result]
end 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