Class: Amass::OutputFile

Inherits:
Object
  • Object
show all
Defined in:
lib/amass/output_file.rb

Overview

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

Constant Summary collapse

PARSERS =

Mapping of formats to parsers.

API:

  • semipublic

{
  :json => Parsers::JSON,
  :txt  => Parsers::TXT
}
FILE_FORMATS =

Mapping of file extensions to formats

API:

  • semipublic

{
  '.json' => :json,
  '.txt'  => :txt
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, format: self.class.infer_format(path)) ⇒ OutputFile

Initializes the output file.

Parameters:

  • The path to the output file.

  • (defaults to: self.class.infer_format(path))

    The optional format of the output file. If not given, it will be inferred by the path's file extension.

API:

  • public



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.expand_path(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.

Returns:

API:

  • public



37
38
39
# File 'lib/amass/output_file.rb', line 37

def format
  @format
end

#parserParsers::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.

Returns:

API:

  • private



44
45
46
# File 'lib/amass/output_file.rb', line 44

def parser
  @parser
end

#pathString (readonly)

The path to the output file.

Returns:

API:

  • public



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.

Parameters:

  • The path to the output file.

Returns:

  • The output format inferred from the file's extension name.

Raises:

  • The output format could not be inferred from the file's name.

API:

  • semipublic



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.

Yields:

  • (hostname)

    The given block will be passed each parsed hostname.

Yield Parameters:

  • hostname (Hostname)

    A parsed hostname from the output file.

Returns:

  • If no block is given, an Enumerator object will be returned.

API:

  • public



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_sString

Converts the output file to a String.

Returns:

  • The path to the output file.

API:

  • public



119
120
121
# File 'lib/amass/output_file.rb', line 119

def to_s
  @path
end