lib/geocoder/lookups/geoip2.rb in geocoder-1.2.12 vs lib/geocoder/lookups/geoip2.rb in geocoder-1.2.13

- old
+ new

@@ -2,18 +2,22 @@ require 'geocoder/results/geoip2' module Geocoder module Lookup class Geoip2 < Base + attr_reader :gem_name + def initialize unless configuration[:file].nil? begin @gem_name = configuration[:lib] || 'maxminddb' require @gem_name rescue LoadError raise "Could not load Maxmind DB dependency. To use the GeoIP2 lookup you must add the #{@gem_name} gem to your Gemfile or have it installed in your system." end + + @mmdb = db_class.new(configuration[:file].to_s) end super end def name @@ -24,17 +28,17 @@ [] end private + def db_class + gem_name == 'hive_geoip2' ? Hive::GeoIP2 : MaxMindDB + end + def results(query) return [] unless configuration[:file] - 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] + + Array(@mmdb.lookup(query.to_s)) end end end end