Class: Scraptacular::Suite

Inherits:
Object
  • Object
show all
Defined in:
lib/scraptacular/suite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Suite

Returns a new instance of Suite.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/scraptacular/suite.rb', line 5

def initialize(name, options = {}, &block)
  @name = name

  if !options.has_key?(:with)
    raise ArgumentError, "You must supply a default scraper using :with"
  end

  @default_scraper =  Scraptacular.world.scrapers[options[:with]]
  if @default_scraper.nil?
    raise ArgumentError, "The supplied scraper :#{options[:with]} does not exist"
  end

  @urls = []

  instance_eval(&block)
end

Instance Attribute Details

#default_scraperObject

Returns the value of attribute default_scraper.



3
4
5
# File 'lib/scraptacular/suite.rb', line 3

def default_scraper
  @default_scraper
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/scraptacular/suite.rb', line 3

def name
  @name
end

#urlsObject

Returns the value of attribute urls.



3
4
5
# File 'lib/scraptacular/suite.rb', line 3

def urls
  @urls
end

Instance Method Details

#run(out) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scraptacular/suite.rb', line 22

def run(out)
  out.puts "  Suite: #{self.name}"

  results = []

  urls.each do |url|
    out.puts "   URL: #{url.path}"

    scraper = url.scraper
    scraper ||= default_scraper

    page = Scraptacular.agent.get(url.path)
    results += [*scraper.run(page)]
  end

  results
end

#url(url_path, options = {}) ⇒ Object



40
41
42
# File 'lib/scraptacular/suite.rb', line 40

def url(url_path, options = {})
  urls << Scraptacular::URL.new(url_path, options)
end