mlangenberg-googlesearch 0.1.0 → 0.2.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/googlesearch.gemspec +1 -1
- data/lib/googlesearch/search_page.rb +6 -1
- data/lib/googlesearch/search_response.rb +18 -3
- data/spec/search_page_spec.rb +13 -3
- data/spec/search_response_spec.rb +21 -0
- metadata +1 -1
data/googlesearch.gemspec
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
class SearchPage
|
2
2
|
#index is zero-based
|
3
|
-
def initialize(index, results_per_page)
|
3
|
+
def initialize(index, response_start_index, results_per_page)
|
4
4
|
@index = index
|
5
|
+
@response_start_index = response_start_index
|
5
6
|
@results_per_page = results_per_page
|
6
7
|
end
|
7
8
|
|
@@ -12,4 +13,8 @@ class SearchPage
|
|
12
13
|
def start_index
|
13
14
|
@index*@results_per_page
|
14
15
|
end
|
16
|
+
|
17
|
+
def current_page?
|
18
|
+
start_index == @response_start_index
|
19
|
+
end
|
15
20
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
class SearchResponse
|
2
|
-
attr_reader :total_server_time, :total_number_of_results,
|
3
|
-
|
2
|
+
attr_reader :total_server_time, :total_number_of_results,
|
3
|
+
:results, :index_of_first_result, :index_of_last_result,
|
4
|
+
:requested_number_of_search_results, :start_index
|
5
|
+
def initialize(xml, requested_number_of_search_results=nil, start_index=nil)
|
4
6
|
doc = Nokogiri::XML(xml)
|
5
7
|
@total_server_time = doc.root.xpath('TM').text.to_f
|
6
8
|
@index_of_first_result = doc.root.xpath('RES/@SN').text.to_i
|
@@ -8,6 +10,7 @@ class SearchResponse
|
|
8
10
|
@total_number_of_results = doc.root.xpath('RES/M').text.to_i
|
9
11
|
@results = doc.root.xpath('RES//R').map { |res_doc| SearchResult.new(res_doc) }
|
10
12
|
@requested_number_of_search_results = requested_number_of_search_results ||= 10
|
13
|
+
@start_index = start_index ||= 0
|
11
14
|
end
|
12
15
|
|
13
16
|
def number_of_pages
|
@@ -15,6 +18,18 @@ class SearchResponse
|
|
15
18
|
end
|
16
19
|
|
17
20
|
def pages
|
18
|
-
@pages ||= number_of_pages.enum_for(:times).collect { |page_index| SearchPage.new(page_index, requested_number_of_search_results) }
|
21
|
+
@pages ||= number_of_pages.enum_for(:times).collect { |page_index| SearchPage.new(page_index, start_index, requested_number_of_search_results) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def current_page
|
25
|
+
pages.find { |p| p.current_page? }
|
26
|
+
end
|
27
|
+
|
28
|
+
def previous_page
|
29
|
+
start_index == 0 ? nil : pages[pages.index(current_page)-1]
|
30
|
+
end
|
31
|
+
|
32
|
+
def next_page
|
33
|
+
pages[pages.index(current_page)+1]
|
19
34
|
end
|
20
35
|
end
|
data/spec/search_page_spec.rb
CHANGED
@@ -2,17 +2,27 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
2
2
|
|
3
3
|
describe SearchPage do
|
4
4
|
it "should have page number of 1" do
|
5
|
-
p = SearchPage.new(0, 10)
|
5
|
+
p = SearchPage.new(0, 0, 10)
|
6
6
|
p.page_number.should == 1
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should have a start index of 0 when the index is 0" do
|
10
|
-
p = SearchPage.new(0, 10)
|
10
|
+
p = SearchPage.new(0, 0, 10)
|
11
11
|
p.start_index.should == 0
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should have a start index of 10 when the index is 1 and the requested number of results is 10" do
|
15
|
-
p = SearchPage.new(1, 10)
|
15
|
+
p = SearchPage.new(1, 0, 10)
|
16
16
|
p.start_index.should == 10
|
17
17
|
end
|
18
|
+
|
19
|
+
it "should know that it is the current page if the given page index equals the start index" do
|
20
|
+
p = SearchPage.new(1, 10, 10)
|
21
|
+
p.should be_current_page
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should know that it is not the current page if the given page index does not equal the start index" do
|
25
|
+
p = SearchPage.new(0, 10, 10)
|
26
|
+
p.should_not be_current_page
|
27
|
+
end
|
18
28
|
end
|
@@ -23,6 +23,15 @@ describe SearchResponse do
|
|
23
23
|
@response.index_of_last_result.should == 5
|
24
24
|
end
|
25
25
|
|
26
|
+
it "should return 5 for the start index" do
|
27
|
+
@response = SearchResponse.new(@example_xml, 0, 5)
|
28
|
+
@response.start_index.should == 5
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return 0 for the start index as default" do
|
32
|
+
@response.start_index.should == 0
|
33
|
+
end
|
34
|
+
|
26
35
|
it "should return 5 for the requested number of search results" do
|
27
36
|
@response.requested_number_of_search_results.should == 5
|
28
37
|
end
|
@@ -44,4 +53,16 @@ describe SearchResponse do
|
|
44
53
|
@response.pages[0].page_number.should == 1
|
45
54
|
@response.pages[0].start_index.should == 0
|
46
55
|
end
|
56
|
+
|
57
|
+
it "should be able to return the current page" do
|
58
|
+
@response.current_page.should == @response.pages[0]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should be able to return the next page" do
|
62
|
+
@response.next_page.should == @response.pages[1]
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should return nil for the previous page" do
|
66
|
+
@response.previous_page.should == nil
|
67
|
+
end
|
47
68
|
end
|