diff --git a/lib/geocoder/results/base.rb b/lib/geocoder/results/base.rb index 4c492aeafe13c00d4b21a7ee62f4eb42301bc6bd..85966828966c497f3b4c11361ccb8b92f5504afa 100644 --- a/lib/geocoder/results/base.rb +++ b/lib/geocoder/results/base.rb @@ -11,17 +11,25 @@ module Geocoder end ## - # A two-element array: [lat, lon]. + # A string in the given format. # - def coordinates + def address(format = :full) fail end ## - # A string in the given format. + # A two-element array: [lat, lon]. # - def address(format = :full) - fail + def coordinates + [@data['latitude'].to_f, @data['longitude'].to_f] + end + + def latitude + coordinates[0] + end + + def longitude + coordinates[1] end end end diff --git a/lib/geocoder/results/freegeoip.rb b/lib/geocoder/results/freegeoip.rb index 00388bad11abc7d7c8f7a1dfcb1b33ea81077b93..aebdd20e782417fa8c2a882089e3205b88d156f1 100644 --- a/lib/geocoder/results/freegeoip.rb +++ b/lib/geocoder/results/freegeoip.rb @@ -3,17 +3,13 @@ require 'geocoder/results/base' module Geocoder::Result class Freegeoip < Base - def coordinates - [latitude.to_f, longitude.to_f] - end - def address(format = :full) "#{city}#{', ' + region_code unless region_code == ''} #{zipcode}, #{country_name}" end def self.response_attributes - %w[city region_code region_name metrocode zipcode - latitude longitude country_name country_code ip] + %w[city region_code region_name metrocode + zipcode country_name country_code ip] end response_attributes.each do |a| diff --git a/lib/geocoder/results/yahoo.rb b/lib/geocoder/results/yahoo.rb index ffbf3f71cc013562706111985802460ac47aa540..7af45d554ea1fb82208b4d3a4d5336f1c1677ace 100644 --- a/lib/geocoder/results/yahoo.rb +++ b/lib/geocoder/results/yahoo.rb @@ -3,16 +3,12 @@ require 'geocoder/results/base' module Geocoder::Result class Yahoo < Base - def coordinates - [latitude.to_f, longitude.to_f] - end - def address(format = :full) (1..4).to_a.map{ |i| @data["line#{i}"] }.reject{ |i| i.nil? or i == "" }.join(", ") end def self.response_attributes - %w[quality latitude longitude offsetlat offsetlon radius boundingbox name + %w[quality offsetlat offsetlon radius boundingbox name line1 line2 line3 line4 cross house street xstreet unittype unit postal neighborhood city county state country countrycode statecode countycode level0 level1 level2 level3 level4 level0code level1code level2code diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 61b1a173821914162a2c557dbd837d655573f5e2..61dfb3136bc0ac78aefa095e3d53a425afa7c4d8 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -45,6 +45,11 @@ class GeocoderTest < Test::Unit::TestCase end end + def test_google_result_has_required_attributes + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_result_has_required_attributes(result) + end + # --- Yahoo --- def test_yahoo_result_components Geocoder::Configuration.lookup = :yahoo @@ -59,6 +64,13 @@ class GeocoderTest < Test::Unit::TestCase results.first.address end + def test_yahoo_result_has_required_attributes + Geocoder::Configuration.lookup = :yahoo + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_result_has_required_attributes(result) + end + + # --- FreeGeoIp --- def test_freegeoip_result_on_ip_address_search results = Geocoder.search("74.200.247.59") @@ -70,6 +82,11 @@ class GeocoderTest < Test::Unit::TestCase assert_equal "Plano, TX 75093, United States", results.first.address end + def test_freegeoip_result_has_required_attributes + result = Geocoder.search("74.200.247.59").first + assert_result_has_required_attributes(result) + end + # --- search queries --- def test_ip_address_detection assert Geocoder.send(:ip_address?, "232.65.123.94") @@ -84,4 +101,14 @@ class GeocoderTest < Test::Unit::TestCase assert Geocoder.send(:blank_query?, ", , (-)") assert !Geocoder.send(:blank_query?, "a") end + + + private # ------------------------------------------------------------------ + + def assert_result_has_required_attributes(result) + assert result.coordinates.is_a?(Array) + assert result.latitude.is_a?(Float) + assert result.longitude.is_a?(Float) + assert_not_nil result.address + end end