From 872e38ef363d374bce28066e3eb4d4a15877cfcf Mon Sep 17 00:00:00 2001 From: Alec Hipshear Date: Thu, 11 Nov 2010 18:15:27 -0500 Subject: [PATCH] Adds geocoding support to the client module Usage: SimpleGeo::Client.geocode "41 Decatur St, San Francisco, 94103" --- lib/simple_geo/client.rb | 5 +++++ lib/simple_geo/endpoint.rb | 4 ++++ spec/client_spec.rb | 15 +++++++++++++++ spec/fixtures/geocoding.json | 25 +++++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 spec/fixtures/geocoding.json diff --git a/lib/simple_geo/client.rb b/lib/simple_geo/client.rb index 779b753..8c03284 100644 --- a/lib/simple_geo/client.rb +++ b/lib/simple_geo/client.rb @@ -152,6 +152,11 @@ def get_locate(ip) HashUtils.recursively_symbolize_keys(info) end + def geocode(address) + info = get Endpoint.geocode(address) + HashUtils.recursively_symbolize_keys(info) + end + def get(endpoint, data=nil) raise NoConnectionEstablished if @@connection.nil? @@connection.get endpoint, data diff --git a/lib/simple_geo/endpoint.rb b/lib/simple_geo/endpoint.rb index 59601f8..ea4ab51 100644 --- a/lib/simple_geo/endpoint.rb +++ b/lib/simple_geo/endpoint.rb @@ -69,6 +69,10 @@ def locate(ip) endpoint_url "locate/#{ip}.json" end + def geocode(address) + endpoint_url URI.encode("geocode/address.json?q=\"#{address}\"") + end + def endpoint_url(path) [REALM, API_VERSION, path].join('/') end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 1a2c99f..9492f0c 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -1162,6 +1162,21 @@ end end + context "geocoding an address" do + before do + @address = '41 Decatur St, San Francisco, CA' + url = "http://api.simplegeo.com/0.1/geocode/address.json?q=\"#{@address}\"" + uri = URI.parse(URI.encode(url)) + stub_request :get, uri, :fixture_file => 'geocoding.json' + end + + it 'should return a hash with the correct info' do + info = SimpleGeo::Client.geocode(@address) + info[:features][0][:geometry][:coordinates].should == [-122.406032, 37.772502] + info[:address].should == @address + end + end + # this API call seems to always return a 404 # context "getting boundary info by id" do # before do diff --git a/spec/fixtures/geocoding.json b/spec/fixtures/geocoding.json new file mode 100644 index 0000000..1e9645a --- /dev/null +++ b/spec/fixtures/geocoding.json @@ -0,0 +1,25 @@ +{ + "type" : "FeatureCollection", + "features": [ + { + "type" : "Feature", + "properties": { + "number" : "41", + "street" : "Decatur St", + "score" : 0.805, + "precision" : "range", + "zip" : "94103", + "prenum" : "" + }, + "geometry": { + "type" : "Point", + "coordinates": [ + -122.406032, + 37.772502 + ] +} + } + ], + "address" : "41 Decatur St, San Francisco, CA" + } +