Module: GScraper::HasPages

Includes:
Enumerable
Included in:
Search::AJAXQuery, Search::WebQuery
Defined in:
lib/gscraper/has_pages.rb

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Page

The page at the specified index.

Parameters:

  • index (Integer)

    The index.

Returns:

  • (Page)

    The page at the given 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.

Yields:

  • (page)

    A page with results from the query.

Yield Parameters:

  • page (Page)

    A non-empty page from the query.



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.

Parameters:

  • index (Integer)

    The index to access.



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.

Parameters:

  • indices (Array, Range)

    The indices to access.



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.

Parameters:

  • indices (Array, Range)

    The indices.

Yields:

  • (page)

    The given block will be passed each page.

Yield Parameters:

  • page (Page)

    A page at one of the given 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_pagePage

The first page.

Returns:

  • (Page)

    The first page.



31
32
33
# File 'lib/gscraper/has_pages.rb', line 31

def first_page
  page_cache[1]
end

#page_cacheHash (protected)

The cache of previously requested pages.

Returns:

  • (Hash)


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.

Parameters:

  • rank (Integer)

    A result ranking.

Returns:

  • (Integer)

    The page index.



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.

Parameters:

  • indices (Array, Range)

    The indices.

Returns:

  • (Page)

    The pages at the given 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.

Parameters:

  • rank (Integer)

    The result ranking.

Returns:

  • (Integer)

    The in-page index.



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.

Parameters:

  • page_index (Integer)

    The result offset within a page.



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