Class: Threatinator::Actions::List::Action
- Inherits:
-
Threatinator::Action
- Object
- Threatinator::Action
- Threatinator::Actions::List::Action
- Defined in:
- lib/threatinator/actions/list/action.rb
Instance Attribute Summary
Attributes inherited from Threatinator::Action
Instance Method Summary collapse
- #exec ⇒ Object
-
#initialize(registry, config) ⇒ Action
constructor
A new instance of Action.
- #output_json(io_out) ⇒ Object
- #output_table(io_out) ⇒ Object
Constructor Details
#initialize(registry, config) ⇒ Action
Returns a new instance of Action.
10 11 12 13 |
# File 'lib/threatinator/actions/list/action.rb', line 10 def initialize(registry, config) super(registry) @config = config end |
Instance Method Details
#exec ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/threatinator/actions/list/action.rb', line 84 def exec case @config.format when 'table' output_table($stdout) when 'json' output_json($stdout) else raise ArgumentError, "Invalid argument for 'format' = '#{@config.format}'" end end |
#output_json(io_out) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/threatinator/actions/list/action.rb', line 61 def output_json(io_out) feeds = [] registry.each do |feed| info = { provider: feed.provider, name: feed.name, type: 'unknown', link: 'unknown' } fetcher = feed.fetcher_builder.call() case fetcher when Threatinator::Fetchers::Http info[:type] = "http" info[:link] = fetcher.url end feeds << info end feeds.sort! { |a,b| [a[:provider], a[:name]] <=> [b[:provider], b[:name]] } io_out.write(MultiJson.dump(feeds)) end |
#output_table(io_out) ⇒ Object
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 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/threatinator/actions/list/action.rb', line 15 def output_table(io_out) feed_info = [['provider', 'name', 'type', 'link/path', 'event_types']] registry.each do |feed| #binding.pry 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 info << feed.event_types feed_info << info end # This will never happen return if feed_info.count == 0 fmts = [] widths = [] 0.upto(4) do |i| max = feed_info.max { |a,b| a[i].to_s.length <=> b[i].to_s.length }[i].to_s.length widths << max fmts << "%#{max}s" end fmt = "%-#{widths[0]}s %-#{widths[1]}s %-#{widths[2]}s %-#{widths[3]}s %-#{widths[4]}s\n" io_out.printf(fmt, *(feed_info.shift)) # Pop the Header from the feed_info 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 |