Class: MathCaptcha::Captcha

Inherits:
Object
  • Object
show all
Defined in:
lib/math_captcha.rb

Instance Method Summary collapse

Constructor Details

#initializeCaptcha

Returns a new instance of Captcha.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/math_captcha.rb', line 22

def	initialize
  @numbers = [0,1,2,3,4,5,6,7,8,9]
  @numbers_alpha = {
    'es' => {
	  0 => 'cero',
	  1 => 'uno', 
	  2 => 'dos',
	  3 => 'tres',
	  4 => 'cuatro',
	  5 => 'cinco',
	  6 => 'seis',
	  7 => 'siete',
	  8 => 'ocho', 
	  9 => 'nueve'
    },
	'eu' => {
	  0 => 'huts',
1 => 'bat',
2 => 'bi',
3 => 'hiru',
4 => 'lau',
	  5 => 'bost',
	  6 => 'sei',
	  7 => 'zazpi',
8 => 'zortzi',
	  9 => 'bederatzi'	  
	},
	'en' => {
	  0 => 'zero',
	  1 => 'one',
	  2 => 'two',
	  3 => 'three',
	  4 => 'four',
	  5 => 'five',
	  6 => 'six',
	  7 => 'seven',
	  8 => 'eight',
	  9 => 'nine'
	}
  }

  # temporary remove division operation

  @ops = {
  'es' => {
	  '+' => 'más',
	  '-' => 'menos',
	  '*' => 'por'
    },
	'eu' => {
	  '+' => 'gehi',
	  '-' => 'ken',
	  '*' => 'bider'
	},
	'en' => {
	  '+' => 'plus',
	  '-' => 'minus',
	  '*' => 'times'
	}
  }
end

Instance Method Details

#build_challenge(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/math_captcha.rb', line 89

def	build_challenge(options={})
  # loop to prevent negative numbers
  begin 
    digit1 = @numbers[rand(9)]
    digit2 = @numbers[rand(9)]
    op = @ops[locale].keys[rand(3)]
  
    value = eval("#{digit1}#{op}#{digit2}")
  end while value < 0
  
  encrypted = MathCaptcha::Utils.generate_key(value)
  question = "#{@numbers_alpha[locale][digit1]} #{@ops[locale][op]} #{@numbers_alpha[locale][digit2]}"
  challenge = Challenge.new(question, value, encrypted, options)

  return challenge
end

#localeObject



84
85
86
87
# File 'lib/math_captcha.rb', line 84

def locale
  return @locale unless @locale.nil?    
  @locale = I18n.locale.to_s || I18n.default_locale.to_s || 'es'
end