ya_yahoo_geocode 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,21 +4,35 @@ module YaYahooGeocode
4
4
  @@YAHOO_GEOCODE_ENDPOINT = 'http://where.yahooapis.com/geocode'
5
5
  @@YAHOO_PLACES_ENDPOINT = 'http://where.yahooapis.com/v1/place'
6
6
 
7
+ attr_accessor :app_id
8
+
9
+ def initialize(app_id = nil)
10
+ self.app_id = app_id
11
+ end
12
+
7
13
  def search_for_place(search_terms)
8
14
  begin
9
15
  url = @@YAHOO_GEOCODE_ENDPOINT + '?q=' + URI::escape(search_terms)
10
- response = do_http_get(url)
11
- response_hash = Nori.parse(response)['ResultSet']
16
+ url += '&appid=' + app_id if app_id #app_id is optional for this service call
17
+ do_search(url)
18
+ rescue Exception => e
19
+ raise "Error searching for location: #{search_terms.inspect}. Got error: #{e.message}"
20
+ end
21
+ end
22
+
23
+ def search_for_place_by_location(lat, lon)
24
+ begin
25
+ url = @@YAHOO_GEOCODE_ENDPOINT + '?q=' + lat.to_s + '+' + lon.to_s + '&gflags=R'
26
+ url += '&appid=' + app_id if app_id #app_id is optional for this service call
27
+ do_search(url)
12
28
  rescue Exception => e
13
29
  raise "Error searching for location: #{search_terms.inspect}. Got error: #{e.message}"
14
30
  end
15
- handle_errors(response_hash, search_terms)
16
- select_match(response_hash)
17
31
  end
18
32
 
19
33
  def get_place_by_woeid(woeid)
20
34
  begin
21
- url = @@YAHOO_PLACES_ENDPOINT + '/' + woeid.to_s + '?appid=' + 'g9.D9ZTV34Ehp4lJeRQCgIWfBYBQ9NumiUYf76y3LFB3xbI7XU1fDB70uJgsBFkQ'
35
+ url = @@YAHOO_PLACES_ENDPOINT + '/' + woeid.to_s + '?appid=' + app_id.to_s #app_id is required
22
36
  response = do_http_get(url)
23
37
  response = Nori.parse(response)
24
38
  response['place']
@@ -27,16 +41,21 @@ module YaYahooGeocode
27
41
  end
28
42
  end
29
43
 
30
-
31
-
32
44
  private
45
+ def do_search(url)
46
+ response = do_http_get(url)
47
+ response_hash = Nori.parse(response)['ResultSet']
48
+ handle_errors(response_hash)
49
+ select_match(response_hash)
50
+ end
51
+
33
52
  def do_http_get(url)
34
53
  Net::HTTP.get_response(URI.parse(url)).body.to_s
35
54
  end
36
55
 
37
- def handle_errors(response_hash, search_terms)
56
+ def handle_errors(response_hash)
38
57
  if response_hash['Error'].to_i > 0
39
- raise "Error searching for location: #{search_terms.inspect}. Got error: #{response_hash['ErrorMessage']}"
58
+ raise "#{response_hash['ErrorMessage']}"
40
59
  end
41
60
  end
42
61
 
@@ -1,3 +1,3 @@
1
1
  module YaYahooGeocode
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,100 @@
1
+ require './lib/ya_yahoo_geocode'
2
+
3
+ describe YaYahooGeocode::Client do
4
+
5
+ def sample_search_for_place
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
+ def sample_search_for_place_by_woeid
15
+ <<END
16
+ <?xml version="1.0" encoding="UTF-8"?>
17
+ <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>
18
+ <!-- gws25.maps.re3.yahoo.com uncompressed/chunked Fri Jun 10 13:42:37 PDT 2011 -->
19
+ END
20
+ end
21
+
22
+ def sample_get_place_by_woeid
23
+ <<END
24
+ <?xml version="1.0" encoding="UTF-8"?>
25
+ <place xmlns="http://where.yahooapis.com/v1/schema.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:uri="http://where.yahooapis.com/v1/place/28751381" xml:lang="en-US"><woeid>28751381</woeid><placeTypeName code="22">Suburb</placeTypeName><name>Midtown</name><country type="Country" code="US">United States</country><admin1 type="State" code="US-NY">New York</admin1><admin2 type="County" code="">Manhattan</admin2><admin3></admin3><locality1 type="Town">New York</locality1><locality2 type="Suburb">Midtown</locality2><postal type="Zip Code">10017</postal><centroid><latitude>40.751122</latitude><longitude>-73.977859</longitude></centroid><boundingBox><southWest><latitude>40.734409</latitude><longitude>-73.996986</longitude></southWest><northEast><latitude>40.767841</latitude><longitude>-73.958817</longitude></northEast></boundingBox><areaRank>2</areaRank><popRank>0</popRank></place>
26
+ END
27
+ end
28
+
29
+
30
+ it "should instantiate" do
31
+ YaYahooGeocode::Client.new
32
+ end
33
+
34
+ describe "app_id" do
35
+ it "should not have an app_id by default" do
36
+ YaYahooGeocode::Client.new.app_id.should be_nil
37
+ end
38
+ it "should accept the app_id at init" do
39
+ client = YaYahooGeocode::Client.new('12345')
40
+ client.app_id.should == '12345'
41
+ end
42
+ it "should accept the app_id with a setter" do
43
+ client = YaYahooGeocode::Client.new
44
+ client.app_id = '12345'
45
+ client.app_id.should == '12345'
46
+ end
47
+ end
48
+
49
+ describe "#search_for_place" do
50
+ context "all goes well" do
51
+ before :each do
52
+ @client = YaYahooGeocode::Client.new
53
+ @client.stub!(:do_http_get).and_return(sample_search_for_place)
54
+ end
55
+ it "should find a result for 'East Harlem, NY, NY'" do
56
+ place_details = @client.search_for_place("East Harlem, NY, NY")
57
+ place_details['line2'].should include("East Harlem")
58
+ end
59
+ end
60
+ context "error occurs" do
61
+ before :each do
62
+ @client = YaYahooGeocode::Client.new
63
+ @client.stub!(:do_http_get).and_raise("yahoo hates you")
64
+ end
65
+ it "should raise an exception" do
66
+ lambda { @client.search_for_place("East Harlem, NY, NY") }.should raise_error(RuntimeError) { | error|
67
+ error.message.should include("East Harlem")
68
+ error.message.should include("Got error")
69
+ }
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "#search_for_place_by_location" do
75
+ before :each do
76
+ @client = YaYahooGeocode::Client.new
77
+ @client.stub!(:do_http_get).and_return(sample_search_for_place_by_woeid)
78
+ end
79
+ it "should find a place" do
80
+ place_details = @client.search_for_place_by_location("40.796761", "-73.942207")
81
+ place_details['woeid'].should == 23511892.to_s
82
+ end
83
+ end
84
+
85
+ describe "#get_place_by_woeid" do
86
+ context "all goes well" do
87
+ before :each do
88
+ # Note, an app id is actually required here
89
+ @client = YaYahooGeocode::Client.new '12345'
90
+ @client.stub!(:do_http_get).and_return(sample_get_place_by_woeid)
91
+ end
92
+
93
+ it "should find Midtown by 28751381" do
94
+ place = @client.get_place_by_woeid(28751381)
95
+ place['name'].should == "Midtown"
96
+ end
97
+ end
98
+ end
99
+
100
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ya_yahoo_geocode
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - John Hinnegan
@@ -52,8 +52,7 @@ files:
52
52
  - lib/ya_yahoo_geocode.rb
53
53
  - lib/ya_yahoo_geocode/client.rb
54
54
  - lib/ya_yahoo_geocode/version.rb
55
- - spec/ya_yahoo_geocode/client__get_place_by_woeid_spec.rb
56
- - spec/ya_yahoo_geocode/client__search_for_place_spec.rb
55
+ - spec/ya_yahoo_geocode/client_spec.rb
57
56
  - ya_yahoo_geocode.gemspec
58
57
  has_rdoc: true
59
58
  homepage: ""
@@ -84,5 +83,4 @@ signing_key:
84
83
  specification_version: 3
85
84
  summary: This is a gem to help make various calls to yahoo's geo-aware api's
86
85
  test_files:
87
- - spec/ya_yahoo_geocode/client__get_place_by_woeid_spec.rb
88
- - spec/ya_yahoo_geocode/client__search_for_place_spec.rb
86
+ - spec/ya_yahoo_geocode/client_spec.rb
@@ -1,24 +0,0 @@
1
- require './lib/ya_yahoo_geocode.rb'
2
-
3
- describe YaYahooGeocode::Client do
4
- def sample
5
- <<END
6
- <?xml version="1.0" encoding="UTF-8"?>
7
- <place xmlns="http://where.yahooapis.com/v1/schema.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:uri="http://where.yahooapis.com/v1/place/28751381" xml:lang="en-US"><woeid>28751381</woeid><placeTypeName code="22">Suburb</placeTypeName><name>Midtown</name><country type="Country" code="US">United States</country><admin1 type="State" code="US-NY">New York</admin1><admin2 type="County" code="">Manhattan</admin2><admin3></admin3><locality1 type="Town">New York</locality1><locality2 type="Suburb">Midtown</locality2><postal type="Zip Code">10017</postal><centroid><latitude>40.751122</latitude><longitude>-73.977859</longitude></centroid><boundingBox><southWest><latitude>40.734409</latitude><longitude>-73.996986</longitude></southWest><northEast><latitude>40.767841</latitude><longitude>-73.958817</longitude></northEast></boundingBox><areaRank>2</areaRank><popRank>0</popRank></place>
8
- END
9
- end
10
-
11
- describe "#get_place_by_woeid" do
12
- context "all goes well" do
13
- before :each do
14
- @client = YaYahooGeocode::Client.new
15
- @client.stub!(:do_http_get).and_return(sample)
16
- end
17
-
18
- it "should find Midtown by 28751381" do
19
- place = @client.get_place_by_woeid(28751381)
20
- place['name'].should == "Midtown"
21
- end
22
- end
23
- end
24
- end
@@ -1,42 +0,0 @@
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