Class: Threatinator::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/threatinator/feed.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Feed

Returns a new instance of Feed.

Parameters:

  • opts (Hash) (defaults to: {})

    Options hash

Options Hash (opts):

  • :provider (String)

    The name of the provider

  • :name (String)

    The name of the feed

  • :parser_block (Proc)

    A block that will be called by the parser each time it processes a record.

  • :parser_builder (Proc)

    A proc that, when called, will return a brand new instance of a Threatinator::Parser.

  • :fetcher_builder (Proc)

    A proc that, when called, will return a brand new instance of a Threatinator::Fetcher.

  • :filter_builders (Array<Proc>)

    An array of procs that, when called, will each return an instance of a filter (something that responds to :filter?)

  • :decoder_builders (Array<Proc>)

    An array of procs that, when called, will each return an instance of a Threatinator::Decoder



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/threatinator/feed.rb', line 19

def initialize(opts = {})
  @provider = opts.delete(:provider)
  @name = opts.delete(:name)
  @event_types = opts.delete(:event_types) || [:uknown]
  @parser_block = opts.delete(:parser_block)

  @parser_builder = opts.delete(:parser_builder)
  @fetcher_builder = opts.delete(:fetcher_builder)
  @filter_builders = opts.delete(:filter_builders) || []
  @decoder_builders = opts.delete(:decoder_builders) || []
  validate!
end

Instance Method Details

#decoder_buildersObject



60
61
62
# File 'lib/threatinator/feed.rb', line 60

def decoder_builders
  @decoder_builders.dup
end

#event_typesObject



40
41
42
# File 'lib/threatinator/feed.rb', line 40

def event_types
  @event_types.dup
end

#fetcher_builderObject



48
49
50
# File 'lib/threatinator/feed.rb', line 48

def fetcher_builder
  @fetcher_builder
end

#filter_buildersObject



56
57
58
# File 'lib/threatinator/feed.rb', line 56

def filter_builders
  @filter_builders.dup
end

#nameObject



36
37
38
# File 'lib/threatinator/feed.rb', line 36

def name
  @name.dup
end

#parser_blockObject



44
45
46
# File 'lib/threatinator/feed.rb', line 44

def parser_block
  @parser_block
end

#parser_builderObject



52
53
54
# File 'lib/threatinator/feed.rb', line 52

def parser_builder
  @parser_builder
end

#providerObject



32
33
34
# File 'lib/threatinator/feed.rb', line 32

def provider
  @provider.dup
end

#validate!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/threatinator/feed.rb', line 64

def validate!
  validate_attribute!(:provider, @provider) { |x| x.kind_of?(::String) }
  validate_attribute!(:name, @name) { |x| x.kind_of?(::String) }
  validate_attribute!(:event_types, @event_types) { |x| x.kind_of?(::Array) }
  validate_attribute!(:parser_block, @parser_block) { |x| x.kind_of?(::Proc) }
  validate_attribute!(:fetcher_builder, @fetcher_builder) { |x| x.kind_of?(::Proc) }
  validate_attribute!(:parser_builder, @parser_builder) { |x| x.kind_of?(::Proc) }
  validate_attribute!(:filter_builders, @filter_builders) do |x|
    x.kind_of?(::Array) &&
      x.all? { |e| e.kind_of?(::Proc) }
  end

  validate_attribute!(:decoder_builders, @decoder_builders) do |x|
    x.kind_of?(::Array) &&
      x.all? { |e| e.kind_of?(::Proc) }
  end
end

#validate_attribute!(name, val, &block) ⇒ Object



82
83
84
85
86
# File 'lib/threatinator/feed.rb', line 82

def validate_attribute!(name, val, &block)
  unless block.call(val) == true
    raise Threatinator::Exceptions::InvalidAttributeError.new("Invalid attribute (#{name}). Got: #{val.inspect}")
  end
end