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



151
152
153
# File 'lib/threatinator/feed_builder.rb', line 151

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



122
123
124
125
126
127
128
129
# File 'lib/threatinator/feed_builder.rb', line 122

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


137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/threatinator/feed_builder.rb', line 137

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/threatinator/feed_builder.rb', line 107

def build
  Feed.new(
    :provider => @provider, 
    :name => @name,
    :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



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

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

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



26
27
28
29
30
31
32
33
# File 'lib/threatinator/feed_builder.rb', line 26

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



74
75
76
77
78
# File 'lib/threatinator/feed_builder.rb', line 74

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.



88
89
90
91
92
# File 'lib/threatinator/feed_builder.rb', line 88

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.



81
82
83
84
85
# File 'lib/threatinator/feed_builder.rb', line 81

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



64
65
66
67
68
69
70
71
# File 'lib/threatinator/feed_builder.rb', line 64

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



55
56
57
58
59
60
61
62
# File 'lib/threatinator/feed_builder.rb', line 55

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



46
47
48
49
50
51
52
53
# File 'lib/threatinator/feed_builder.rb', line 46

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/threatinator/feed_builder.rb', line 35

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