Class: BasicForgery

Inherits:
Forgery show all
Defined in:
lib/forgeries/basic_forgery.rb

Constant Summary collapse

HEX_DIGITS =
%w{0 1 2 3 4 5 6 7 8 9 a b c d e f}
UPPER_ALPHA =
('A'..'Z').to_a
LOWER_ALPHA =
('a'..'z').to_a
NUMERIC =
('0'..'9').to_a
SPECIAL_CHARACTERS =
%w{! ' @ # $ % ^ & * ( ) _ + - = [ ] { } ; : " , . / ?}
BOOLEAN =
[true, false]

Class Method Summary collapse

Methods inherited from Forgery

dictionaries, formats

Class Method Details

.booleanObject



25
26
27
# File 'lib/forgeries/basic_forgery.rb', line 25

def self.boolean
  BOOLEAN.random
end

.colorObject



29
30
31
# File 'lib/forgeries/basic_forgery.rb', line 29

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

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



21
22
23
# File 'lib/forgeries/basic_forgery.rb', line 21

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

.frequencyObject



68
69
70
# File 'lib/forgeries/basic_forgery.rb', line 68

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

.hex_colorObject



33
34
35
36
# File 'lib/forgeries/basic_forgery.rb', line 33

def self.hex_color
  hex_digits = (1..6).collect { HEX_DIGITS.random}
  "##{hex_digits.join}"
end

.number(options = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/forgeries/basic_forgery.rb', line 42

def self.number(options={})
  options = {:at_least => 1,
             :at_most => 10}.merge(options)

  (options[:at_least]..options[:at_most]).random
end

.password(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/forgeries/basic_forgery.rb', line 11

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)
  self.text(options)
end

.short_hex_colorObject



38
39
40
# File 'lib/forgeries/basic_forgery.rb', line 38

def self.short_hex_color
  hex_color[0,4]
end

.text(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/forgeries/basic_forgery.rb', line 49

def self.text(options={})
  options = {:at_least => 10,
             :at_most => 15,
             :allow_lower => true,
             :allow_upper => true,
             :allow_numeric => true,
             :allow_special => false}.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[:at_least]..options[:at_most]).random

  allowed_characters.random_subset(length).join
end