diff --git a/Gemfile b/Gemfile index c18d867d2b271c11e65de384d6e993c1e05af614..a62bbe096a8fa1f9e23b8739f4d6798868084112 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ group :development, :test do gem 'rake' gem 'mongoid', '2.6.0' gem 'bson_ext', :platforms => :ruby + gem 'geoip' gem 'rails' diff --git a/README.md b/README.md index d2ec199f541ac0978f2c880d20d8e9c5c6cc0a08..3c4745c4853da37170fd469f0e50dedad689c529 100644 --- a/README.md +++ b/README.md @@ -420,6 +420,18 @@ Yahoo BOSS is **not a free service**. As of November 17, 2012 Yahoo no longer of * **Limitations**: ? * **Notes**: You must specify which MaxMind service you are using in your configuration. For example: `Geocoder.configure(:maxmind => {:service => :omni})`. +#### MaxMind Local (`:maxmind_local`) + +* **API key**: none. But requires the city database which can be downloaded here http://dev.maxmind.com/geoip/geolite +* **Quota**: none +* **Region**: world +* **SSL support**: N/A +* **Languages**: English +* **Documentation**: http://www.maxmind.com/en/city +* **Terms of Service**: ? +* **Limitations**: ? +* **Notes**: You must specify the path of the MaxMind database in your configuration. For example: `Geocoder.configure(:maxmind_local => {:database => File.join('folder', 'GeoLiteCity.dat')})`. + #### ESRI (`:esri`) * **API key**: none diff --git a/lib/geocoder/lookup.rb b/lib/geocoder/lookup.rb index 054d55cc7f520f530607969e2499224d2bcf4d47..78ac0dc315f428d135fa76448ef7e0a023bc7a9b 100644 --- a/lib/geocoder/lookup.rb +++ b/lib/geocoder/lookup.rb @@ -39,7 +39,7 @@ module Geocoder # All IP address lookup services, default first. # def ip_services - [:freegeoip, :maxmind] + [:freegeoip, :maxmind, :maxmind_local] end ## diff --git a/lib/geocoder/lookups/maxmind_local.rb b/lib/geocoder/lookups/maxmind_local.rb new file mode 100644 index 0000000000000000000000000000000000000000..ae1578f70c83dcd30b9ea04cf301173dbf399d41 --- /dev/null +++ b/lib/geocoder/lookups/maxmind_local.rb @@ -0,0 +1,30 @@ +require 'geocoder/lookups/base' +require 'geocoder/results/maxmind_local' +require 'geoip' + +module Geocoder::Lookup + class MaxmindLocal < Base + + def name + "MaxMind Local" + end + + def required_api_key_parts + [] + end + + private + + def results(query) + if configuration[:database].nil? + raise( + Geocoder::ConfigurationError, + "When using MaxMind Database you MUST specify the path: " + + "Geocoder.configure(:maxmind_local => {:database => ...}), " + ) + end + + [GeoIP.new(configuration[:database]).city(query.to_s)] + end + end +end \ No newline at end of file diff --git a/lib/geocoder/results/maxmind_local.rb b/lib/geocoder/results/maxmind_local.rb new file mode 100644 index 0000000000000000000000000000000000000000..7b8b9d09cd33f050bf0c9c670d720fb2956b8096 --- /dev/null +++ b/lib/geocoder/results/maxmind_local.rb @@ -0,0 +1,44 @@ +require 'geocoder/results/base' + +module Geocoder::Result + class MaxmindLocal < Base + def address(format = :full) + s = state_code.to_s == "" ? "" : ", #{state_code}" + "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "") + end + + def city + @data.city_name + end + + def state + @data.region_name + end + + def state_code + @data.region_code + end + + def country + @data.country_name + end + + def country_code + @data.country_code3 + end + + def postal_code + @data.postal_code + end + + def self.response_attributes + %w[ip] + end + + response_attributes.each do |a| + define_method a do + @data[a] + end + end + end +end \ No newline at end of file diff --git a/test/maxmind_local.rb b/test/maxmind_local.rb new file mode 100644 index 0000000000000000000000000000000000000000..acc912915b9b5579ac5cd28d7a49abb5472afadb --- /dev/null +++ b/test/maxmind_local.rb @@ -0,0 +1,12 @@ +# encoding: utf-8 +require 'test_helper' + +class MaxmindLocalTest < Test::Unit::TestCase + def test_it_requires_database_path + g = Geocoder::Lookup::MaxmindLocal.new + + assert_raise Geocoder::ConfigurationError do + g.search(Geocoder::Query.new('8.8.8.8')).first + end + end +end \ No newline at end of file