Class: Scraptacular::CommandLine
- Inherits:
-
Object
- Object
- Scraptacular::CommandLine
- Defined in:
- lib/scraptacular/command_line.rb
Instance Method Summary collapse
-
#initialize ⇒ CommandLine
constructor
A new instance of CommandLine.
- #load_file(file_name) ⇒ Object
- #options ⇒ Object
- #parse_options ⇒ Object
- #run(options, out = $stdout) ⇒ Object
- #validate_file(file_name, caption, out) ⇒ Object
Constructor Details
#initialize ⇒ CommandLine
Returns a new instance of CommandLine.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/scraptacular/command_line.rb', line 7 def initialize at_exit do next unless $!.nil? || $!.kind_of?(SystemExit) status = run().to_i exit status if status != 0 end end |
Instance Method Details
#load_file(file_name) ⇒ Object
18 19 |
# File 'lib/scraptacular/command_line.rb', line 18 def load_file(file_name) end |
#options ⇒ Object
21 22 23 |
# File 'lib/scraptacular/command_line.rb', line 21 def @options ||= {} end |
#parse_options ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/scraptacular/command_line.rb', line 25 def @options = {} OptionParser.new do |parser| parser. = "Usage: scraptacular -d DEFINITION_FILE -s SESSION_FILE" parser.on('-d', '--definition-file DEFINITION_FILE', 'Specify a file container scraper definitions') do |file| @options[:definition_file] = file end parser.on('-s','--session-file SESSION_FILE','Specify groups, suites, and URLs to be scraped') do |file| @options[:session_file] = file end parser.on('-o','--output-file [OUTPUT_FILE]', 'Scrape result output file. Only useful for text output') do |file| @options[:output_file] = file end parser.on('-g GROUP', '--group', 'Specify a single group to scrape') do |group| @options[:only_group] = group end parser.on('-f', '--format [FORMAT]', 'only "json" is supported') do |format| @options[:format] = "json" end parser.on_tail('-h','--help','The help file') do puts parser exit end end.parse! end |
#run(options, out = $stdout) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/scraptacular/command_line.rb', line 56 def run(, out = $stdout) return 1 unless (validate_file([:definition_file], "definition file", out) && validate_file([:session_file], "session file", out) ) res = Scraptacular.world.run() #TODO: Replace with "Formatters" e.g. JSONFormatter, XMLFormatter, whatever if [:format] == "json" res.each do |group, suites| suites.each do |suite, results| suites[suite] = results.map!(&:to_h) end end res = res.to_json end # TODO : Replace with "Outputters". if [:output_file] File.open([:output_file], 'w') { |file| file.write(res) } else out.puts res end return 0 end |
#validate_file(file_name, caption, out) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/scraptacular/command_line.rb', line 83 def validate_file(file_name, caption, out) if file_name.nil? out.puts "You must specify a #{caption}. See --help for more information" return false else if File.exists?(file_name) load file_name return true else out.puts "The #{caption} specified does not exist: #{file_name}" return false end end end |