diff --git a/lib/geocoder/lookups/maxmind_local.rb b/lib/geocoder/lookups/maxmind_local.rb index ae1578f70c83dcd30b9ea04cf301173dbf399d41..0f84014dd1da60821a45497753431ce00656334c 100644 --- a/lib/geocoder/lookups/maxmind_local.rb +++ b/lib/geocoder/lookups/maxmind_local.rb @@ -23,8 +23,8 @@ module Geocoder::Lookup "Geocoder.configure(:maxmind_local => {:database => ...}), " ) end - - [GeoIP.new(configuration[:database]).city(query.to_s)] + + [GeoIP.new(configuration[:database]).city(query.to_s).to_hash] 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 index 7b8b9d09cd33f050bf0c9c670d720fb2956b8096..82dd1d7ff6d1a1080225ba9e8c7ad1eedcebb5f8 100644 --- a/lib/geocoder/results/maxmind_local.rb +++ b/lib/geocoder/results/maxmind_local.rb @@ -3,32 +3,32 @@ require 'geocoder/results/base' module Geocoder::Result class MaxmindLocal < Base def address(format = :full) - s = state_code.to_s == "" ? "" : ", #{state_code}" + s = state.to_s == "" ? "" : ", #{state}" "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "") end def city - @data.city_name + @data[:city_name] end def state - @data.region_name + @data[:region_name] end def state_code - @data.region_code + "" # Not available in Maxmind's database end def country - @data.country_name + @data[:country_name] end def country_code - @data.country_code3 + @data[:country_code3] end def postal_code - @data.postal_code + @data[:postal_code] end def self.response_attributes diff --git a/test/cache_test.rb b/test/cache_test.rb index edd45bbfae62464244570da05ad58195b5ef1e31..7e9a6239ff84635a6b17b4f4f9dc8720fd4fca95 100644 --- a/test/cache_test.rb +++ b/test/cache_test.rb @@ -6,6 +6,7 @@ class CacheTest < Test::Unit::TestCase def test_second_occurrence_of_request_is_cache_hit Geocoder.configure(:cache => {}) Geocoder::Lookup.all_services_except_test.each do |l| + next if l == :maxmind_local # local, does not use cache Geocoder.configure(:lookup => l) set_api_key!(l) results = Geocoder.search("Madison Square Garden") diff --git a/test/error_handling_test.rb b/test/error_handling_test.rb index b4f28774ea90b6624a005b138ee5ce4409411038..cd83c3cdb5b1e51ad88a642076df91f3e1a3d72b 100644 --- a/test/error_handling_test.rb +++ b/test/error_handling_test.rb @@ -22,6 +22,7 @@ class ErrorHandlingTest < Test::Unit::TestCase def test_always_raise_timeout_error Geocoder.configure(:always_raise => [TimeoutError]) Geocoder::Lookup.all_services_except_test.each do |l| + next if l == :maxmind_local # local, does not raise timeout lookup = Geocoder::Lookup.get(l) set_api_key!(l) assert_raises TimeoutError do @@ -33,6 +34,7 @@ class ErrorHandlingTest < Test::Unit::TestCase def test_always_raise_socket_error Geocoder.configure(:always_raise => [SocketError]) Geocoder::Lookup.all_services_except_test.each do |l| + next if l == :maxmind_local # local, does not raise timeout lookup = Geocoder::Lookup.get(l) set_api_key!(l) assert_raises SocketError do diff --git a/test/lookup_test.rb b/test/lookup_test.rb index 185249c657512c62c36ce9267b8b1bca568e98b8..4303eb6f3dc6b0dabdbca4addb40cfd217dd49a8 100644 --- a/test/lookup_test.rb +++ b/test/lookup_test.rb @@ -22,7 +22,7 @@ class LookupTest < Test::Unit::TestCase def test_query_url_contains_values_in_params_hash Geocoder::Lookup.all_services_except_test.each do |l| - next if l == :freegeoip # does not use query string + next if l == :freegeoip || l == :maxmind_local # does not use query string set_api_key!(l) url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new( "test", :params => {:one_in_the_hand => "two in the bush"} diff --git a/test/maxmind_local.rb b/test/maxmind_local.rb index acc912915b9b5579ac5cd28d7a49abb5472afadb..5c0e8fc5122b4bf2ac06ecdb28fefa0cf6867060 100644 --- a/test/maxmind_local.rb +++ b/test/maxmind_local.rb @@ -2,11 +2,16 @@ require 'test_helper' class MaxmindLocalTest < Test::Unit::TestCase - def test_it_requires_database_path + def test_it_returns_the_correct_results g = Geocoder::Lookup::MaxmindLocal.new - assert_raise Geocoder::ConfigurationError do - g.search(Geocoder::Query.new('8.8.8.8')).first - end + result = g.search(Geocoder::Query.new('8.8.8.8')).first + + assert_equal result.address, 'Mountain View, CA 94043, United States' + assert_equal result.city, 'Mountain View' + assert_equal result.state, 'CA' + assert_equal result.country, 'United States' + assert_equal result.country_code, 'USA' + assert_equal result.postal_code, '94043' end end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index b74017d41d0d497d8b61b60ea933e452613cf452..8b1facaf6b95032af6bdddf7ad5f369fc1263082 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -140,6 +140,15 @@ module Geocoder end end + class MaxmindLocal + private + def results query + return [] if query.to_s == "no results" + + [{:request=>"8.8.8.8", :ip=>"8.8.8.8", :country_code2=>"US", :country_code3=>"USA", :country_name=>"United States", :continent_code=>"NA", :region_name=>"CA", :city_name=>"Mountain View", :postal_code=>"94043", :latitude=>37.41919999999999, :longitude=>-122.0574, :dma_code=>807, :area_code=>650, :timezone=>"America/Los_Angeles"}] + end + end + end end