Class: ExtraSpace::Crawl

Inherits:
Object
  • Object
show all
Defined in:
lib/extraspace/crawl.rb

Overview

Handles the crawl command via CLI.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout: $stdout, stderr: $stderr, url: nil) ⇒ Crawl

Returns a new instance of Crawl.

Parameters:

  • stdout (IO) (defaults to: $stdout)

    optional

  • stderr (IO) (defaults to: $stderr)

    optional

  • url (String) (defaults to: nil)

    optional



13
14
15
16
17
# File 'lib/extraspace/crawl.rb', line 13

def initialize(stdout: $stdout, stderr: $stderr, url: nil)
  @stdout = stdout
  @stderr = stderr
  @url = url
end

Class Method Details

.runObject



6
7
8
# File 'lib/extraspace/crawl.rb', line 6

def self.run(...)
  new(...).run
end

Instance Method Details

#process(url:) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/extraspace/crawl.rb', line 30

def process(url:)
  @stdout.puts(url)
  facility = Facility.fetch(url: url)
  @stdout.puts(facility.text)
  facility.prices.each { |price| @stdout.puts(price.text) }
  @stdout.puts
rescue FetchError => e
  @stderr.puts("url=#{url} error=#{e.message}")
end

#runObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/extraspace/crawl.rb', line 19

def run
  if @url
    process(url: @url)
  else
    sitemap = Facility.sitemap
    @stdout.puts("count=#{sitemap.links.count}")
    @stdout.puts
    sitemap.links.each { |link| process(url: link.loc) }
  end
end