Skip to content
Snippets Groups Projects
Commit a925c709 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge branch 'master' of git://github.com/gonzoyumo/geocoder into gonzoyumo-master

Conflicts:
	README.rdoc
	lib/geocoder.rb
	lib/geocoder/configuration.rb
parents 53113ff8 61a4ddb4
No related branches found
No related tags found
No related merge requests found
......@@ -394,6 +394,17 @@ Yahoo BOSS is **not a free service**. As of November 17, 2012 Yahoo no longer of
* **Terms of Service**: ?
* **Limitations**: ?
#### MaxMind Web Services (`:maxmind`)
* **API key**: required
* **Quota**: Request Packs can be purchased
* **Region**: world
* **SSL support**: no
* **Languages**: English
* **Documentation**: http://www.maxmind.com/app/web_services
* **Terms of Service**: ?
* **Limitations**: ?
Caching
-------
......
......@@ -37,7 +37,7 @@ module Geocoder
# All IP address lookup services, default first.
#
def ip_services
[:freegeoip]
[:freegeoip, :maxmind]
end
##
......
require 'geocoder/lookups/base'
require 'geocoder/results/maxmind'
require 'csv'
module Geocoder::Lookup
class Maxmind < Base
private # ---------------------------------------------------------------
def results(query, reverse = false)
# don't look up a loopback address, just return the stored result
return [reserved_result] if loopback_address?(query)
begin
doc = fetch_data(query, reverse)
if doc && doc.size == 10
return [doc]
else
warn "Maxmind error : #{doc[10]}" if doc
return []
end
rescue StandardError => err
raise_error(err)
return []
end
end
def parse_raw_data(raw_data)
# Maxmind just returns text/plain as csv format but according to documentation,
# we get ISO-8859-1 encoded string. We need to convert it.
CSV.parse_line raw_data.force_encoding("ISO-8859-1").encode("UTF-8")
end
def reserved_result
",,,,0,0,0,0,,"
end
def query_url(query, reverse = false)
"http://geoip3.maxmind.com/f?l=#{Geocoder::Configuration.ip_lookup_api_key}&i=#{query}"
end
end
end
require 'geocoder/results/base'
module Geocoder::Result
class Maxmind < Base
def address(format = :full)
s = state_code.to_s == "" ? "" : ", #{state_code}"
"#{city}#{s} #{postal_code}, #{country_code}".sub(/^[ ,]*/, "")
end
def country_code
@data[0]
end
def state_code
@data[1]
end
def city
@data[2]
end
def postal_code
@data[3]
end
def coordinates
[@data[4].to_f, @data[5].to_f]
end
def metrocode
@data[6]
end
def area_code
@data[7]
end
def isp
@data[8][1,@data[8].length-2]
end
def organization
@data[9][1,@data[9].length-2]
end
def country #not given by MaxMind
country_code
end
def state #not given by MaxMind
state_code
end
end
end
US,TX,Plano,75093,33.034698,-96.813400,623,972,"Layered Technologies , US","Layered Technologies , US"
\ No newline at end of file
,,,,,,,,,,IP_NOT_FOUND
\ No newline at end of file
......@@ -43,4 +43,10 @@ class LookupTest < Test::Unit::TestCase
Geocoder.search("Madison Square Garden, New York, NY 10001, United States")
end
end
def test_maxmind_api_key
Geocoder::Configuration.ip_lookup_api_key = "MY_KEY"
g = Geocoder::Lookup::Maxmind.new
assert_match "l=MY_KEY", g.send(:query_url, "74.200.247.59")
end
end
......@@ -142,6 +142,20 @@ class ServicesTest < Test::Unit::TestCase
assert_equal "Plano, TX 75093, United States", result.address
end
# --- MaxMind ---
def test_maxmind_result_on_ip_address_search
Geocoder::Configuration.ip_lookup = :maxmind
result = Geocoder.search("74.200.247.59").first
assert result.is_a?(Geocoder::Result::Maxmind)
end
def test_maxmind_result_components
Geocoder::Configuration.ip_lookup = :maxmind
result = Geocoder.search("74.200.247.59").first
assert_equal "Plano, TX 75093, US", result.address
end
# --- Bing ---
......
......@@ -161,6 +161,19 @@ module Geocoder
end
end
class Maxmind < Base
private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
file = case query
when "no results"; :no_results
else "74_200_247_59"
end
read_fixture "maxmind_#{file}.txt"
end
end
class Bing < Base
private #-----------------------------------------------------------------
def make_api_request(query)
......
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