Skip to content

Commit

Permalink
Added a common WordlistMetadata module for #url.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Jan 12, 2024
1 parent 7a4b3e1 commit 41ea025
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 17 deletions.
19 changes: 11 additions & 8 deletions lib/ronin/wordlists/wordlist_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# along with ronin-wordlists. If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/wordlists/wordlist_metadata'
require 'ronin/wordlists/exceptions'
require 'ronin/core/system'

Expand All @@ -31,6 +32,8 @@ module Wordlists
#
class WordlistFile

include WordlistMetadata

# The path to the wordlist file.
#
# @return [String]
Expand All @@ -41,24 +44,24 @@ class WordlistFile
# @return [String]
attr_reader :name

# The optional URL for the wordlist file.
#
# @return [String, nil]
attr_reader :url

#
# Initializes the wordlist file.
#
# @param [String] path
# The path to the wordlist file.
#
# @param [String, nil] url
# @param [Hash{Symbol => Object}] kwargs
# Additional metadata keyword arguments for
# {WordlistMetadata#initialize}.
#
# @option kwargs [String, nil] :url
# The optional URL for the wordlist file.
#
def initialize(path, url: nil)
def initialize(path,**kwargs)
super(**kwargs)

@path = path
@name = File.basename(@path,File.extname(@path))
@url = url
end

#
Expand Down
40 changes: 40 additions & 0 deletions lib/ronin/wordlists/wordlist_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true
#
# ronin-wordlists - A library and tool for managing wordlists.
#
# Copyright (c) 2023 Hal Brodigan (postmodern.mod3@gmail.com)
#
# ronin-wordlists is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-wordlists is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-wordlists. If not, see <https://www.gnu.org/licenses/>.
#

module Ronin
module Wordlists
module WordlistMetadata
# The optional URL for the wordlist.
#
# @return [String, nil]
attr_reader :url

#
# Initializes the wordlist metadata attributes.
#
# @param [String, nil] url
# The optional URL of the wordlist.
#
def initialize(url: nil)
@url = url
end
end
end
end
14 changes: 11 additions & 3 deletions lib/ronin/wordlists/wordlist_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# along with ronin-wordlists. If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/wordlists/wordlist_metadata'
require 'ronin/wordlists/exceptions'

require 'fileutils'
Expand All @@ -30,6 +31,8 @@ module Wordlists
#
class WordlistRepo

include WordlistMetadata

# The path to the wordlist repository.
#
# @return [String]
Expand All @@ -46,14 +49,19 @@ class WordlistRepo
# @param [String] path
# The path to the wordlist repository.
#
# @param [String, nil] url
# @param [Hash{Symbol => Object}] kwargs
# Additional metadata keyword arguments for
# {WordlistMetadata#initialize}.
#
# @option kwargs [String, nil] :url
# The optional URL of the wordlist repository.
# If no URL is given, {#url} will infer it from the git repository.
#
def initialize(path, url: nil)
def initialize(path,**kwargs)
super(**kwargs)

@path = path
@name = File.basename(@path)
@url = url
end

#
Expand Down
26 changes: 22 additions & 4 deletions spec/wordlist_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@
require 'ronin/wordlists/wordlist_file'
require 'ronin/core/system'

require_relative 'wordlist_metadata_examples'

describe Ronin::Wordlists::WordlistFile do
subject { described_class.new(path, url: url) }

let(:fixtures_dir) { File.expand_path(File.join(__dir__, '..', 'spec', 'fixtures')) }

let(:path) { File.join(fixtures_dir, 'wordlists') }
let(:url) { "www.example.com" }
let(:url) { "https://www.example.com/wordlist" }

describe "#initialzie" do
it "must initialize #path, #name, and #url" do
subject { described_class.new(path) }

it "must initialize #path and #name" do
expect(subject.path).to eq(path)
expect(subject.name).to eq("wordlists")
expect(subject.url).to eq("www.example.com")
end

it "must default #name to the basename of #path" do
expect(subject.name).to eq(File.basename(path))
end

include_context "WordlistMetadata#initialize"

context "when the url: keyword is given" do
subject { described_class.new(path, url: url) }

it "must initialize #path, #name, and #url" do
expect(subject.path).to eq(path)
expect(subject.name).to eq(File.basename(path))
expect(subject.url).to eq(url)
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/wordlist_metadata_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rspec'

shared_examples_for "WordlistMetadata#initialize" do
it "must default #url to nil" do
expect(subject.url).to be(nil)
end
end
22 changes: 20 additions & 2 deletions spec/wordlist_repo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
require 'spec_helper'
require 'ronin/wordlists/wordlist_repo'

require_relative 'wordlist_metadata_examples'

describe Ronin::Wordlists::WordlistRepo do
subject { described_class.new(path) }

let(:fixtures_dir) { File.expand_path(File.join(__dir__, '..', 'spec', 'fixtures')) }

let(:path) { File.join(fixtures_dir, 'wordlists') }
let(:url) { "https://www.example.com/wordlist" }

describe "#initialize" do
it "must initialize #path and #name" do
it "must initialize #path" do
expect(subject.path).to eq(path)
expect(subject.name).to eq("wordlists")
end

it "must default #name to the basename of #path" do
expect(subject.name).to eq(File.basename(path))
end

include_context "WordlistMetadata#initialize"

context "when the url: keyword is given" do
subject { described_class.new(path, url: url) }

it "must initialize #path, #name, and #url" do
expect(subject.path).to eq(path)
expect(subject.name).to eq(File.basename(path))
expect(subject.url).to eq(url)
end
end
end

Expand Down

0 comments on commit 41ea025

Please sign in to comment.