Class: Threatinator::FeedBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_dsl(&block) ⇒ Object

Executes the block parameter within DSL scope



157
158
159
# File 'lib/threatinator/feed_builder.rb', line 157

def self.from_dsl(&block)
  Docile.dsl_eval(self.new, &block)
end

.from_file(filename) ⇒ Object

Loads the provided file, and generates a builder from it.

Parameters:

  • filename (String)

    The name of the file to read the feed from

Raises:

  • (FeedFileNotFoundError)

    if the file is not found



128
129
130
131
132
133
134
135
# File 'lib/threatinator/feed_builder.rb', line 128

def self.from_file(filename)
  begin
    filedata = File.read(filename)
  rescue Errno::ENOENT
    raise Threatinator::Exceptions::FeedFileNotFoundError.new(filename)
  end
  from_string(filedata, filename, 0)
end

.from_string(str, filename = nil, lineno = nil) ⇒ Object

Generates a builder from a string via eval.

Parameters:

  • str (String)

    The DSL code that specifies the feed.

  • filename (String) (defaults to: nil)

    (nil) Passed to eval.

  • lineno (String) (defaults to: nil)

    (nil) Passed to eval.

Raises:

  • (FeedFileNotFoundError)

    if the file is not found

See Also:

  • for details on filename and lineno


143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/threatinator/feed_builder.rb', line 143

def self.from_string(str, filename = nil, lineno = nil)
  from_dsl do
    args = [str, binding]
    unless filename.nil?
      args << filename
      unless lineno.nil?
        args << lineno
      end
    end
    eval(*args)
  end
end

Instance Method Details

#buildObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/threatinator/feed_builder.rb', line 112

def build
  Feed.new(
    :provider => @provider,
    :name => @name,
    :event_types => @event_types || [:unknown],
    :parser_block => @parser_block,
    :fetcher_builder => @fetcher_builder,
    :parser_builder => @parser_builder,
    :filter_builders => @filter_builders,
    :decoder_builders => decoder_builders
  )
end

#decode_gzipObject Also known as: extract_gzip, gunzip

Add the Gzip decoder



100
101
102
103
# File 'lib/threatinator/feed_builder.rb', line 100

def decode_gzip
  decoder_builders << lambda { Threatinator::Decoders::Gzip.new }
  self
end

#event_types(event_types = [:notlabeled]) ⇒ Object



26
27
28
29
# File 'lib/threatinator/feed_builder.rb', line 26

def event_types(event_types=[:notlabeled])
  @event_types ||= event_types
  self
end

#fetch_http(url, opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/threatinator/feed_builder.rb', line 31

def fetch_http(url, opts = {})
  opts[:url] = url
  @fetcher_builder = lambda do
    opts_dup = Marshal.load(Marshal.dump(opts))
    Threatinator::Fetchers::Http.new(opts_dup)
  end
  self
end

#filter(&block) ⇒ Object

Specify a block filter for the parser



79
80
81
82
83
# File 'lib/threatinator/feed_builder.rb', line 79

def filter(&block)
  @filter_builders ||= []
  @filter_builders << lambda { Threatinator::Filters::Block.new(block) }
  self
end

#filter_commentsObject

Filter out whitespace lines. Only works on line-based text.



93
94
95
96
97
# File 'lib/threatinator/feed_builder.rb', line 93

def filter_comments
  @filter_builders ||= []
  @filter_builders << lambda { Threatinator::Filters::Comments.new }
  self
end

#filter_whitespaceObject

Filter out whitespace lines. Only works on line-based text.



86
87
88
89
90
# File 'lib/threatinator/feed_builder.rb', line 86

def filter_whitespace
  @filter_builders ||= []
  @filter_builders << lambda { Threatinator::Filters::Whitespace.new }
  self
end

#name(name) ⇒ Object



21
22
23
24
# File 'lib/threatinator/feed_builder.rb', line 21

def name(name)
  @name = name
  self
end

#parse_csv(opts = {}, &block) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/threatinator/feed_builder.rb', line 69

def parse_csv(opts = {}, &block)
  @parser_builder = lambda do
    opts_dup = Marshal.load(Marshal.dump(opts))
    Threatinator::Parsers::CSV::Parser.new(opts_dup, &block)
  end
  @parser_block = block
  self
end

#parse_eachline(opts = {}, &block) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/threatinator/feed_builder.rb', line 60

def parse_eachline(opts = {}, &block)
  @parser_builder = lambda do
    opts_dup = Marshal.load(Marshal.dump(opts))
    Threatinator::Parsers::Getline::Parser.new(opts_dup, &block)
  end
  @parser_block = block
  self
end

#parse_json(opts = {}, &block) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/threatinator/feed_builder.rb', line 51

def parse_json(opts = {}, &block)
  @parser_builder = lambda do
    opts_dup = Marshal.load(Marshal.dump(opts))
    Threatinator::Parsers::JSON::Parser.new(opts_dup, &block)
  end
  @parser_block = block
  self
end

#parse_xml(pattern_string, opts = {}, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/threatinator/feed_builder.rb', line 40

def parse_xml(pattern_string, opts = {}, &block)
  @parser_builder = lambda do
    pattern = Threatinator::Parsers::XML::Pattern.new(pattern_string)
    opts_dup = Marshal.load(Marshal.dump(opts))
    opts_dup[:pattern] = pattern
    Threatinator::Parsers::XML::Parser.new(opts_dup, &block)
  end
  @parser_block = block
  self
end

#provider(provider_name) ⇒ Object



16
17
18
19
# File 'lib/threatinator/feed_builder.rb', line 16

def provider(provider_name)
  @provider = provider_name
  self
end