Class: Scrapouille::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/scrapouille/scraper.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Scraper

Returns a new instance of Scraper.



8
9
10
11
# File 'lib/scrapouille/scraper.rb', line 8

def initialize(&block)
  @rules = {collect_unique: [], collect_all: []} 
  instance_eval(&block) if block_given?
end

Instance Method Details

#scrap(property, xpath_options) ⇒ Object



19
20
21
22
23
# File 'lib/scrapouille/scraper.rb', line 19

def scrap(property, xpath_options)
  ensure_valid_definition(property, xpath_options)
  block = Proc.new if block_given? 
  add_rule(:collect_unique, property, xpath_options, block)
end

#scrap!(uri) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/scrapouille/scraper.rb', line 39

def scrap!(uri)
  page = open(uri).read

  results = @rules[:collect_unique].inject({}) do |acc, rule|
    property, items = process_rule(rule, page)
    acc[property] = items.first
    acc
  end

  @rules[:collect_all].inject(results) do |acc, rule|
    property, items = process_rule(rule, page)
    acc[property] = items
    acc
  end

  results
end

#scrap_all(property, xpath_options) ⇒ Object



13
14
15
16
17
# File 'lib/scrapouille/scraper.rb', line 13

def scrap_all(property, xpath_options)
  ensure_valid_definition(property, xpath_options)
  block = Proc.new if block_given? 
  add_rule(:collect_all, property, xpath_options, block)
end

#scrap_each!(*uris) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scrapouille/scraper.rb', line 25

def scrap_each!(*uris)
  if uris.length == 1 
    full_uris = uris.first
  elsif uris.length == 2
    root, relative_uris = *uris
    full_uris = relative_uris.map do |uri| "#{root}/#{uri}" end
  else
    raise ArgumentError, "Expecting 1 or 2 arguments when calling #{__callee__}"
  end
  full_uris.map do |uri|
    scrap!(uri)
  end
end