Class: Forgery::Basic
Constant Summary
collapse
- HEX_DIGITS =
Forgery::Extend(%w{0 1 2 3 4 5 6 7 8 9 a b c d e f})
- UPPER_ALPHA =
Forgery::Extend(('A'..'Z').to_a)
- LOWER_ALPHA =
Forgery::Extend(('a'..'z').to_a)
- NUMERIC =
Forgery::Extend(('0'..'9').to_a)
- SPECIAL_CHARACTERS =
Forgery::Extend(%w{! ' @ # $ % ^ & * ( ) _ + - = [ ] { } ; : " , . / ?})
- BOOLEAN =
Forgery::Extend([true, false])
Constants inherited
from Forgery
VERSION
Class Method Summary
collapse
Methods inherited from Forgery
Extend, dictionaries, formats, load_from!, load_paths, rails?, rails_root
Class Method Details
.boolean ⇒ Object
Gets a random boolean value
Forgery(:basic).boolean
Forgery(:basic).boolean
64
65
66
|
# File 'lib/forgery/forgery/basic.rb', line 64
def self.boolean
BOOLEAN.random
end
|
.color ⇒ Object
68
69
70
|
# File 'lib/forgery/forgery/basic.rb', line 68
def self.color
dictionaries[:colors].random.unextend
end
|
.encrypt(password = "password", salt = rand) ⇒ Object
SHA1 hexdigests a password salted with the current time
Forgery(:basic).encrypt
Forgery(:basic).encrypt('your-password')
Forgery(:basic).encrypt('your-password', Time.utc(2009))
53
54
55
|
# File 'lib/forgery/forgery/basic.rb', line 53
def self.encrypt(password="password", salt=rand)
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end
|
.frequency ⇒ Object
108
109
110
|
# File 'lib/forgery/forgery/basic.rb', line 108
def self.frequency
dictionaries[:frequencies].random.unextend
end
|
.hex_color ⇒ Object
72
73
74
75
|
# File 'lib/forgery/forgery/basic.rb', line 72
def self.hex_color
hex_digits = (1..6).collect { HEX_DIGITS.random.unextend}
"##{hex_digits.join}"
end
|
.number(options = {}) ⇒ Object
81
82
83
84
85
86
|
# File 'lib/forgery/forgery/basic.rb', line 81
def self.number(options={})
options = {:at_least => 1,
:at_most => 10}.merge(options)
Forgery::Extend(options[:at_least]..options[:at_most]).random
end
|
.password(options = {}) ⇒ Object
Gets a random string for use as a password
Forgery(:basic).password
Forgery(:basic).password(:at_least => 3, :at_most => 5)
Supported Options
[:at_least]
The minimum length the password may be. Defaults to 6.
[:at_most]
The maximum length the password may be. Defaults to 12.
[:allow_lower]
Allow lowercase characters. Defaults to true.
[:allow_upper]
Allow uppercase characters. Defaults to true.
[:allow_numeric]
Allow numbers. Defaults to true.
[:allow_special]
Allow special characters. Defaults to false.
33
34
35
36
37
38
39
40
41
|
# File 'lib/forgery/forgery/basic.rb', line 33
def self.password(options={})
options = {:at_least => 6,
:at_most => 12,
:allow_lower => true,
:allow_upper => true,
:allow_numeric => true,
:allow_special => false}.merge!(options)
text(options)
end
|
.short_hex_color ⇒ Object
77
78
79
|
# File 'lib/forgery/forgery/basic.rb', line 77
def self.short_hex_color
hex_color[0,4]
end
|
.text(options = {}) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/forgery/forgery/basic.rb', line 88
def self.text(options={})
options = {:at_least => 10,
:at_most => 15,
:allow_lower => true,
:allow_upper => true,
:allow_numeric => true,
:allow_special => false,
:exactly => nil}.merge!(options)
allowed_characters = []
allowed_characters += LOWER_ALPHA if options[:allow_lower]
allowed_characters += UPPER_ALPHA if options[:allow_upper]
allowed_characters += NUMERIC if options[:allow_numeric]
allowed_characters += SPECIAL_CHARACTERS if options[:allow_special]
length = options[:exactly] || Forgery::Extend(options[:at_least]..options[:at_most]).random
Forgery::Extend(allowed_characters).random_subset(length).join
end
|