Module: TextCaptcha::Validation::InstanceMethods

Defined in:
lib/text_captcha/validation.rb

Instance Method Summary collapse

Instance Method Details

#all_challengesObject

Return all questions.



110
111
112
# File 'lib/text_captcha/validation.rb', line 110

def all_challenges
  @all_challenges ||= I18n.t("text_captcha.challenges")
end

#challengeObject

Return the question string.



79
80
81
# File 'lib/text_captcha/validation.rb', line 79

def challenge
  current_challenge.first
end

#challenge_answersObject

Return all accepted answers.



84
85
86
# File 'lib/text_captcha/validation.rb', line 84

def challenge_answers
  current_challenge.last
end

#challenge_idObject

Return the question id. If none is assigned it chooses one randomly.



105
106
107
# File 'lib/text_captcha/validation.rb', line 105

def challenge_id
  @challenge_id ||= Kernel.rand(all_challenges.count)
end

#current_challengeObject

Return an array with the current question and its answers.



74
75
76
# File 'lib/text_captcha/validation.rb', line 74

def current_challenge
  all_challenges[challenge_id.to_i]
end

#encrypted_challenge_idObject

Return an encrypted challenge id. This is useful to add to a form.



90
91
92
93
94
# File 'lib/text_captcha/validation.rb', line 90

def encrypted_challenge_id
  ActiveSupport::MessageEncryptor
    .new(text_captcha_encryption_key)
    .encrypt_and_sign(challenge_id.to_s)
end

#encrypted_challenge_id=(encrypted_challenge_id) ⇒ Object

Assign decrypted challenge id.



97
98
99
100
101
102
# File 'lib/text_captcha/validation.rb', line 97

def encrypted_challenge_id=(encrypted_challenge_id)
  @challenge_id = ActiveSupport::MessageEncryptor
                    .new(text_captcha_encryption_key)
                    .decrypt_and_verify(encrypted_challenge_id.to_s)
                    .to_i
end

#valid_challenge_answer?Boolean

Detect if the answer is correct. Will also return true if TextCaptcha.enabled is set to false.

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'lib/text_captcha/validation.rb', line 116

def valid_challenge_answer?
  return true unless TextCaptcha.enabled
  return false unless current_challenge

  answers = challenge_answers.map {|a| to_captcha(a)}
  !challenge_answer.blank? && answers.include?(to_captcha(challenge_answer))
end