Class: Threatinator::FeedBuilder
- Inherits:
-
Object
- Object
- Threatinator::FeedBuilder
- Defined in:
- lib/threatinator/feed_builder.rb
Class Method Summary collapse
-
.from_dsl(&block) ⇒ Object
Executes the block parameter within DSL scope.
-
.from_file(filename) ⇒ Object
Loads the provided file, and generates a builder from it.
-
.from_string(str, filename = nil, lineno = nil) ⇒ Object
Generates a builder from a string via eval.
Instance Method Summary collapse
- #build ⇒ Object
-
#decode_gzip ⇒ Object
(also: #extract_gzip, #gunzip)
Add the Gzip decoder.
- #fetch_http(url, opts = {}) ⇒ Object
-
#filter(&block) ⇒ Object
Specify a block filter for the parser.
-
#filter_comments ⇒ Object
Filter out whitespace lines.
-
#filter_whitespace ⇒ Object
Filter out whitespace lines.
- #name(name) ⇒ Object
- #parse_csv(opts = {}, &block) ⇒ Object
- #parse_eachline(opts = {}, &block) ⇒ Object
- #parse_json(opts = {}, &block) ⇒ Object
- #parse_xml(pattern_string, opts = {}, &block) ⇒ Object
- #provider(provider_name) ⇒ Object
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.
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.
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
#build ⇒ Object
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_gzip ⇒ Object 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_comments ⇒ Object
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_whitespace ⇒ Object
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 |