class Threatinator::FeedBuilder

Public Class Methods

from_dsl(&block) click to toggle source

Executes the block parameter within DSL scope

# File lib/threatinator/feed_builder.rb, line 157
def self.from_dsl(&block)
  Docile.dsl_eval(self.new, &block)
end
from_file(filename) click to toggle source

Loads the provided file, and generates a builder from it. @param [String] filename The name of the file to read the feed from @raise [FeedFileNotFoundError] if the file is not found

# 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) click to toggle source

Generates a builder from a string via eval. @param [String] str The DSL code that specifies the feed. @param [String] filename (nil) Passed to eval. @param [String] lineno (nil) Passed to eval. @raise [FeedFileNotFoundError] if the file is not found @see Kernel#eval for details on filename and lineno

# 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

Public Instance Methods

build() click to toggle source
# 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_gzip() click to toggle source

Add the Gzip decoder

# File lib/threatinator/feed_builder.rb, line 100
def decode_gzip
  decoder_builders << lambda { Threatinator::Decoders::Gzip.new }
  self
end
Also aliased as: extract_gzip, gunzip
event_types(event_types=[:notlabeled]) click to toggle source
# File lib/threatinator/feed_builder.rb, line 26
def event_types(event_types=[:notlabeled])
  @event_types ||= event_types
  self
end
extract_gzip()
Alias for: decode_gzip
fetch_http(url, opts = {}) click to toggle source
# 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) click to toggle source

Specify a block filter for the parser

# File lib/threatinator/feed_builder.rb, line 79
def filter(&block)
  @filter_builders ||= []
  @filter_builders << lambda { Threatinator::Filters::Block.new(block) }
  self
end
filter_comments() click to toggle source

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

# File lib/threatinator/feed_builder.rb, line 93
def filter_comments
  @filter_builders ||= []
  @filter_builders << lambda { Threatinator::Filters::Comments.new }
  self
end
filter_whitespace() click to toggle source

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

# File lib/threatinator/feed_builder.rb, line 86
def filter_whitespace
  @filter_builders ||= []
  @filter_builders << lambda { Threatinator::Filters::Whitespace.new }
  self
end
gunzip()
Alias for: decode_gzip
name(name) click to toggle source
# File lib/threatinator/feed_builder.rb, line 21
def name(name)
  @name = name
  self
end
parse_csv(opts = {}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/threatinator/feed_builder.rb, line 16
def provider(provider_name)
  @provider = provider_name
  self
end

Private Instance Methods

decoder_builders() click to toggle source
# File lib/threatinator/feed_builder.rb, line 107
def decoder_builders
  @decoder_builders ||= []
end