From 47229849c6e12d4100386c418ecccf03a243f39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20B=C3=B8tker=20H=C3=B8j?= <christian@skeize.dk> Date: Mon, 20 Oct 2014 10:41:46 +0200 Subject: [PATCH] Add support for pure Ruby access to MaxMind database through maxminddb gem --- README.md | 6 +++++- lib/geocoder/lookups/geolite2.rb | 12 ++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9c282efe..a91b4a44 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 c0246b6e..5733d2be 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 -- GitLab