Skip to content

Commit

Permalink
Switch to V3 of the Google Geocoding API
Browse files Browse the repository at this point in the history
  • Loading branch information
mlandauer committed Oct 30, 2012
1 parent bca71d8 commit 7134454
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(*params)
end

def self.geocode(address)
r = Geokit::Geocoders::GoogleGeocoder.geocode(address, :bias => "au")
r = Geokit::Geocoders::GoogleGeocoder3.geocode(address, :bias => "au")
r = r.all.find{|l| Location.new(l).in_correct_country?} || r
l = Location.new(r)
l.original_address = address
Expand Down Expand Up @@ -43,7 +43,7 @@ def error
"isn't valid"
elsif !in_correct_country?
"isn't in Australia"
elsif accuracy < 6
elsif accuracy < 5
"isn't complete. We saw that address as \"#{full_address}\" which we don't recognise as a full street address. Check your spelling and make sure to include suburb and state"
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/models/location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe "valid location" do
before :each do
@result = mock(:all => [mock(:country_code => "AU", :lat => -33.772609, :lng => 150.624263, :accuracy => 6)])
Geokit::Geocoders::GoogleGeocoder.should_receive(:geocode).with("24 Bruce Road, Glenbrook, NSW 2773", :bias => "au").and_return(@result)
Geokit::Geocoders::GoogleGeocoder3.should_receive(:geocode).with("24 Bruce Road, Glenbrook, NSW 2773", :bias => "au").and_return(@result)
@loc = Location.geocode("24 Bruce Road, Glenbrook, NSW 2773")
end

Expand All @@ -19,7 +19,7 @@
end

it "should return nil if the address to geocode isn't valid" do
Geokit::Geocoders::GoogleGeocoder.should_receive(:geocode).with("", :bias => "au").and_return(mock(:lat => nil, :lng => nil, :all => []))
Geokit::Geocoders::GoogleGeocoder3.should_receive(:geocode).with("", :bias => "au").and_return(mock(:lat => nil, :lng => nil, :all => []))
l = Location.geocode("")
l.lat.should be_nil
l.lng.should be_nil
Expand All @@ -42,36 +42,36 @@
end

it "should error if the address is empty" do
Geokit::Geocoders::GoogleGeocoder.stub!(:geocode).and_return(mock(:all => []))
Geokit::Geocoders::GoogleGeocoder3.stub!(:geocode).and_return(mock(:all => []))

l = Location.geocode("")
l.error.should == "can't be empty"
end

it "should error if the address is not valid" do
Geokit::Geocoders::GoogleGeocoder.stub!(:geocode).and_return(mock(:lat => nil, :lng => nil, :all => []))
Geokit::Geocoders::GoogleGeocoder3.stub!(:geocode).and_return(mock(:lat => nil, :lng => nil, :all => []))

l = Location.geocode("rxsd23dfj")
l.error.should == "isn't valid"
end

it "should error if the street address is not in australia" do
Geokit::Geocoders::GoogleGeocoder.stub!(:geocode).and_return(mock(:lat => 1, :lng => 2, :country_code => "US", :all => [mock(:lat => 1, :lng => 2, :country_code => "US")]))
Geokit::Geocoders::GoogleGeocoder3.stub!(:geocode).and_return(mock(:lat => 1, :lng => 2, :country_code => "US", :all => [mock(:lat => 1, :lng => 2, :country_code => "US")]))

l = Location.geocode("New York")
l.error.should == "isn't in Australia"
end

it "should not error if there are multiple matches from the geocoder" do
Geokit::Geocoders::GoogleGeocoder.stub!(:geocode).and_return(mock(:all =>
Geokit::Geocoders::GoogleGeocoder3.stub!(:geocode).and_return(mock(:all =>
[mock(:country_code => "AU", :lat => 1, :lng => 2, :accuracy => 6), mock(:country_code => "AU", :lat => 1.1, :lng => 2.1, :accuracy => 6)]))

l = Location.geocode("Bruce Road")
l.error.should be_nil
end

it "should error if the address is not a full street address but rather a suburb name or similar" do
Geokit::Geocoders::GoogleGeocoder.stub!(:geocode).and_return(mock(:all => [mock(:country_code => "AU", :lat => 1, :lng => 2, :accuracy => 5, :full_address => "Glenbrook NSW, Australia")]))
Geokit::Geocoders::GoogleGeocoder3.stub!(:geocode).and_return(mock(:all => [mock(:country_code => "AU", :lat => 1, :lng => 2, :accuracy => 4, :full_address => "Glenbrook NSW, Australia")]))

l = Location.geocode("Glenbrook, NSW")
l.error.should == "isn't complete. We saw that address as \"Glenbrook NSW\" which we don't recognise as a full street address. Check your spelling and make sure to include suburb and state"
Expand All @@ -92,7 +92,7 @@
mock(:full_address => "Bathurst Rd, Riverside, CA, USA", :country_code => "US"),
]
m.stub!(:all => all)
Geokit::Geocoders::GoogleGeocoder.stub!(:geocode).and_return(mock(:all => all))
Geokit::Geocoders::GoogleGeocoder3.stub!(:geocode).and_return(mock(:all => all))
l = Location.geocode("Bathurst Rd")
all = l.all
all.count.should == 3
Expand All @@ -110,7 +110,7 @@
mock(:full_address => "Sowerby St, Burnley, Lancashire BB12 8, UK", :country_code => "UK")
]
m.stub!(:all => all)
Geokit::Geocoders::GoogleGeocoder.stub!(:geocode).and_return(mock(:full_address => "Sowerby St, Lawrence 9532, New Zealand", :all => all))
Geokit::Geocoders::GoogleGeocoder3.stub!(:geocode).and_return(mock(:full_address => "Sowerby St, Lawrence 9532, New Zealand", :all => all))
l = Location.geocode("Sowerby St")
l.full_address.should == "Sowerby St, Garfield NSW 2580"
all = l.all
Expand Down

0 comments on commit 7134454

Please sign in to comment.