Class: Captcha
- Inherits:
-
Object
- Object
- Captcha
- Defined in:
- app/models/captcha.rb
Constant Summary collapse
- OPERATORS =
['+','-','*','/','minute','hour','day','week','month','year'].shuffle
- MAX_OP =
4
- MIN_OP =
4
Instance Attribute Summary collapse
-
#color_code ⇒ Object
Returns the value of attribute color_code.
-
#pointsize ⇒ Object
Returns the value of attribute pointsize.
Class Method Summary collapse
Instance Method Summary collapse
- #generate_captcha(text_on_captcha = 'default') ⇒ Object
- #generate_question_answer ⇒ Object
-
#initialize(pointsize = 25, color_code = 'ffffff') ⇒ Captcha
constructor
A new instance of Captcha.
Constructor Details
#initialize(pointsize = 25, color_code = 'ffffff') ⇒ Captcha
Returns a new instance of Captcha.
9 10 11 12 13 14 |
# File 'app/models/captcha.rb', line 9 def initialize(pointsize=25,color_code='ffffff') self.pointsize = pointsize.to_i self.pointsize = 25 if self.pointsize.to_i == 0 self.color_code = color_code self.color_code = 'ffffff' if !self.color_code.present? end |
Instance Attribute Details
#color_code ⇒ Object
Returns the value of attribute color_code.
5 6 7 |
# File 'app/models/captcha.rb', line 5 def color_code @color_code end |
#pointsize ⇒ Object
Returns the value of attribute pointsize.
5 6 7 |
# File 'app/models/captcha.rb', line 5 def pointsize @pointsize end |
Class Method Details
.valid?(session, params) ⇒ Boolean
72 73 74 |
# File 'app/models/captcha.rb', line 72 def self.valid?(session,params) return params[:iq_captcha_result] && (session[:iq_captcha_answer].to_i == params[:iq_captcha_result].to_i) end |
Instance Method Details
#generate_captcha(text_on_captcha = 'default') ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/models/captcha.rb', line 16 def generate_captcha(text_on_captcha='default') color_code = self.color_code pointsize = self.pointsize img = ImageList.new("public/iq_captcha_background.png") txt = Draw.new img.annotate(txt, 0,0,0,0, "#{text_on_captcha}"){ txt.gravity = Magick::SouthGravity txt.pointsize = pointsize txt.stroke = '#000000' txt.fill = '#' + color_code txt.font_weight = Magick::BoldWeight } img.format = 'jpeg' return img end |
#generate_question_answer ⇒ Object
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 |
# File 'app/models/captcha.rb', line 32 def generate_question_answer op1 = rand(Captcha::MAX_OP) op2 = 1 + rand(Captcha::MIN_OP) operator = Captcha::OPERATORS[rand(Captcha::OPERATORS.size)] case operator when '+','-','*','/' answer = eval("#{op1}#{operator}#{op2}") question = "#{op1}#{operator}#{op2} = ?" when 'minute' op1 = 1 if op1 == 0 answer = eval("#{op1.minute/1.second}") question = "#{op1} minutes = ? seconds" when 'hour' op1 = 1 if op1 == 0 answer = eval("#{op1.hour/1.minute}") question = "#{op1} hours = ? minutes" when 'day' op1 = 1 if op1 == 0 answer = eval("#{op1.day/1.hour}") question = "#{op1} days = ? hours" when 'week' op1 = 1 if op1 == 0 answer = eval("#{op1.week/1.day}") question = "#{op1} weeks = ? days" when 'month' op1 = 1 if op1 == 0 answer = eval("#{op1.month/1.day}") question = "#{op1} months = ? days" when 'year' op1 = 1 if op1 == 0 answer = eval("#{op1.year/1.month}").to_i question = "#{op1} years = ? months" else answer = eval("#{op1}+#{op2}") question = "#{op1}+#{op2} = ?" end return question, answer end |