yahoo-search 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright 2006 Eric Hodel, The Robot Co-op. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+ 3. Neither the names of the authors nor the names of their contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
17
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
data/Manifest.txt ADDED
@@ -0,0 +1,9 @@
1
+ LICENSE
2
+ Manifest.txt
3
+ README
4
+ Rakefile
5
+ lib/yahoo/local_search.rb
6
+ lib/yahoo/search.rb
7
+ lib/yahoo/web_search.rb
8
+ test/test_local_search.rb
9
+ test/test_web_search.rb
data/README ADDED
@@ -0,0 +1,47 @@
1
+ = yahoo-search
2
+
3
+ Rubyforge Project:
4
+
5
+ http://rubyforge.org/projects/rctools/
6
+
7
+ Documentation:
8
+
9
+ http://dev.robotcoop.com/Libraries/yahoo-search/
10
+
11
+ == About
12
+
13
+ yahoo-search implements version 1 of Yahoo's Web Search service and version 3
14
+ of Yahoo's Local Search Web Service.
15
+
16
+ == Installing yahoo-search
17
+
18
+ Just install the gem:
19
+
20
+ $ sudo gem install yahoo-search
21
+
22
+ == Using yahoo-search
23
+
24
+ First you'll need a Yahoo Application ID. You can register for one here:
25
+
26
+ http://api.search.yahoo.com/webservices/register_application
27
+
28
+ Then you create a Yahoo::Search object and start locating places:
29
+
30
+ require 'rubygems'
31
+ require 'yahoo/web_search'
32
+
33
+ ys = Yahoo::WebSearch.new application_id
34
+ results, = ys.search 'madonna'
35
+ results.each do |result|
36
+ puts "#{result.title} at #{result.url}"
37
+ end
38
+
39
+ require 'rubygems'
40
+ require 'yahoo/local_search'
41
+
42
+ yls = Yahoo::LocalSearch.new application_id
43
+ results, = yls.locate 'pizza', 94306, 2
44
+ results.each do |location|
45
+ puts "#{location.title} at #{location.address}, #{location.city}"
46
+ end
47
+
data/Rakefile ADDED
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+
7
+ $VERBOSE = nil
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'yahoo-search'
11
+ s.version = '1.0.0'
12
+ s.summary = 'A Ruby Yahoo Search API Implementation'
13
+ s.description = 'An interface to Yahoo\'s Local Search service.
14
+
15
+ http://developer.yahoo.com/search/local/V3/localSearch.html'
16
+
17
+ s.author = 'Eric Hodel'
18
+ s.email = 'eric@robotcoop.com'
19
+
20
+ s.has_rdoc = true
21
+ s.files = File.read('Manifest.txt').split($/)
22
+ s.require_path = 'lib'
23
+
24
+ s.add_dependency 'yahoo', '1.0.0'
25
+ end
26
+
27
+ desc 'Run tests'
28
+ task :default => [ :test ]
29
+
30
+ Rake::TestTask.new('test') do |t|
31
+ t.libs << 'test'
32
+ t.libs << '../yahoo'
33
+ t.libs << '../yahoo/lib'
34
+ t.pattern = 'test/test_*.rb'
35
+ t.verbose = true
36
+ end
37
+
38
+ desc 'Update Manifest.txt'
39
+ task :update_manifest do
40
+ sh "find . -type f | sed -e 's%./%%' | egrep -v 'svn|swp|~' | egrep -v '^(doc|pkg)/' | sort > Manifest.txt"
41
+ end
42
+
43
+ desc 'Generate RDoc'
44
+ Rake::RDocTask.new :rdoc do |rd|
45
+ rd.rdoc_dir = 'doc'
46
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
47
+ rd.main = 'README'
48
+ rd.options << '-d' if `which dot` =~ /\/dot/
49
+ rd.options << '-t Yahoo Search Web Services'
50
+ end
51
+
52
+ desc 'Generate RDoc for dev.robotcoop.com'
53
+ Rake::RDocTask.new :dev_rdoc do |rd|
54
+ rd.rdoc_dir = '../../../www/trunk/dev/html/Libraries/yahoo-search'
55
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
56
+ rd.main = 'README'
57
+ rd.options << '-d' if `which dot` =~ /\/dot/
58
+ rd.options << '-t Yahoo Search Web Services'
59
+ end
60
+
61
+ desc 'Build Gem'
62
+ Rake::GemPackageTask.new spec do |pkg|
63
+ pkg.need_tar = true
64
+ end
65
+
66
+ desc 'Clean up'
67
+ task :clean => [ :clobber_rdoc, :clobber_package ]
68
+
69
+ desc 'Clean up'
70
+ task :clobber => [ :clean ]
71
+
72
+ # vim: syntax=Ruby
73
+
@@ -0,0 +1,147 @@
1
+ require 'yahoo/search'
2
+
3
+ ##
4
+ # Yahoo local search API.
5
+ #
6
+ # http://developer.yahoo.com/search/local/V3/localSearch.html
7
+
8
+ class Yahoo::LocalSearch < Yahoo::Search
9
+
10
+ ##
11
+ # Local search result struct.
12
+
13
+ Result = Struct.new :id, :title, :address, :city, :state, :phone,
14
+ :latitude, :longitude,
15
+
16
+ :average_rating, :total_ratings, :total_reviews,
17
+ :last_review_date, :last_review_intro,
18
+
19
+ :distance,
20
+
21
+ :url, :click_url, :map_url, :business_url,
22
+ :business_click_url,
23
+
24
+ :categories
25
+
26
+ def initialize(*args) # :nodoc:
27
+ @host = 'api.local.yahoo.com'
28
+ @service_name = 'LocalSearchService'
29
+ @version = 'V3'
30
+ @method = 'localSearch'
31
+ super
32
+ end
33
+
34
+ ##
35
+ # Search for +query+ at +location+ and return up to +results+ items.
36
+ #
37
+ # If +results+ is omitted then ten results are returned.
38
+ #
39
+ # +query+ is the location you are searching for.
40
+ #
41
+ # +location+ can be any of
42
+ # * city, state
43
+ # * city, state, zip
44
+ # * zip
45
+ # * street, city, state
46
+ # * street, city, state, zip
47
+ # * street, zip
48
+ #
49
+ # locate returns five items, an Array of search results, a URI for a webpage
50
+ # with a map of the search results, the total number of results available,
51
+ # the number of results returned and the position of the first result in the
52
+ # overall search (one based).
53
+
54
+ def locate(query, location, results = nil)
55
+ params = { :query => query, :location => location }
56
+ params[:results] = results unless results.nil?
57
+ get params
58
+ end
59
+
60
+ def parse_response(xml) # :nodoc:
61
+ search_results = []
62
+
63
+ result_set_map_url = URI.parse xml.elements['ResultSet/ResultSetMapUrl'].text
64
+ total_results_available, total_results_returned, first_result_position =
65
+ parse_result_info xml
66
+
67
+ xml.elements['ResultSet'].each do |r|
68
+ next unless r.name == 'Result'
69
+ sr = Result.new
70
+
71
+ sr.id = r.attributes['id'].to_i
72
+ sr.title = r.elements['Title'].text
73
+ sr.address = r.elements['Address'].text
74
+ sr.city = r.elements['City'].text
75
+ sr.state = r.elements['State'].text
76
+ sr.phone = r.elements['Phone'].text
77
+ sr.latitude = r.elements['Latitude'].text.to_f
78
+ sr.longitude = r.elements['Longitude'].text.to_f
79
+
80
+ rating = r.elements['Rating']
81
+ sr.average_rating = rating.elements['AverageRating'].text.to_f
82
+ sr.total_ratings = rating.elements['TotalRatings'].text.to_i
83
+ sr.total_reviews = rating.elements['TotalReviews'].text.to_i
84
+ sr.last_review_date = Time.at rating.elements['LastReviewDate'].text.to_i
85
+ sr.last_review_intro = rating.elements['LastReviewIntro'].text
86
+
87
+ sr.distance = r.elements['Distance'].text.to_f
88
+ sr.url = URI.parse r.elements['Url'].text
89
+ sr.click_url = URI.parse r.elements['ClickUrl'].text
90
+ sr.map_url = URI.parse r.elements['MapUrl'].text
91
+ sr.business_url = URI.parse r.elements['BusinessUrl'].text
92
+ sr.business_click_url = URI.parse r.elements['BusinessClickUrl'].text
93
+
94
+ sr.categories = {}
95
+ r.elements['Categories'].each do |c|
96
+ sr.categories[c.text] = c.attributes['id'].to_i
97
+ end
98
+
99
+ search_results << sr
100
+ end
101
+
102
+ return search_results, result_set_map_url, total_results_available,
103
+ total_results_returned, first_result_position
104
+ end
105
+
106
+ end
107
+
108
+ ##
109
+ # A Result contains the following fields:
110
+ #
111
+ # +id+:: The id of this result.
112
+ # +title+:: The name of the result
113
+ # +address+:: Street address of the result
114
+ # +city+:: City in which the result is located
115
+ # +state+:: State in which the result is located
116
+ # +phone+:: Phone number, if known
117
+ # +latitude+:: Latitude of the location
118
+ # +longitude+:: Longitude of the location
119
+ # +average_rating+:: Average score of end-user ratings for the business or
120
+ # service
121
+ # +total_ratings+:: Total number of ratings submitted for the business or
122
+ # service
123
+ # +total_reviews+:: Total number of ratings submitted for the business or
124
+ # service
125
+ # +last_review_date+:: Time of the last review submitted for the business or
126
+ # service
127
+ # +last_review_intro+:: The first few words of the last review submitted for
128
+ # the business or service
129
+ # +distance+:: Distance from the search location to this business or service
130
+ # +url+:: URL to the detailed page for a business
131
+ # +click_url+:: URL for linking to the detailed page for a business
132
+ # +map_url+:: URL for a map of the address
133
+ # +business_url+:: URL of the businesses website, if known
134
+ # +business_click_url+:: URL for linking to the businesses website, if known
135
+ # +categories+:: Hash of category names and category ids for this result
136
+
137
+ class Yahoo::LocalSearch::Result
138
+
139
+ ##
140
+ # Returns an Array with latitude and longitude.
141
+
142
+ def coordinates
143
+ return [latitude, longitude]
144
+ end
145
+
146
+ end
147
+
@@ -0,0 +1,23 @@
1
+ require 'yahoo'
2
+
3
+ ##
4
+ # Abstract class for searching yahoo.
5
+
6
+ class Yahoo::Search < Yahoo
7
+
8
+ ##
9
+ # Returns the total results available, returned, and first result position
10
+ # for the returned results.
11
+
12
+ def parse_result_info(xml) # :nodoc:
13
+ rs = xml.elements['ResultSet']
14
+ total_results_available = rs.attributes['totalResultsAvailable'].to_i
15
+ total_results_returned = rs.attributes['totalResultsReturned'].to_i
16
+ first_result_position = rs.attributes['firstResultPosition'].to_i
17
+
18
+ return total_results_available, total_results_returned,
19
+ first_result_position
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,75 @@
1
+ require 'yahoo/search'
2
+
3
+ ##
4
+ # Yahoo Web Search API
5
+ #
6
+ # http://developer.yahoo.com/search/web/V1/webSearch.html
7
+
8
+ class Yahoo::WebSearch < Yahoo::Search
9
+
10
+ Result = Struct.new :title, :summary, :url, :click_url, :mime_type,
11
+ :modification_date, :cache_url, :cache_size
12
+
13
+ def initialize(*args) # :nodoc:
14
+ @host = 'api.search.yahoo.com'
15
+ @service_name = 'WebSearchService'
16
+ @version = 'V1'
17
+ @method = 'webSearch'
18
+ super
19
+ end
20
+
21
+ ##
22
+ # Searches the web for +query+ and returns up to +results+ items.
23
+ #
24
+ # If +results+ is omitted then ten results are returned.
25
+ #
26
+ # For details on constructing +query+, see:
27
+ # http://help.yahoo.com/help/us/ysearch/tips/tips-04.html
28
+
29
+ def search(query, results = nil)
30
+ params = { :query => query }
31
+ params[:results] = results unless results.nil?
32
+ get params
33
+ end
34
+
35
+ def parse_response(xml) # :nodoc:
36
+ search_results = []
37
+
38
+ total_results_available, total_results_returned, first_result_position =
39
+ parse_result_info xml
40
+
41
+ xml.elements['ResultSet'].each do |r|
42
+ next if REXML::Text === r
43
+ result = Result.new
44
+
45
+ result.title = r.elements['Title'].text
46
+ result.summary = r.elements['Summary'].text
47
+ result.url = URI.parse r.elements['Url'].text
48
+ result.click_url = URI.parse r.elements['ClickUrl'].text
49
+ result.mime_type = r.elements['MimeType'].text
50
+ result.modification_date = Time.at r.elements['ModificationDate'].text.to_i
51
+ result.cache_url = URI.parse r.elements['Cache/Url'].text
52
+ result.cache_size = r.elements['Cache/Size'].text.to_i
53
+
54
+ search_results << result
55
+ end
56
+
57
+ return search_results, total_results_available, total_results_returned,
58
+ first_result_position
59
+ end
60
+
61
+ end
62
+
63
+ ##
64
+ # A Result contains the following fields:
65
+ #
66
+ # +title+:: the title of the web page
67
+ # +summary+:: summary text associated with the web page
68
+ # +url+:: URL for the web page
69
+ # +click_url+:: URL for linking to the page
70
+ # +mime_type+:: MIME type of the page
71
+ # +modification_date+:: Time the page was last modified
72
+ # +cache_url+:: URL of the cached result
73
+ # +cache_size+:: size of the cached result in bytes
74
+
75
+ class Yahoo::WebSearch::Result; end
@@ -0,0 +1,74 @@
1
+ require 'test/unit'
2
+ require 'test/uri_stub'
3
+ require 'yahoo/local_search'
4
+
5
+ class Yahoo::TestLocalSearch < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+
11
+ @s = Yahoo::LocalSearch.new 'APP_ID'
12
+ end
13
+
14
+ def test_search
15
+ URI::HTTP.responses << <<-EOF.strip
16
+ <?xml version="1.0"?>
17
+ <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:lcl" xsi:schemaLocation="urn:yahoo:lcl http://api.local.yahoo.com/LocalSearchService/V3/LocalSearchResponse.xsd" totalResultsAvailable="431" totalResultsReturned="2" firstResultPosition="1"><ResultSetMapUrl>http://local.yahoo.com/mapview?stx=pizza&amp;csz=Palo+Alto%2C+CA+94306&amp;city=Palo+Alto&amp;state=CA&amp;radius=15&amp;ed=BVD56a131DxIV6V7_5O_wO8KQY1.bxtOAd8qew--</ResultSetMapUrl><Result id="21395990"><Title>Round Table Pizza Palo Alto</Title><Address>3407 Alma St</Address><City>Palo Alto</City><State>CA</State><Phone>(650) 494-2928</Phone><Latitude>37.419862</Latitude><Longitude>-122.126129</Longitude><Rating><AverageRating>NaN</AverageRating><TotalRatings>0</TotalRatings><TotalReviews>0</TotalReviews><LastReviewDate>0</LastReviewDate><LastReviewIntro></LastReviewIntro></Rating><Distance>0.19</Distance><Url>http://local.yahoo.com/details?id=21395990&amp;stx=pizza&amp;csz=Palo+Alto+CA&amp;ed=OtVQW6160SwBXrynJIx8rirX3iaOtFHwFBC480.oEQVLaK1tM9x1WN.BrJASDzg2asPklg--</Url><ClickUrl>http://local.yahoo.com/details?id=21395990&amp;stx=pizza&amp;csz=Palo+Alto+CA&amp;ed=OtVQW6160SwBXrynJIx8rirX3iaOtFHwFBC480.oEQVLaK1tM9x1WN.BrJASDzg2asPklg--</ClickUrl><MapUrl>http://maps.yahoo.com/maps_result?name=Round+Table+Pizza+Palo+Alto&amp;desc=6504942928&amp;csz=Palo+Alto+CA&amp;qty=9&amp;cs=9&amp;ed=OtVQW6160SwBXrynJIx8rirX3iaOtFHwFBC480.oEQVLaK1tM9x1WN.BrJASDzg2asPklg--&amp;gid1=21395990</MapUrl><BusinessUrl>http://www.roundtablepizza.com/</BusinessUrl><BusinessClickUrl>http://www.roundtablepizza.com/</BusinessClickUrl><Categories><Category id="96926243">Pizza</Category><Category id="96926236">Restaurants</Category></Categories></Result><Result id="21395062"><Title>Papa Murphys Pizza Take &amp; Bake</Title><Address>2730 Middlefield Rd</Address><City>Palo Alto</City><State>CA</State><Phone>(650) 328-5200</Phone><Latitude>37.433243</Latitude><Longitude>-122.129291</Longitude><Rating><AverageRating>4</AverageRating><TotalRatings>8</TotalRatings><TotalReviews>5</TotalReviews><LastReviewDate>1140036645</LastReviewDate><LastReviewIntro>Great place to have pizza with the family.</LastReviewIntro></Rating><Distance>0.99</Distance><Url>http://local.yahoo.com/details?id=21395062&amp;stx=pizza&amp;csz=Palo+Alto+CA&amp;ed=0runZK160SyuzGmb4f1aQLs1KJTXAH56bJ_0HffsK.rhcWN62yU_7KJzff4AZ8TX2LILpT.nu6KJiQQ-</Url><ClickUrl>http://local.yahoo.com/details?id=21395062&amp;stx=pizza&amp;csz=Palo+Alto+CA&amp;ed=0runZK160SyuzGmb4f1aQLs1KJTXAH56bJ_0HffsK.rhcWN62yU_7KJzff4AZ8TX2LILpT.nu6KJiQQ-</ClickUrl><MapUrl>http://maps.yahoo.com/maps_result?name=Papa+Murphys+Pizza+Take+%26amp%3B+Bake&amp;desc=6503285200&amp;csz=Palo+Alto+CA&amp;qty=9&amp;cs=9&amp;ed=0runZK160SyuzGmb4f1aQLs1KJTXAH56bJ_0HffsK.rhcWN62yU_7KJzff4AZ8TX2LILpT.nu6KJiQQ-&amp;gid1=21395062</MapUrl><BusinessUrl>http://www.papamurphys.com/</BusinessUrl><BusinessClickUrl>http://www.papamurphys.com/</BusinessClickUrl><Categories><Category id="96926242">Fast Food</Category><Category id="96926234">Carry Out &amp; Take Out</Category><Category id="96926243">Pizza</Category><Category id="96926236">Restaurants</Category></Categories></Result></ResultSet>
18
+ <!-- ws02.search.scd.yahoo.com compressed/chunked Fri Jun 9 10:53:31 PDT 2006 -->
19
+ EOF
20
+
21
+ results, map_url, avail, returned, position = @s.locate('pizza', 94306, 2)
22
+
23
+ assert_equal 'http://local.yahoo.com/mapview?stx=pizza&csz=Palo+Alto%2C+CA+94306&city=Palo+Alto&state=CA&radius=15&ed=BVD56a131DxIV6V7_5O_wO8KQY1.bxtOAd8qew--',
24
+ map_url.to_s
25
+ assert_equal 431, avail
26
+ assert_equal 2, returned
27
+ assert_equal 1, position
28
+
29
+ assert_equal 2, results.length
30
+
31
+ result = results.first
32
+ assert_equal 21395990, result.id
33
+ assert_equal 'Round Table Pizza Palo Alto', result.title
34
+ assert_equal '3407 Alma St', result.address
35
+ assert_equal 'Palo Alto', result.city
36
+ assert_equal 'CA', result.state
37
+ assert_equal '(650) 494-2928', result.phone
38
+ assert_equal 37.419862, result.latitude
39
+ assert_equal -122.126129, result.longitude
40
+ assert_equal [37.419862, -122.126129], result.coordinates
41
+
42
+ assert_equal 0.19, result.distance
43
+
44
+ categories = { 'Pizza' => 96926243, 'Restaurants' => 96926236 }
45
+
46
+ assert_equal categories, result.categories
47
+
48
+ assert_equal 'http://local.yahoo.com/details?id=21395990&stx=pizza&csz=Palo+Alto+CA&ed=OtVQW6160SwBXrynJIx8rirX3iaOtFHwFBC480.oEQVLaK1tM9x1WN.BrJASDzg2asPklg--',
49
+ result.url.to_s
50
+ assert_equal 'http://local.yahoo.com/details?id=21395990&stx=pizza&csz=Palo+Alto+CA&ed=OtVQW6160SwBXrynJIx8rirX3iaOtFHwFBC480.oEQVLaK1tM9x1WN.BrJASDzg2asPklg--',
51
+ result.click_url.to_s
52
+ assert_equal 'http://maps.yahoo.com/maps_result?name=Round+Table+Pizza+Palo+Alto&desc=6504942928&csz=Palo+Alto+CA&qty=9&cs=9&ed=OtVQW6160SwBXrynJIx8rirX3iaOtFHwFBC480.oEQVLaK1tM9x1WN.BrJASDzg2asPklg--&gid1=21395990',
53
+ result.map_url.to_s
54
+ assert_equal 'http://www.roundtablepizza.com/',
55
+ result.business_url.to_s
56
+ assert_equal 'http://www.roundtablepizza.com/',
57
+ result.business_click_url.to_s
58
+
59
+ result = results.last
60
+ assert_equal 4.0, result.average_rating
61
+ assert_equal 8, result.total_ratings
62
+ assert_equal 5, result.total_reviews
63
+ assert_equal Time.at(1140036645), result.last_review_date
64
+ assert_equal 'Great place to have pizza with the family.',
65
+ result.last_review_intro
66
+
67
+ assert_equal true, URI::HTTP.responses.empty?
68
+ assert_equal 1, URI::HTTP.uris.length
69
+ assert_equal 'http://api.local.yahoo.com/LocalSearchService/V3/localSearch?appid=APP_ID&location=94306&output=xml&query=pizza&results=2',
70
+ URI::HTTP.uris.first
71
+ end
72
+
73
+ end
74
+
@@ -0,0 +1,57 @@
1
+ require 'test/unit'
2
+ require 'test/uri_stub'
3
+ require 'yahoo/web_search'
4
+
5
+ class Yahoo::TestWebSearch < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+
11
+ @s = Yahoo::WebSearch.new 'APP_ID'
12
+ end
13
+
14
+ def test_search
15
+ URI::HTTP.responses << <<-EOF.strip
16
+ <?xml version="1.0" encoding="UTF-8"?>
17
+ <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:srch" xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/WebSearchService/V1/WebSearchResponse.xsd" totalResultsAvailable="32900000" totalResultsReturned="2" firstResultPosition="1">
18
+ <Result><Title>Madonna</Title><Summary>Official site of pop diva Madonna, with news, music, media, and fan club.</Summary><Url>http://www.madonna.com/</Url><ClickUrl>http://uk.wrs.yahoo.com/_ylt=A0Je5VwxLIpEF60ArETdmMwF;_ylu=X3oDMTB2cXVjNTM5BGNvbG8DdwRsA1dTMQRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=11bs38c7g/EXP=1149992369/**http%3a//www.madonna.com/</ClickUrl><ModificationDate>1145602800</ModificationDate><MimeType>text/html</MimeType>
19
+ <Cache><Url>http://uk.wrs.yahoo.com/_ylt=A0Je5VwxLIpEF60AskTdmMwF;_ylu=X3oDMTBwOHA5a2tvBGNvbG8DdwRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=15igoccah/EXP=1149992369/**http%3a//66.218.69.11/search/cache%3fei=UTF-8%26appid=APP_ID%26query=madonna%26results=2%26u=www.madonna.com/%26w=madonna%26d=BCcdDDmtM3M7%26icp=1%26.intl=us</Url><Size>17777</Size></Cache>
20
+ </Result>
21
+
22
+ <Result><Title>Madonnalicious</Title><Summary>Pictures, articles, downloads, concert info, news, and more about Madonna.</Summary><Url>http://www.madonnalicious.com/</Url><ClickUrl>http://uk.wrs.yahoo.com/_ylt=A0Je5VwxLIpEF60AtETdmMwF;_ylu=X3oDMTB2ZjQ4dDExBGNvbG8DdwRsA1dTMQRwb3MDMgRzZWMDc3IEdnRpZAM-/SIG=11injilpr/EXP=1149992369/**http%3a//www.madonnalicious.com/</ClickUrl><ModificationDate>1149750000</ModificationDate><MimeType>text/html</MimeType>
23
+ <Cache><Url>http://uk.wrs.yahoo.com/_ylt=A0Je5VwxLIpEF60AukTdmMwF;_ylu=X3oDMTBwZG5hOWwzBGNvbG8DdwRwb3MDMgRzZWMDc3IEdnRpZAM-/SIG=15pmktjmj/EXP=1149992369/**http%3a//66.218.69.11/search/cache%3fei=UTF-8%26appid=APP_ID%26query=madonna%26results=2%26u=www.madonnalicious.com/%26w=madonna%26d=SuWzgDmtM6lL%26icp=1%26.intl=us</Url><Size>202503</Size></Cache>
24
+ </Result>
25
+ </ResultSet>
26
+ <!-- ws02.search.scd.yahoo.com compressed/chunked Fri Jun 9 19:19:28 PDT 2006 -->
27
+ EOF
28
+
29
+ results, avail, returned, position = @s.search 'madonna', 2
30
+
31
+ assert_equal 32900000, avail
32
+ assert_equal 2, returned
33
+ assert_equal 1, position
34
+
35
+ assert_equal 2, results.length
36
+
37
+ result = results.first
38
+ assert_equal 'Madonna', result.title
39
+ assert_equal 'Official site of pop diva Madonna, with news, music, media, and fan club.',
40
+ result.summary
41
+ assert_equal 'http://www.madonna.com/', result.url.to_s
42
+ assert_equal 'http://uk.wrs.yahoo.com/_ylt=A0Je5VwxLIpEF60ArETdmMwF;_ylu=X3oDMTB2cXVjNTM5BGNvbG8DdwRsA1dTMQRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=11bs38c7g/EXP=1149992369/**http%3a//www.madonna.com/',
43
+ result.click_url.to_s
44
+ assert_equal 'text/html', result.mime_type
45
+ assert_equal Time.at(1145602800), result.modification_date
46
+ assert_equal 'http://uk.wrs.yahoo.com/_ylt=A0Je5VwxLIpEF60AskTdmMwF;_ylu=X3oDMTBwOHA5a2tvBGNvbG8DdwRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=15igoccah/EXP=1149992369/**http%3a//66.218.69.11/search/cache%3fei=UTF-8%26appid=APP_ID%26query=madonna%26results=2%26u=www.madonna.com/%26w=madonna%26d=BCcdDDmtM3M7%26icp=1%26.intl=us',
47
+ result.cache_url.to_s
48
+ assert_equal 17777, result.cache_size
49
+
50
+ assert_equal true, URI::HTTP.responses.empty?
51
+ assert_equal 1, URI::HTTP.uris.length
52
+ assert_equal 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=APP_ID&output=xml&query=madonna&results=2',
53
+ URI::HTTP.uris.first
54
+ end
55
+
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11.15
3
+ specification_version: 1
4
+ name: yahoo-search
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-06-10 00:00:00 -07:00
8
+ summary: A Ruby Yahoo Search API Implementation
9
+ require_paths:
10
+ - lib
11
+ email: eric@robotcoop.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: An interface to Yahoo's Local Search service. http://developer.yahoo.com/search/local/V3/localSearch.html
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Eric Hodel
31
+ files:
32
+ - LICENSE
33
+ - Manifest.txt
34
+ - README
35
+ - Rakefile
36
+ - lib/yahoo/local_search.rb
37
+ - lib/yahoo/search.rb
38
+ - lib/yahoo/web_search.rb
39
+ - test/test_local_search.rb
40
+ - test/test_web_search.rb
41
+ test_files: []
42
+
43
+ rdoc_options: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ requirements: []
52
+
53
+ dependencies:
54
+ - !ruby/object:Gem::Dependency
55
+ name: yahoo
56
+ version_requirement:
57
+ version_requirements: !ruby/object:Gem::Version::Requirement
58
+ requirements:
59
+ - - "="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ version: