Class: Wordlist::File

Inherits:
AbstractWordlist show all
Defined in:
lib/wordlist/file.rb

Overview

Represents a .txt file wordlist.

wordlist = Wordlist::File.new("rockyou.txt")
wordlist.each do |word|
  puts word
end

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ListMethods

#capitalize, #concat, #downcase, #gsub, #intersect, #mutate, #mutate_case, #power, #product, #sub, #subtract, #tr, #union, #uniq, #upcase

Constructor Details

#initialize(path, format: Format.infer(path)) ⇒ File

Opens a wordlist file.

Parameters:

  • path (String)

    The path to the .txt file wordlist read from.

  • format (:txt, :gz, :bzip2, :xz, :zip, :7zip, nil) (defaults to: Format.infer(path))

    The format of the wordlist. If not given the format will be inferred from the file extension.

Raises:

Since:

  • 1.0.0



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wordlist/file.rb', line 48

def initialize(path, format: Format.infer(path))
  @path   = ::File.expand_path(path)
  @format = format

  unless ::File.file?(@path)
    raise(WordlistNotFound,"wordlist file does not exist: #{@path.inspect}")
  end

  unless Format::FORMATS.include?(@format)
    raise(UnknownFormat,"unknown format given: #{@format.inspect}")
  end
end

Instance Attribute Details

#format:txt, ... (readonly)

The format of the wordlist file.

Returns:

  • (:txt, :gzip, :bzip2, :xz, :zip, :7zip)

Since:

  • 1.0.0



28
29
30
# File 'lib/wordlist/file.rb', line 28

def format
  @format
end

#pathObject (readonly)

The path to the .txt file

Since:

  • 1.0.0



23
24
25
# File 'lib/wordlist/file.rb', line 23

def path
  @path
end

Class Method Details

.open(path, **kwargs) {|wordlist| ... } ⇒ File

Opens a wordlist file.

Parameters:

  • path (String)

    The path to the .txt file wordlist read from.

Yields:

  • (wordlist)

    If a block is given, it will be passed the opened wordlist.

Yield Parameters:

  • wordlist (File)

    The newly opened wordlist.

Returns:

  • (File)

    The newly opened wordlist.

See Also:

Since:

  • 1.0.0



80
81
82
83
84
# File 'lib/wordlist/file.rb', line 80

def self.open(path,**kwargs)
  wordlist = new(path,**kwargs)
  yield wordlist if block_given?
  return wordlist
end

.read(path, **kwargs) {|word| ... } ⇒ Enumerator

Opens and reads the wordlist file.

Parameters:

  • path (String)

    The path to the .txt file wordlist read from.

Yields:

  • (word)

    The given block will be passed every word from the wordlist.

Yield Parameters:

  • word (String)

    A word from the wordlist.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



101
102
103
# File 'lib/wordlist/file.rb', line 101

def self.read(path,**kwargs,&block)
  open(path,**kwargs).each(&block)
end

Instance Method Details

#each {|word| ... } ⇒ Enumerator

Note:

Empty lines and lines beginning with # characters will be ignored.

Enumerates through every word in the .txt file.

Examples:

wordlist.each do |word|
  puts word
end

Yields:

  • (word)

    The given block will be passed every word from the wordlist.

Yield Parameters:

  • word (String)

    A word from the wordlist.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/wordlist/file.rb', line 147

def each
  return enum_for(__method__) unless block_given?

  each_line do |line|
    line.chomp!

    unless (line.empty? || line.start_with?('#'))
      yield line
    end
  end
end

#each_line {|line| ... } ⇒ Enumerator

Enumerates through each line in the .txt file wordlist.

Yields:

  • (line)

    The given block will be passed each line from the .txt file.

Yield Parameters:

  • line (String)

    A newline terminated line from the file.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



119
120
121
122
123
# File 'lib/wordlist/file.rb', line 119

def each_line(&block)
  return enum_for(__method__) unless block

  open { |io| io.each_line(&block) }
end