class Amass::OutputFile
Represents either a ‘.json` or `.txt` output file.
## Example
require 'amass/output_file' output_file = Amass::OutputFile.new('/path/to/amass.json') output_file.each do |hostname| p hostname end
@api public
Constants
- FILE_FORMATS
Mapping of file extensions to formats
@api semipublic
- PARSERS
Mapping of formats to parsers.
@api semipublic
Attributes
format[R]
The format of the output file.
@return [:json, :txt]
parser[R]
path[R]
The path to the output file.
@return [String]
Public Class Methods
infer_format(path)
click to toggle source
Infers the format from the output file’s extension name.
@param [String] path
The path to the output file.
@return [:json, :txt]
The output format inferred from the file's extension name.
@raise [ArgumentError]
The output format could not be inferred from the file's name.
@api semipublic
# 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
new(path, format: self.class.infer_format(path))
click to toggle source
Initializes the output file.
@param [String] path
The path to the output file.
@param [:json, :txt] format
The optional format of the output file. If not given, it will be inferred by the path's file extension.
# File lib/amass/output_file.rb, line 56 def initialize(path, format: self.class.infer_format(path)) @path = File.expand_path(path) @format = format @parser = PARSERS.fetch(format) do raise(ArgumentError,"unrecognized file type: #{@path.inspect}") end end
Public Instance Methods
each(&block)
click to toggle source
Parses the contents of the output file.
@yield [hostname]
The given block will be passed each parsed hostname.
@yieldparam [Hostname] hostname
A parsed hostname from the output file.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
# 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()
click to toggle source
Converts the output file to a String.
@return [String]
The path to the output file.
# File lib/amass/output_file.rb, line 119 def to_s @path end