kieranj-google-local-search 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/google_local_search.rb +6 -2
- data/spec/google_local_search_spec.rb +8 -4
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/google_local_search.rb
CHANGED
@@ -5,16 +5,20 @@ module GoogleLocalSearch
|
|
5
5
|
class GeoCode
|
6
6
|
|
7
7
|
GOOGLE_URL = "http://www.google.com/uds/GlocalSearch?"
|
8
|
+
|
9
|
+
class NoMatchFound < StandardError; end
|
10
|
+
|
11
|
+
class Location < Struct.new(:lat, :lng); end
|
8
12
|
|
9
13
|
class << self
|
10
14
|
def locate(address)
|
11
15
|
options = { "rsz" => "small", "v" => "1.0" }
|
12
16
|
options.merge!("q" => CGI.escape(address))
|
13
|
-
|
14
17
|
response = Net::HTTP.get_response(URI.parse(GOOGLE_URL + options.map { |k,v| "#{k}=#{v}" }.join("&")))
|
15
18
|
if response.is_a?(Net::HTTPSuccess)
|
16
19
|
result = JSON.parse(response.body)
|
17
|
-
|
20
|
+
raise NoMatchFound if result['responseData']['results'].empty?
|
21
|
+
location = Location.new(result['responseData']['results'][0]['lat'], result['responseData']['results'][0]['lng'])
|
18
22
|
else
|
19
23
|
response.error!
|
20
24
|
end
|
@@ -14,12 +14,16 @@ describe "GoogleLocalSearch" do
|
|
14
14
|
lambda { GoogleLocalSearch::GeoCode.locate.should raise_error(ArgumentError) }
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should return
|
18
|
-
GoogleLocalSearch::GeoCode.locate("NW1").should be_instance_of(
|
17
|
+
it "should return a Location object" do
|
18
|
+
GoogleLocalSearch::GeoCode.locate("NW1").should be_instance_of(GoogleLocalSearch::GeoCode::Location)
|
19
19
|
end
|
20
20
|
|
21
|
-
it "should have
|
22
|
-
GoogleLocalSearch::GeoCode.locate("NW1").
|
21
|
+
it "should have a latitude" do
|
22
|
+
GoogleLocalSearch::GeoCode.locate("NW1").lat.should_not be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should have a longitude" do
|
26
|
+
GoogleLocalSearch::GeoCode.locate("NW1").lng.should_not be_nil
|
23
27
|
end
|
24
28
|
|
25
29
|
end
|