ya_yahoo_geocode 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rvmrc +1 -0
- data/lib/ya_yahoo_geocode.rb +5 -0
- data/lib/ya_yahoo_geocode/client.rb +39 -0
- data/lib/ya_yahoo_geocode/version.rb +1 -1
- data/spec/ya_yahoo_geocode/client_spec.rb +42 -0
- data/ya_yahoo_geocode.gemspec +3 -0
- metadata +29 -16
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm ruby-1.9.2-p180@ya_yahoo_geocode
|
data/lib/ya_yahoo_geocode.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
module YaYahooGeocode
|
2
|
+
|
3
|
+
class Client
|
4
|
+
@@YAHOO_GEOCODE_ENDPOING = 'http://where.yahooapis.com/geocode'
|
5
|
+
|
6
|
+
def search_for_place(search_terms)
|
7
|
+
begin
|
8
|
+
url = @@YAHOO_GEOCODE_ENDPOING + '?q=' + URI::escape(search_terms)
|
9
|
+
response = do_http_get(url)
|
10
|
+
response_hash = Nori.parse(response)['ResultSet']
|
11
|
+
rescue Exception => e
|
12
|
+
raise "Error searching for location: #{search_terms.inspect}. Got error: #{e.message}"
|
13
|
+
end
|
14
|
+
handle_errors(response_hash, search_terms)
|
15
|
+
select_match(response_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def do_http_get(url)
|
20
|
+
Net::HTTP.get_response(URI.parse(url)).body.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def handle_errors(response_hash, search_terms)
|
24
|
+
if response_hash['Error'].to_i > 0
|
25
|
+
raise "Error searching for location: #{search_terms.inspect}. Got error: #{response_hash['ErrorMessage']}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def select_match(response_hash)
|
30
|
+
results = response_hash['Result']
|
31
|
+
if results.is_a? Array
|
32
|
+
results.first
|
33
|
+
else
|
34
|
+
results
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require './lib/ya_yahoo_geocode'
|
2
|
+
|
3
|
+
describe YaYahooGeocode::Client do
|
4
|
+
|
5
|
+
def sample
|
6
|
+
# just copy the output of a successful call
|
7
|
+
<<END
|
8
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
9
|
+
<ResultSet version="1.0"><Error>0</Error><ErrorMessage>No error</ErrorMessage><Locale>us_US</Locale><Quality>50</Quality><Found>1</Found><Result><quality>50</quality><latitude>40.796761</latitude><longitude>-73.942207</longitude><offsetlat>40.796761</offsetlat><offsetlon>-73.942207</offsetlon><radius>2100</radius><name></name><line1></line1><line2>East Harlem, NY 10029</line2><line3></line3><line4>United States</line4><house></house><street></street><xstreet></xstreet><unittype></unittype><unit></unit><postal>10029</postal><neighborhood>East Harlem</neighborhood><city>New York</city><county>New York County</county><state>New York</state><country>United States</country><countrycode>US</countrycode><statecode>NY</statecode><countycode></countycode><uzip>10029</uzip><hash></hash><woeid>23511892</woeid><woetype>22</woetype></Result></ResultSet>
|
10
|
+
<!-- gws25.maps.re3.yahoo.com uncompressed/chunked Fri Jun 10 13:42:37 PDT 2011 -->
|
11
|
+
END
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should instantiate" do
|
15
|
+
YaYahooGeocode::Client.new
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#search_for_place" do
|
19
|
+
context "all goes well" do
|
20
|
+
before :each do
|
21
|
+
@client = YaYahooGeocode::Client.new
|
22
|
+
@client.stub!(:do_http_get).and_return(sample)
|
23
|
+
end
|
24
|
+
it "should find a result for 'East Harlem, NY, NY'" do
|
25
|
+
place_details = @client.search_for_place("East Harlem, NY, NY")
|
26
|
+
place_details['line2'].should include("East Harlem")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
context "error occurs" do
|
30
|
+
before :each do
|
31
|
+
@client = YaYahooGeocode::Client.new
|
32
|
+
@client.stub!(:do_http_get).and_raise("yahoo hates you")
|
33
|
+
end
|
34
|
+
it "should raise an exception" do
|
35
|
+
lambda { @client.search_for_place("East Harlem, NY, NY") }.should raise_error(RuntimeError) { | error|
|
36
|
+
error.message.should include("East Harlem")
|
37
|
+
error.message.should include("Got error")
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/ya_yahoo_geocode.gemspec
CHANGED
@@ -13,6 +13,9 @@ Gem::Specification.new do |s|
|
|
13
13
|
|
14
14
|
s.rubyforge_project = "ya_yahoo_geocode"
|
15
15
|
|
16
|
+
s.add_development_dependency('rspec')
|
17
|
+
s.add_dependency('nori')
|
18
|
+
|
16
19
|
s.files = `git ls-files`.split("\n")
|
17
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
21
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ya_yahoo_geocode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 29
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
5
|
+
version: 0.0.2
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- John Hinnegan
|
@@ -17,8 +12,29 @@ cert_chain: []
|
|
17
12
|
|
18
13
|
date: 2011-06-10 00:00:00 -07:00
|
19
14
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nori
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
22
38
|
description:
|
23
39
|
email:
|
24
40
|
- john@thinknear.com
|
@@ -30,10 +46,13 @@ extra_rdoc_files: []
|
|
30
46
|
|
31
47
|
files:
|
32
48
|
- .gitignore
|
49
|
+
- .rvmrc
|
33
50
|
- Gemfile
|
34
51
|
- Rakefile
|
35
52
|
- lib/ya_yahoo_geocode.rb
|
53
|
+
- lib/ya_yahoo_geocode/client.rb
|
36
54
|
- lib/ya_yahoo_geocode/version.rb
|
55
|
+
- spec/ya_yahoo_geocode/client_spec.rb
|
37
56
|
- ya_yahoo_geocode.gemspec
|
38
57
|
has_rdoc: true
|
39
58
|
homepage: ""
|
@@ -49,18 +68,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
68
|
requirements:
|
50
69
|
- - ">="
|
51
70
|
- !ruby/object:Gem::Version
|
52
|
-
hash: 3
|
53
|
-
segments:
|
54
|
-
- 0
|
55
71
|
version: "0"
|
56
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
73
|
none: false
|
58
74
|
requirements:
|
59
75
|
- - ">="
|
60
76
|
- !ruby/object:Gem::Version
|
61
|
-
hash: 3
|
62
|
-
segments:
|
63
|
-
- 0
|
64
77
|
version: "0"
|
65
78
|
requirements: []
|
66
79
|
|
@@ -69,5 +82,5 @@ rubygems_version: 1.6.2
|
|
69
82
|
signing_key:
|
70
83
|
specification_version: 3
|
71
84
|
summary: This is a gem to help make various calls to yahoo's geo-aware api's
|
72
|
-
test_files:
|
73
|
-
|
85
|
+
test_files:
|
86
|
+
- spec/ya_yahoo_geocode/client_spec.rb
|