Class: Parser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fly_parser/base.rb

Overview

Html Base

Direct Known Subclasses

Exercise, Fitness

Instance Method Summary collapse

Constructor Details

#initialize(url, options) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
# File 'lib/fly_parser/base.rb', line 5

def initialize(url, options)
  @source = Parser.connect(url)
  @copyright = copyright(options)
  @limit_pages ||= 5
  @delay ||= 10
end

Instance Method Details

#collect_between(first, last) ⇒ Object



34
35
36
37
# File 'lib/fly_parser/base.rb', line 34

def collect_between(first, last)
  return nil if first.nil?
  first == last ? [first] : [first, *collect_between(first.next, last)]
end


38
39
40
41
42
43
44
# File 'lib/fly_parser/base.rb', line 38

def copyright(options)
  source = options[:source]
  {
    url: source['copyright'],
    title: source['copyright_title']
  }
end

#next_page(css_selector) ⇒ Object



12
13
14
# File 'lib/fly_parser/base.rb', line 12

def next_page(css_selector)
  @next_page = @source.links_with(css_selector)[0]
end

#parse_allObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fly_parser/base.rb', line 16

def parse_all
  result = parse_page
  next_page()
  #concat all pages into one array
  i = 1
  ap "Parsing #{i} page"
  until @next_page.nil? || i == @limit_pages
    sleep @delay
    i += 1
    ap "Parsing #{i} page"
    @source = @next_page.click
    next_page()
    result.concat(parse_page)
  end
  result
end

#parse_pageObject



32
33
# File 'lib/fly_parser/base.rb', line 32

def parse_page
end