Class: Masscan::OutputFile
- Inherits:
-
Object
- Object
- Masscan::OutputFile
- Includes:
- Enumerable
- Defined in:
- lib/masscan/output_file.rb
Overview
Constant Summary collapse
- PARSERS =
Mapping of formats to parsers.
{ binary: Parsers::Binary, list: Parsers::List, json: Parsers::JSON, ndjson: Parsers::JSON, # xml: Parsers::XML, }
- FILE_FORMATS =
Mapping of file extensions to formats
{ '.bin' => :binary, '.dat' => :binary, '.txt' => :list, '.list' => :list, '.json' => :json, '.ndjson' => :ndjson, '.xml' => :xml }
Instance Attribute Summary collapse
-
#format ⇒ Symbol
readonly
The format of the output file.
-
#parser ⇒ Parsers::Binary, ...
readonly
private
The parser for the output file format.
-
#path ⇒ String
readonly
The path to the output file.
Class Method Summary collapse
-
.infer_format(path) ⇒ :binary, ...
Infers the format from the output file's extension name.
Instance Method Summary collapse
-
#each {|record| ... } ⇒ Enumerator
Parses the contents of the output file.
-
#initialize(path, format: self.class.infer_format(path)) ⇒ OutputFile
constructor
Initializes the output file.
-
#to_s ⇒ String
Converts the output file to a String.
Constructor Details
#initialize(path, format: self.class.infer_format(path)) ⇒ OutputFile
Initializes the output file.
62 63 64 65 66 67 68 69 |
# File 'lib/masscan/output_file.rb', line 62 def initialize(path, format: self.class.infer_format(path)) @path = path @format = format @parser = PARSERS.fetch(format) do raise(ArgumentError,"unknown format: #{format.inspect}") end end |
Instance Attribute Details
#format ⇒ Symbol (readonly)
The format of the output file.
41 42 43 |
# File 'lib/masscan/output_file.rb', line 41 def format @format end |
#parser ⇒ Parsers::Binary, ... (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The parser for the output file format.
48 49 50 |
# File 'lib/masscan/output_file.rb', line 48 def parser @parser end |
#path ⇒ String (readonly)
The path to the output file.
36 37 38 |
# File 'lib/masscan/output_file.rb', line 36 def path @path end |
Class Method Details
.infer_format(path) ⇒ :binary, ...
Infers the format from the output file's extension name.
101 102 103 104 105 |
# File 'lib/masscan/output_file.rb', line 101 def self.infer_format(path) FILE_FORMATS.fetch(File.extname(path)) do raise(ArgumentError,"could not infer format of #{path}") end end |
Instance Method Details
#each {|record| ... } ⇒ Enumerator
Parses the contents of the output file.
119 120 121 122 123 124 125 |
# File 'lib/masscan/output_file.rb', line 119 def each(&block) return enum_for(__method__) unless block @parser.open(@path) do |file| @parser.parse(file,&block) end end |
#to_s ⇒ String
Converts the output file to a String.
135 136 137 |
# File 'lib/masscan/output_file.rb', line 135 def to_s @path end |