Class: SeleniumSpider::Pagination

Inherits:
SeleniumStandaloneDSL::Base
  • Object
show all
Defined in:
lib/selenium_spider/pagination.rb

Constant Summary collapse

@@attributes =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_url) ⇒ Pagination

Returns a new instance of Pagination.



28
29
30
31
32
# File 'lib/selenium_spider/pagination.rb', line 28

def initialize(start_url)
  super()
  visit start_url
  @uri = URI.parse(start_url)
end

Class Method Details



20
21
22
# File 'lib/selenium_spider/pagination.rb', line 20

def self.detail_links(selector)
  @@detail_links_selector = selector
end


11
12
13
14
# File 'lib/selenium_spider/pagination.rb', line 11

def self.next_link(selector, find_by: :link_text)
  @@next_link = selector
  @@next_link_find_by = find_by
end


24
25
26
# File 'lib/selenium_spider/pagination.rb', line 24

def self.no_detail_link
  @@detail_links_selector = nil
end


16
17
18
# File 'lib/selenium_spider/pagination.rb', line 16

def self.no_next_link
  @@next_link = nil
end

Instance Method Details

#attributesObject



7
8
9
# File 'lib/selenium_spider/pagination.rb', line 7

def attributes
  @@attributes
end

#before_crawl(times) ⇒ Object

You can define something to do before crawling



35
36
# File 'lib/selenium_spider/pagination.rb', line 35

def before_crawl(times)
end

#continue?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/selenium_spider/pagination.rb', line 52

def continue?
  return false if !@@next_link
  has_element?(@@next_link, find_by: @@next_link_find_by)
end


42
43
44
45
# File 'lib/selenium_spider/pagination.rb', line 42

def detail_links
  return false if !@@detail_links_selector
  search(@@detail_links_selector).map(&->(x) { full_url(x.attribute('href').value) } )
end

#full_url(path) ⇒ Object



47
48
49
50
# File 'lib/selenium_spider/pagination.rb', line 47

def full_url(path)
  port = (@uri.port == 80) ? '' : ':' + @uri.port.to_s
  @uri.scheme + '://' + @uri.host + port + path
end

#nextObject



38
39
40
# File 'lib/selenium_spider/pagination.rb', line 38

def next
  click @@next_link, find_by: @@next_link_find_by
end

#register(attr_name_sym) {|| ... } ⇒ Object

Yields:

  • ()


57
58
59
60
# File 'lib/selenium_spider/pagination.rb', line 57

def register(attr_name_sym)
  @@attributes[attr_name_sym] = SeleniumSpider::Attribute.new
  yield @@attributes[attr_name_sym] if block_given?
end