diff --git a/README.md b/README.md index 9c282efef521da4128d45ede94c9f9a6338dd772..a91b4a441a4c0c03c20e7882a0f1c56fdce093a0 100644 --- a/README.md +++ b/README.md @@ -731,10 +731,14 @@ This lookup provides methods for geocoding IP addresses without making a call to * **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. -**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') }) +To use the hive_geoip2 gem: + + Geocoder.configure(ip_lookup: :geolite2, geolite2: { maxminddb_gem: 'hive_geoip2', file: File.join('folder', 'GeoLite2-City.mmdb') }) Caching ------- diff --git a/lib/geocoder/lookups/geolite2.rb b/lib/geocoder/lookups/geolite2.rb index c0246b6e49ed5ff8d6412c97c4b89bc02aacd3c4..5733d2be0fcb6954206659458c875c7cc4b7422a 100644 --- a/lib/geocoder/lookups/geolite2.rb +++ b/lib/geocoder/lookups/geolite2.rb @@ -7,10 +7,10 @@ module Geocoder def initialize unless configuration[:file].nil? begin - gem_name = 'hive_geoip2' - require gem_name + @gem_name = configuration[:maxminddb_gem] || 'maxminddb' + require @gem_name 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 super @@ -28,7 +28,11 @@ module Geocoder def results(query) 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] end end