Class: Forgery::Basic

Inherits:
Forgery show all
Defined in:
lib/forgery/forgery/basic.rb

Constant Summary

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

.booleanObject

Gets a random boolean value

Forgery(:basic).boolean
# => true

Forgery(:basic).boolean
# => false


64
65
66
# File 'lib/forgery/forgery/basic.rb', line 64

def self.boolean
  BOOLEAN.random
end

.colorObject



68
69
70
# File 'lib/forgery/forgery/basic.rb', line 68

def self.color
  dictionaries[:colors].random.unextend
end

.encrypt(password = "password", salt = ::Time.now.to_s) ⇒ Object

SHA1 hexdigests a password salted with the current time

Forgery(:basic).encrypt
# => "b2fbd3955a36068e93e0b9db45bcb178f08336f5"

Forgery(:basic).encrypt('your-password')
# => "00932bafce4a9391f888ca77f81f98b8e89d3aa6"

Forgery(:basic).encrypt('your-password', Time.utc(2009))
# => "4b157c2fbf430b962842d21926eaa887c3a12f81"


53
54
55
# File 'lib/forgery/forgery/basic.rb', line 53

def self.encrypt(password="password", salt=::Time.now.to_s)
  Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end

.frequencyObject



108
109
110
# File 'lib/forgery/forgery/basic.rb', line 108

def self.frequency
  dictionaries[:frequencies].random.unextend
end

.hex_colorObject



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
# => "1MbyMQhU"

Forgery(:basic).password(:at_least => 3, :at_most => 5)
# => "WbgP"

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_colorObject



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