Skip to content
Snippets Groups Projects
Commit 6b03f839 authored by Fernando Morgenstern's avatar Fernando Morgenstern
Browse files

Adding support for MaxMind's local database.

parent bdb2e1d1
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ group :development, :test do
gem 'rake'
gem 'mongoid', '2.6.0'
gem 'bson_ext', :platforms => :ruby
gem 'geoip'
gem 'rails'
......
......@@ -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
......
......@@ -39,7 +39,7 @@ module Geocoder
# All IP address lookup services, default first.
#
def ip_services
[:freegeoip, :maxmind]
[:freegeoip, :maxmind, :maxmind_local]
end
##
......
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
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
# 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
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