Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add geocoding to client #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/simple_geo/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/simple_geo/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions spec/fixtures/geocoding.json
Original file line number Diff line number Diff line change
@@ -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"
}