Module: GScraper::HasPages
- Includes:
- Enumerable
- Included in:
- Search::AJAXQuery, Search::WebQuery
- Defined in:
- lib/gscraper/has_pages.rb
Instance Method Summary collapse
-
#[](index) ⇒ Page
The page at the specified index.
-
#each {|page| ... } ⇒ Object
Iterates over all the pages of the query, until an empty page is encountered.
-
#each_on_page(index, &block) ⇒ Object
Iterates over the elements on the page with the specified index.
-
#each_on_pages(indices, &block) ⇒ Object
Iterates over each element on the pages with the specified indices.
-
#each_page(indices) {|page| ... } ⇒ Object
Iterates over the pages at the specified indices.
-
#first_page ⇒ Page
The first page.
-
#page_cache ⇒ Hash
protected
The cache of previously requested pages.
-
#page_index_of(rank) ⇒ Integer
protected
The page index for the specified result rank.
-
#pages(indices) ⇒ Page
The pages with the specified indices.
-
#result_index_of(rank) ⇒ Integer
protected
The in-page index of the specified result rank.
-
#result_offset_of(page_index) ⇒ Object
protected
The rank offset for the specified page-index.
Instance Method Details
#[](index) ⇒ Page
The page at the specified index.
44 45 46 |
# File 'lib/gscraper/has_pages.rb', line 44 def [](index) page_cache[index] end |
#each {|page| ... } ⇒ Object
Iterates over all the pages of the query, until an empty page is encountered.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gscraper/has_pages.rb', line 91 def each return enum_for(:each) unless block_given? index = 1 until ((next_page = page_cache[index]).empty?) do yield next_page index = index + 1 end return self end |
#each_on_page(index, &block) ⇒ Object
Iterates over the elements on the page with the specified index.
110 111 112 |
# File 'lib/gscraper/has_pages.rb', line 110 def each_on_page(index,&block) page_cache[index].each(&block) end |
#each_on_pages(indices, &block) ⇒ Object
Iterates over each element on the pages with the specified indices.
120 121 122 |
# File 'lib/gscraper/has_pages.rb', line 120 def each_on_pages(indices,&block) each_page(indices) { |page| page.each(&block) } end |
#each_page(indices) {|page| ... } ⇒ Object
Iterates over the pages at the specified indices.
73 74 75 76 77 78 79 |
# File 'lib/gscraper/has_pages.rb', line 73 def each_page(indices) unless block_given? enum_for(:each_page,indices) else indices.map { |index| yield page_cache[index] } end end |
#first_page ⇒ Page
The first page.
31 32 33 |
# File 'lib/gscraper/has_pages.rb', line 31 def first_page page_cache[1] end |
#page_cache ⇒ Hash (protected)
The cache of previously requested pages.
167 168 169 |
# File 'lib/gscraper/has_pages.rb', line 167 def page_cache @page_cache ||= Hash.new { |hash,key| hash[key] = page(key.to_i) } end |
#page_index_of(rank) ⇒ Integer (protected)
The page index for the specified result rank.
135 136 137 |
# File 'lib/gscraper/has_pages.rb', line 135 def page_index_of(rank) (((rank.to_i - 1) / results_per_page.to_i) + 1) end |
#pages(indices) ⇒ Page
The pages with the specified indices.
57 58 59 |
# File 'lib/gscraper/has_pages.rb', line 57 def pages(indices) indices.map { |index| page_cache[index] } end |
#result_index_of(rank) ⇒ Integer (protected)
The in-page index of the specified result rank.
158 159 160 |
# File 'lib/gscraper/has_pages.rb', line 158 def result_index_of(rank) ((rank.to_i - 1) % results_per_page.to_i) end |
#result_offset_of(page_index) ⇒ Object (protected)
The rank offset for the specified page-index.
145 146 147 |
# File 'lib/gscraper/has_pages.rb', line 145 def result_offset_of(page_index) ((page_index.to_i - 1) * results_per_page.to_i) end |