module TextCaptcha::Validation::InstanceMethods

Public Instance Methods

all_challenges() click to toggle source

Return all questions.

# File lib/text_captcha/validation.rb, line 110
def all_challenges
  @all_challenges ||= I18n.t("text_captcha.challenges")
end
challenge() click to toggle source

Return the question string.

# File lib/text_captcha/validation.rb, line 79
def challenge
  current_challenge.first
end
challenge_answers() click to toggle source

Return all accepted answers.

# File lib/text_captcha/validation.rb, line 84
def challenge_answers
  current_challenge.last
end
challenge_id() click to toggle source

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

# File lib/text_captcha/validation.rb, line 105
def challenge_id
  @challenge_id ||= Kernel.rand(all_challenges.count)
end
current_challenge() click to toggle source

Return an array with the current question and its answers.

# File lib/text_captcha/validation.rb, line 74
def current_challenge
  all_challenges[challenge_id.to_i]
end
encrypted_challenge_id() click to toggle source

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

# 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) click to toggle source

Assign decrypted challenge id.

# 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?() click to toggle source

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

# 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

Private Instance Methods

check_challenge_answer() click to toggle source

Check if the answer is correct. Add an error to <tt><:challenge_answer/tt> attribute otherwise.

# File lib/text_captcha/validation.rb, line 128
def check_challenge_answer
  unless valid_challenge_answer?
    errors.add(:challenge_answer, :invalid_challenge_answer)
  end
end
text_captcha_encryption_key() click to toggle source

Return the encryption key.

# File lib/text_captcha/validation.rb, line 140
def text_captcha_encryption_key
  return TextCaptcha.encryption_key if TextCaptcha.encryption_key
  raise ArgumentError, "no TextCaptcha.encryption_key defined"
end
to_captcha(str) click to toggle source

Normalize the strings for comparison.

# File lib/text_captcha/validation.rb, line 135
def to_captcha(str)
  str.to_s.squish.downcase
end