Class: Amass::OutputFile
- Inherits:
-
Object
- Object
- Amass::OutputFile
- Defined in:
- lib/amass/output_file.rb
Overview
Constant Summary collapse
- PARSERS =
Mapping of formats to parsers.
{ :json => Parsers::JSON, :txt => Parsers::TXT }
- FILE_FORMATS =
Mapping of file extensions to formats
{ '.json' => :json, '.txt' => :txt }
Instance Attribute Summary collapse
-
#format ⇒ :json, :txt
readonly
The format of the output file.
-
#parser ⇒ Parsers::JSON, Parsers::TXT
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) ⇒ :json, :txt
Infers the format from the output file's extension name.
Instance Method Summary collapse
-
#each {|hostname| ... } ⇒ 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.
56 57 58 59 60 61 62 63 |
# File 'lib/amass/output_file.rb', line 56 def initialize(path, format: self.class.infer_format(path)) @path = File.(path) @format = format @parser = PARSERS.fetch(format) do raise(ArgumentError,"unrecognized file type: #{@path.inspect}") end end |
Instance Attribute Details
#format ⇒ :json, :txt (readonly)
The format of the output file.
37 38 39 |
# File 'lib/amass/output_file.rb', line 37 def format @format end |
#parser ⇒ Parsers::JSON, Parsers::TXT (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.
44 45 46 |
# File 'lib/amass/output_file.rb', line 44 def parser @parser end |
#path ⇒ String (readonly)
The path to the output file.
32 33 34 |
# File 'lib/amass/output_file.rb', line 32 def path @path end |
Class Method Details
.infer_format(path) ⇒ :json, :txt
Infers the format from the output file's extension name.
87 88 89 90 91 |
# File 'lib/amass/output_file.rb', line 87 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 {|hostname| ... } ⇒ Enumerator
Parses the contents of the output file.
105 106 107 108 109 110 111 |
# File 'lib/amass/output_file.rb', line 105 def each(&block) return enum_for(__method__) unless block File.open(@path) do |file| @parser.parse(file,&block) end end |
#to_s ⇒ String
Converts the output file to a String.
119 120 121 |
# File 'lib/amass/output_file.rb', line 119 def to_s @path end |