public
Rubygem
Description: Find some previously used domain names and check availability
Homepage:
Clone URL: git://github.com/remi/domain-finder.git
added Google pagination ... Google.search 'query', :pages => 3 (should give 30 
results)
remi (author)
Sat Jun 14 15:34:10 -0700 2008
commit  a6e706532cb74e08d94662c2d35ef96f8b1a4256
tree    1512ca8426593e476dcbbb88cc8a87f5b79fabe2
parent  03488e1c162988c9904b559e64b8e8ca068374a9
...
3
4
5
6
7
 
 
 
 
8
9
10
...
15
16
17
18
19
 
 
 
 
 
 
 
 
 
 
20
21
22
...
3
4
5
 
 
6
7
8
9
10
11
12
...
17
18
19
 
 
20
21
22
23
24
25
26
27
28
29
30
31
32
0
@@ -3,8 +3,10 @@ class Google
0
 
0
   BASE_SEARCH_URL = "http://www.google.com/search?q="
0
 
0
-  def self.search_url query
0
-    BASE_SEARCH_URL + URI.encode(query)
0
+  def self.search_url query, options = {}
0
+    url =  BASE_SEARCH_URL + URI.encode(query)
0
+    url += "&start=#{ (options[:page] - 1) * 10 }" if options[:page] and options[:page] != 1
0
+    url
0
   end
0
 
0
   def self.fetch_url url
0
@@ -15,8 +17,16 @@ class Google
0
     ( Hpricot(html) / 'div.g' ).map { |result| Result.new(result) }
0
   end
0
 
0
-  def self.search query 
0
-    parse_results( fetch_url(search_url(query)) )
0
+  def self.search query, options = {}
0
+    if options[:pages] and options[:pages] > 1
0
+      results = []
0
+      options[:pages].times do |i|
0
+        results += parse_results( fetch_url(search_url(query, :page => i+1)) )
0
+      end
0
+      results
0
+    else
0
+      parse_results( fetch_url(search_url(query)) )
0
+    end
0
   end
0
 
0
   # Simple class representing a single Google result
...
12
13
14
 
 
 
 
 
 
15
16
17
...
30
31
32
33
 
 
 
 
 
 
 
 
 
 
 
 
 
34
35
36
...
12
13
14
15
16
17
18
19
20
21
22
23
...
36
37
38
 
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
0
@@ -12,6 +12,12 @@ describe Google do
0
     Google.search_url(@example_query).should == @example_url
0
   end
0
 
0
+  it '.search_url should support pagination' do
0
+    Google.search_url(@example_query, :page => 1 ).should == @example_url
0
+    Google.search_url(@example_query, :page => 2 ).should == @example_url + '&start=10'
0
+    Google.search_url(@example_query, :page => 10).should == @example_url + '&start=90'
0
+  end
0
+
0
   it '.fetch_url should fetch the results of a full url' do
0
     Google.should_receive(:open).with(@example_url).and_return(StringIO.new(@example_html))
0
     Google.fetch_url(@example_url).should == @example_html
0
@@ -30,7 +36,19 @@ describe Google do
0
 
0
   it '.search should return GoogleResult objects for a query' do
0
     Google.should_receive(:open).with(@example_url).and_return(StringIO.new(@example_html))
0
-    Google.search(@example_query).first.should be_a_kind_of(Google::Result)
0
+    results = Google.search(@example_query)
0
+    results.length.should == 10
0
+    results.first.should be_a_kind_of(Google::Result)
0
+  end
0
+  
0
+  it '.search should support pagination' do
0
+    Google.should_receive(:open).with(@example_url).and_return(StringIO.new(@example_html))
0
+    Google.should_receive(:open).with(@example_url + '&start=10').and_return(StringIO.new(@example_html))
0
+    Google.should_receive(:open).with(@example_url + '&start=20').and_return(StringIO.new(@example_html))
0
+
0
+    results = Google.search(@example_query, :pages => 3)
0
+    results.length.should == 30
0
+    results.first.should be_a_kind_of(Google::Result)
0
   end
0
 
0
 end

Comments