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

Ensure all Results return lat/lon as floats.

parent 2df93e02
No related branches found
No related tags found
No related merge requests found
...@@ -11,17 +11,25 @@ module Geocoder ...@@ -11,17 +11,25 @@ module Geocoder
end end
## ##
# A two-element array: [lat, lon]. # A string in the given format.
# #
def coordinates def address(format = :full)
fail fail
end end
## ##
# A string in the given format. # A two-element array: [lat, lon].
# #
def address(format = :full) def coordinates
fail [@data['latitude'].to_f, @data['longitude'].to_f]
end
def latitude
coordinates[0]
end
def longitude
coordinates[1]
end end
end end
end end
......
...@@ -3,17 +3,13 @@ require 'geocoder/results/base' ...@@ -3,17 +3,13 @@ require 'geocoder/results/base'
module Geocoder::Result module Geocoder::Result
class Freegeoip < Base class Freegeoip < Base
def coordinates
[latitude.to_f, longitude.to_f]
end
def address(format = :full) def address(format = :full)
"#{city}#{', ' + region_code unless region_code == ''} #{zipcode}, #{country_name}" "#{city}#{', ' + region_code unless region_code == ''} #{zipcode}, #{country_name}"
end end
def self.response_attributes def self.response_attributes
%w[city region_code region_name metrocode zipcode %w[city region_code region_name metrocode
latitude longitude country_name country_code ip] zipcode country_name country_code ip]
end end
response_attributes.each do |a| response_attributes.each do |a|
......
...@@ -3,16 +3,12 @@ require 'geocoder/results/base' ...@@ -3,16 +3,12 @@ require 'geocoder/results/base'
module Geocoder::Result module Geocoder::Result
class Yahoo < Base class Yahoo < Base
def coordinates
[latitude.to_f, longitude.to_f]
end
def address(format = :full) def address(format = :full)
(1..4).to_a.map{ |i| @data["line#{i}"] }.reject{ |i| i.nil? or i == "" }.join(", ") (1..4).to_a.map{ |i| @data["line#{i}"] }.reject{ |i| i.nil? or i == "" }.join(", ")
end end
def self.response_attributes 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 line1 line2 line3 line4 cross house street xstreet unittype unit postal
neighborhood city county state country countrycode statecode countycode neighborhood city county state country countrycode statecode countycode
level0 level1 level2 level3 level4 level0code level1code level2code level0 level1 level2 level3 level4 level0code level1code level2code
......
...@@ -45,6 +45,11 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -45,6 +45,11 @@ class GeocoderTest < Test::Unit::TestCase
end end
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 --- # --- Yahoo ---
def test_yahoo_result_components def test_yahoo_result_components
Geocoder::Configuration.lookup = :yahoo Geocoder::Configuration.lookup = :yahoo
...@@ -59,6 +64,13 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -59,6 +64,13 @@ class GeocoderTest < Test::Unit::TestCase
results.first.address results.first.address
end 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 --- # --- FreeGeoIp ---
def test_freegeoip_result_on_ip_address_search def test_freegeoip_result_on_ip_address_search
results = Geocoder.search("74.200.247.59") results = Geocoder.search("74.200.247.59")
...@@ -70,6 +82,11 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -70,6 +82,11 @@ class GeocoderTest < Test::Unit::TestCase
assert_equal "Plano, TX 75093, United States", results.first.address assert_equal "Plano, TX 75093, United States", results.first.address
end 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 --- # --- search queries ---
def test_ip_address_detection def test_ip_address_detection
assert Geocoder.send(:ip_address?, "232.65.123.94") assert Geocoder.send(:ip_address?, "232.65.123.94")
...@@ -84,4 +101,14 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -84,4 +101,14 @@ class GeocoderTest < Test::Unit::TestCase
assert Geocoder.send(:blank_query?, ", , (-)") assert Geocoder.send(:blank_query?, ", , (-)")
assert !Geocoder.send(:blank_query?, "a") assert !Geocoder.send(:blank_query?, "a")
end 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 end
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