class WonderScrape::Commands::Scrape

Constants

VALID_SCRAPER_NAMES
VALID_WRITERS

Attributes

raw_options[R]

Public Class Methods

new(raw_options) click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 23
def initialize(raw_options)
  @raw_options = raw_options
end

Public Instance Methods

execute(input: $stdin, output: $stdout) click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 27
def execute(input: $stdin, output: $stdout)
  recorder = WonderScrape::Recorder.new(output, options)
  writer = build_writer
  scraper = build_scraper(writer, recorder)

  scraper.scrape
  writer.output_results
  recorder.print
end

Private Instance Methods

approximate_records() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 84
def approximate_records
  target_module::RESULTS_PER_PAGE * num_pages
end
build_scraper(writer, recorder) click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 41
def build_scraper(writer, recorder)
  target_module.new(writer, recorder, options)
end
build_writer() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 45
def build_writer
  case output
  when WonderScrape::Writers::CSV::NAME
    WonderScrape::Writers::CSV.new(file, target_module::FIELDS)
  when WonderScrape::Writers::Hash::NAME
    WonderScrape::Writers::Hash.new
  end
end
file() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 73
def file
  @file ||= raw_options[:file] || prompt.ask('Please specify the file path you want to write to:', required: true)
end
num_pages() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 88
def num_pages
  @num_pages ||= raw_options[:num_pages] || prompt.ask('How many pages of search results do you want to scrape?', default: target_module::DEFAULT_MAX_PAGES, convert: :int)
end
options() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 77
def options
  @options ||= raw_options.merge({
                                   progress_bar: progress_bar,
                                   num_pages: num_pages
                                 })
end
output() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 69
def output
  @output ||= raw_options[:format] || prompt.select('How would you like to output?', VALID_WRITERS)
end
progress_bar() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 61
def progress_bar
  TTY::ProgressBar.new('[:bar] :percent', total: approximate_records)
end
target() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 65
def target
  @target ||= raw_options[:target] || prompt.select('What website would you like to scrape?', VALID_SCRAPER_NAMES)
end
target_module() click to toggle source
# File lib/wonder_scrape/commands/scrape.rb, line 54
def target_module
  @target_module ||= case target
                     when WonderScrape::Scrapers::MFC::Scraper::NAME
                       WonderScrape::Scrapers::MFC::Scraper
  end
end