module GScraper::HasPages

Public Instance Methods

[](index) click to toggle source

The page at the specified index.

@param [Integer] index

The index.

@return [Page]

The page at the given index.
# File lib/gscraper/has_pages.rb, line 44
def [](index)
  page_cache[index]
end
each() { |next_page| ... } click to toggle source

Iterates over all the pages of the query, until an empty page is encountered.

@yield [page]

A page with results from the query.

@yieldparam [Page] page

A non-empty page from the query.
# 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) click to toggle source

Iterates over the elements on the page with the specified index.

@param [Integer] index

The index to access.
# 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) click to toggle source

Iterates over each element on the pages with the specified indices.

@param [Array, Range] indices

The indices to access.
# 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_cache| ... } click to toggle source

Iterates over the pages at the specified indices.

@param [Array, Range] indices

The indices.

@yield [page]

The given block will be passed each page.

@yieldparam [Page] page

A page at one of the given indices.
# 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() click to toggle source

The first page.

@return [Page]

The first page.
# File lib/gscraper/has_pages.rb, line 31
def first_page
  page_cache[1]
end
pages(indices) click to toggle source

The pages with the specified indices.

@param [Array, Range] indices

The indices.

@return [Page]

The pages at the given indices.
# File lib/gscraper/has_pages.rb, line 57
def pages(indices)
  indices.map { |index| page_cache[index] }
end

Protected Instance Methods

page_cache() click to toggle source

The cache of previously requested pages.

@return [Hash]

# 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) click to toggle source

The page index for the specified result rank.

@param [Integer] rank

A result ranking.

@return [Integer]

The page index.
# File lib/gscraper/has_pages.rb, line 135
def page_index_of(rank)
  (((rank.to_i - 1) / results_per_page.to_i) + 1)
end
result_index_of(rank) click to toggle source

The in-page index of the specified result rank.

@param [Integer] rank

The result ranking.

@return [Integer]

The in-page index.
# 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) click to toggle source

The rank offset for the specified page-index.

@param [Integer] page_index

The result offset within a page.
# 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