Class: Threatinator::Actions::Run::CoverageObserver

Inherits:
Object
  • Object
show all
Defined in:
lib/threatinator/actions/run/coverage_observer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ CoverageObserver

Returns a new instance of CoverageObserver.



8
9
10
11
# File 'lib/threatinator/actions/run/coverage_observer.rb', line 8

def initialize(filename)
  @filename = filename
  @csv = nil
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/threatinator/actions/run/coverage_observer.rb', line 7

def filename
  @filename
end

Instance Method Details

#closeObject



53
54
55
# File 'lib/threatinator/actions/run/coverage_observer.rb', line 53

def close
  @csv.close unless closed?
end

#closed?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/threatinator/actions/run/coverage_observer.rb', line 13

def closed?
  return false if @csv.nil?
  @csv.closed?
end

#log_record(status, record, event_count, message = '') ⇒ Object

Parameters:



46
47
48
49
50
51
# File 'lib/threatinator/actions/run/coverage_observer.rb', line 46

def log_record(status, record, event_count, message = '')
  return if closed?
  @csv.add_row(  [
    status, event_count, record.line_number, 
    record.pos_start, record.pos_end, record.data.inspect, message])
end

#openObject



18
19
20
# File 'lib/threatinator/actions/run/coverage_observer.rb', line 18

def open
  @csv = ::CSV.open(@filename, "wb", :headers => [:status, :event_count, :line_number, :pos_start, :pos_end, :data, :message], :write_headers => true)
end

#update(message, *args) ⇒ Object

Handles FeedRunner observations



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/threatinator/actions/run/coverage_observer.rb', line 23

def update(message, *args)
  case message
  when :record_missed
    log_record(:missed, args.shift, 0)
  when :record_filtered
    log_record(:filtered, args.shift, 0)
  when :record_parsed
    log_record(:parsed, args.shift, args.shift.count)
  when :record_error
    record = args.shift
    errors = args.shift
    message = errors.map { |e| e.message }.join(', ')
    log_record(:error, record, 0, message)
  when :end
    close
  when :start
    open
  end
end