diff --git a/lib/geocoder/results/base.rb b/lib/geocoder/results/base.rb index 85966828966c497f3b4c11361ccb8b92f5504afa..10844002e62615ac8f4aec56db4dd0b1908d137c 100644 --- a/lib/geocoder/results/base.rb +++ b/lib/geocoder/results/base.rb @@ -31,6 +31,14 @@ module Geocoder def longitude coordinates[1] end + + def country + fail + end + + def country_code + fail + end end end end diff --git a/lib/geocoder/results/freegeoip.rb b/lib/geocoder/results/freegeoip.rb index aebdd20e782417fa8c2a882089e3205b88d156f1..c1e6e0ab4283a1f07ee1fa289b3ef4cb242fea58 100644 --- a/lib/geocoder/results/freegeoip.rb +++ b/lib/geocoder/results/freegeoip.rb @@ -4,7 +4,23 @@ module Geocoder::Result class Freegeoip < Base def address(format = :full) - "#{city}#{', ' + region_code unless region_code == ''} #{zipcode}, #{country_name}" + "#{city}#{', ' + region_code unless region_code == ''} #{postal_code}, #{country}" + end + + def city + @data['city'] + end + + def country + @data['country_name'] + end + + def country_code + @data['country_code'] + end + + def postal_code + @data['zipcode'] end def self.response_attributes diff --git a/lib/geocoder/results/google.rb b/lib/geocoder/results/google.rb index 01d7b69a56a684589cc05ec8184a9188244de03c..5e7f44ae704807a54e163f600570bcac268250f0 100644 --- a/lib/geocoder/results/google.rb +++ b/lib/geocoder/results/google.rb @@ -11,6 +11,22 @@ module Geocoder::Result formatted_address end + def city + address_components_of_type(:locality).first['long_name'] + end + + def country + address_components_of_type(:country).first['long_name'] + end + + def country_code + address_components_of_type(:country).first['short_name'] + end + + def postal_code + address_components_of_type(:postal_code).first['long_name'] + end + def types @data['types'] end diff --git a/lib/geocoder/results/yahoo.rb b/lib/geocoder/results/yahoo.rb index 7af45d554ea1fb82208b4d3a4d5336f1c1677ace..1c6ae43083193e8ad7c2ffd08426cb2c2bde91bd 100644 --- a/lib/geocoder/results/yahoo.rb +++ b/lib/geocoder/results/yahoo.rb @@ -7,6 +7,22 @@ module Geocoder::Result (1..4).to_a.map{ |i| @data["line#{i}"] }.reject{ |i| i.nil? or i == "" }.join(", ") end + def city + @data['city'] + end + + def country + @data['country'] + end + + def country_code + @data['countrycode'] + end + + def postal_code + @data['postal'] + end + def self.response_attributes %w[quality offsetlat offsetlon radius boundingbox name line1 line2 line3 line4 cross house street xstreet unittype unit postal diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 65dcdecf7afd411084bf5cd19b82b7e69e4cfac6..1a7dc97cd3981905af467cdcd63558f281f65617 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -134,6 +134,10 @@ class GeocoderTest < Test::Unit::TestCase assert result.coordinates.is_a?(Array) assert result.latitude.is_a?(Float) assert result.longitude.is_a?(Float) + assert result.city.is_a?(String) + assert result.postal_code.is_a?(String) + assert result.country.is_a?(String) + assert result.country_code.is_a?(String) assert_not_nil result.address end end