Class: Threatinator::Actions::List::Action

Inherits:
Threatinator::Action show all
Defined in:
lib/threatinator/actions/list/action.rb

Instance Attribute Summary

Attributes inherited from Threatinator::Action

#registry

Instance Method Summary collapse

Constructor Details

#initialize(registry, config) ⇒ Action

Returns a new instance of Action.



9
10
11
12
# File 'lib/threatinator/actions/list/action.rb', line 9

def initialize(registry, config)
  super(registry)
  @config = config
end

Instance Method Details

#execObject



14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/threatinator/actions/list/action.rb', line 14

def exec
  io_out = $stdout
  feed_info = [['provider', 'name', 'type', 'link/path']]
  registry.each do |feed|
    info = [ feed.provider, feed.name ]
    fetcher = feed.fetcher_builder.call()
    type = "unknown"
    link = "unknown"
    case fetcher
    when Threatinator::Fetchers::Http
      type = "http"
      link = fetcher.url
    end
    info << type
    info << link
    feed_info << info
  end
  return if feed_info.count == 0
  fmts = []
  widths = []
  0.upto(3) do |i|
    max = feed_info.max { |a,b| a[i].length <=> b[i].length }[i].length
    widths << max
    fmts << "%#{max}s"
  end
  fmt = "%-#{widths[0]}s  %-#{widths[1]}s  %-#{widths[2]}s  %-#{widths[3]}s\n"
  io_out.printf(fmt, *(feed_info.shift))
  sep = widths.map {|x| '-' * x }
  io_out.printf(fmt, *sep)
  feed_info.sort! { |a,b| [a[0], a[1]] <=> [b[0], b[1]] }
  feed_info.each  do |info|
    io_out.printf(fmt, *info)
  end
  io_out.printf(fmt, *sep)
  io_out.puts("Total: #{feed_info.count}")
end