Class: Oober::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/oober/cli.rb

Instance Method Summary collapse

Instance Method Details

#consoleObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/oober/cli.rb', line 58

def console
  if File.exist?(options[:config])
    begin
      client = Oober.configure(options[:config]) 
      puts 'your oober client is in the local variable "client"'
    rescue JSON::ParserError => e
      puts "invalid config #{options[:config]}"
    end
  end
  binding.pry
end

#download_feedObject



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

def download_feed
  oob=Oober.configure(options[:config])
  data_blocks = oob.get_blocks
  data_blocks.each_with_index do |block,index|
    filename = format('%s_%08d.%08d.xml',
                      oob.feed_name,
                      Time.new.to_i,
                      index)
    file_path = File.join(options[:dir],filename)
    FileUtils.mkdir_p(options[:dir]) unless Dir.exist?(options[:dir])
    File.open(file_path, 'w') do |file|
      file.puts(block)
    end
  end
end

#poll_feedObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/oober/cli.rb', line 17

def poll_feed
  oob=Oober.configure(options[:config])
  errored_blocks = []      
  oob.extract_blocks.each do |extracted_hash|
    begin
      event = CEF::Event.new(extracted_hash)
      oob.cef.emit(event)
    rescue Exception  => exception
      if options[:warn]
        errored_blocks.push([extracted_hash,exception])
      end
    else
      puts event.to_cef
    end
    errored_blocks.each do |blk|
      event, error = blk
      STDERR.puts "ERROR: #{error.to_s}"
      STDERR.puts "ERRORED MESSAGE: #{JSON.pretty_generate(event)}"
    end
  end
end

#xpath(*files) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/oober/cli.rb', line 73

def xpath(*files)
  xmls=files.map do |filename|
    filedata = File.read(filename)
    Nokogiri::XML(filedata,nil,nil,Nokogiri::XML::ParseOptions::DEFAULT_XML|Nokogiri::XML::ParseOptions::NOBLANKS)
  end

  file_xmls=Hash[files.zip(xmls)]

  while query = Readline.readline(format('XPath Query[%d]> ',files.size), true)
    if matched = query.match(%r{\A(children|save) (.*)})
      cmd   = matched[1]
      query = matched[2]
    else
      q   = query
      cmd = nil
    end

    file_xmls.each do |filename, xml|
      begin
        [*xml.xpath(q)].each_with_index do |node,index|
          puts format(options[:separator],filename,index)
          case cmd
            when nil
              puts node.to_s
            when /save/
              fname = File.join(options[:dir],format('saved_%012d.xml',index))
              File.open(fname,'w') {|f| f.puts node.children.first.to_s }
            when /children/
              list_children(node)
            else
              puts "unknown operation: #{cmd}"
          end
        end
      rescue Nokogiri::XML::XPath::SyntaxError => e
        #puts "could not get #{q}"
      end
    end
  end
end