stephencelis-haddock 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,19 @@
1
+ === 0.1.1 / 2009-03-29
2
+
3
+ * 2 minor enhancements
4
+
5
+ * Add version option to command-line utility.
6
+ * Bumping up version numbers to improve credibility.
7
+
8
+
9
+ === 0.1.0 / 2009-03-29
10
+
11
+ * 2 minor enhancements
12
+
13
+ * Friendlier command-line errors.
14
+ * General cleanup.
15
+
16
+
1
17
  === 0.0.1 / 2009-03-28
2
18
 
3
19
  * 1 major enhancement
data/Manifest.txt CHANGED
@@ -4,4 +4,5 @@ README.txt
4
4
  Rakefile
5
5
  bin/haddock
6
6
  lib/haddock.rb
7
+ test/names.txt
7
8
  test/test_haddock.rb
data/bin/haddock CHANGED
@@ -8,13 +8,20 @@ include Haddock
8
8
  parser = OptionParser.new do |opts|
9
9
  opts.banner = "usage: #{File.basename($0)} [options]"
10
10
 
11
+ opts.on("-V", "--version") do
12
+ require 'capistrano/version'
13
+ puts "haddock: v#{Haddock::VERSION}"
14
+ exit
15
+ end
16
+
11
17
  opts.on("-h", "--help") do
12
18
  puts opts
13
19
  exit
14
20
  end
15
21
 
16
22
  opts.on("-l", "--length [length]") do |value|
17
- @length = value
23
+ raise Password::LengthError, "Invalid length" if value.match(/\D/)
24
+ @length = value.to_i
18
25
  end
19
26
 
20
27
  opts.on("-f", "--words [words file]") do |value|
@@ -30,4 +37,9 @@ rescue OptionParser::ParseError, Password::LengthError => error
30
37
  "(must be between #{Password::MINIMUM} and #{Password::MAXIMUM})."
31
38
  puts parser
32
39
  exit 1
40
+ rescue Password::NoWordsError => error
41
+ warn "haddock: #{error.message}."
42
+ puts "Word lists are available here: http://wordlist.sourceforge.net"
43
+ puts parser
44
+ exit 1
33
45
  end
data/lib/haddock.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # A more memorable password generator. Swordfish? No, I got tired of that. I
2
2
  # changed it.
3
3
  module Haddock
4
- VERSION = '0.0.1'
4
+ VERSION = '0.1.1'
5
5
 
6
6
  module Password
7
7
  MINIMUM = 8
@@ -21,13 +21,11 @@ module Haddock
21
21
  # Password.generate(30) # => "Phlebotomus2473?nonconditioned"
22
22
  # Password.generate(8) # => "amy7@rax"
23
23
  def generate(length = DEFAULT)
24
- @@diction ||= IO.readlines(@@paths.find { |path| File.exist?(path) })
25
-
26
- if !length.is_a?(Integer)
27
- raise LengthError, "Invalid password" if length.match(/\D/)
28
- length = length.to_i
24
+ unless defined? @@diction
25
+ self.diction = @@paths.find { |path| File.exist? path }
29
26
  end
30
27
 
28
+ raise LengthError, "Invalid length" unless length.is_a? Integer
31
29
  raise LengthError, "Password length is too short" if length < MINIMUM
32
30
  raise LengthError, "Password length is too long" if length > MAXIMUM
33
31
 
@@ -41,9 +39,16 @@ module Haddock
41
39
  words.join random_number(length - words_length)
42
40
  end
43
41
 
44
- # Sets the dictionary. Uses "/usr/share/dict/words" otherwise.
42
+ # Sets the dictionary. Uses "/usr/share/dict/words" or
43
+ # "/usr/share/words" otherwise.
44
+ #
45
+ # Password.diction = File.expand_path(__FILE__) + "/my_words.txt"
45
46
  def diction=(path)
46
47
  @@diction = IO.readlines path
48
+ rescue TypeError
49
+ raise NoWordsError, "No words file found"
50
+ rescue Errno::ENOENT
51
+ raise NoWordsError, "No words file at #{path.inspect}"
47
52
  end
48
53
 
49
54
  private
@@ -66,7 +71,11 @@ module Haddock
66
71
  end
67
72
 
68
73
  # Raised if a password is generated with too few or too many characters.
69
- class LengthError < StandardError
74
+ class LengthError < ArgumentError
75
+ end
76
+
77
+ # Raised if no words file is found.
78
+ class NoWordsError < StandardError
70
79
  end
71
80
  end
72
81
  end
data/test/names.txt ADDED
@@ -0,0 +1,2 @@
1
+ Mary
2
+ sturgeon
data/test/test_haddock.rb CHANGED
@@ -21,7 +21,7 @@ class TestHaddock < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  def test_accepts_alternate_wordlist
24
- Password.diction = path = File.dirname(__FILE__) + "/names"
24
+ Password.diction = path = File.dirname(__FILE__) + "/names.txt"
25
25
  pattern = Regexp.new File.read(path).split.join("|")
26
26
  assert_match(pattern, Password.generate(14))
27
27
  ensure
@@ -45,4 +45,16 @@ class TestHaddock < Test::Unit::TestCase
45
45
  Password.generate("invalid")
46
46
  end
47
47
  end
48
+
49
+ def test_fail_on_invalid_path
50
+ assert_raise Password::NoWordsError do
51
+ Password.diction = "invalid/path"
52
+ end
53
+ end
54
+
55
+ def test_fail_on_nil_path
56
+ assert_raise Password::NoWordsError do
57
+ Password.diction = nil
58
+ end
59
+ end
48
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stephencelis-haddock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Celis
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-28 00:00:00 -07:00
12
+ date: 2009-03-29 00:00:00 -07:00
13
13
  default_executable: haddock
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,6 +40,7 @@ files:
40
40
  - Rakefile
41
41
  - bin/haddock
42
42
  - lib/haddock.rb
43
+ - test/names.txt
43
44
  - test/test_haddock.rb
44
45
  has_rdoc: true
45
46
  homepage: http://github.com/stephencelis/haddock