Module: EasyCaptcha::ControllerHelpers

Defined in:
lib/easy_captcha/controller_helpers.rb

Overview

rubocop:disable Metrics/ModuleLength helper class for ActionController

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



7
8
9
10
11
# File 'lib/easy_captcha/controller_helpers.rb', line 7

def self.included(base) #:nodoc:
  base.class_eval do
    helper_method :valid_captcha?, :captcha_valid?
  end
end

Instance Method Details

#captcha_cache_pathObject

return cache path of captcha image



35
36
37
# File 'lib/easy_captcha/controller_helpers.rb', line 35

def captcha_cache_path
  "#{EasyCaptcha.cache_temp_dir}/#{current_captcha_code}.png"
end

#captcha_invalid?(code) ⇒ Boolean Also known as: invalid_captcha?

Returns:

  • (Boolean)


75
76
77
# File 'lib/easy_captcha/controller_helpers.rb', line 75

def captcha_invalid?(code)
  !captcha_valid?(code)
end

#captcha_valid?(code) ⇒ Boolean Also known as: valid_captcha?

validate given captcha code

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/easy_captcha/controller_helpers.rb', line 69

def captcha_valid?(code)
  return false if session[:captcha].to_s.blank? || code.to_s.blank?
  session[:captcha].to_s == code.to_s
end

#current_captcha_codeObject

current active captcha from session



45
46
47
# File 'lib/easy_captcha/controller_helpers.rb', line 45

def current_captcha_code
  session[:captcha] ||= generate_captcha_code
end

#generate_captchaObject

generate captcha image and return it as blob



14
15
16
17
18
19
20
21
22
# File 'lib/easy_captcha/controller_helpers.rb', line 14

def generate_captcha
  Rails.logger.info("#{Time.now}: generate_captcha in EasyCaptcha. params: #{params}.")
  # create image
  image = generate_captcha_image
  # cache image
  cache_image(image) if EasyCaptcha.cache
  # return image
  image
end

#generate_captcha_codeObject

generate captcha code, save in session and return rubocop:disable Metrics/AbcSize, Naming/MemoizedInstanceVariableName



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/easy_captcha/controller_helpers.rb', line 51

def generate_captcha_code
  @captcha_code ||= begin
    length = EasyCaptcha.captcha_code_length
    # overwrite `current_captcha_code`
    session[:captcha] = Array.new(length) { EasyCaptcha.captcha_character_pool.sample }.join
    Rails.logger.info(
      "#{Time.now}: generate_captcha_code in EasyCaptcha. " \
      "session[:captcha]: #{session[:captcha]} " \
      "length: #{length}, " \
      "original length: #{EasyCaptcha.captcha_character_count} " \
      "chars count: #{EasyCaptcha.captcha_character_pool.size}."
    )
    session[:captcha]
  end
end

#generate_speech_captchaObject

generate speech by captcha from session



25
26
27
28
29
30
31
32
# File 'lib/easy_captcha/controller_helpers.rb', line 25

def generate_speech_captcha
  fail 'espeak disabled' unless EasyCaptcha.espeak?
  if EasyCaptcha.cache && cache_audio_file_exists?
    load_cache_audio_file
  else
    new_audio_captcha_file
  end
end

#reset_last_captcha_code!Object

reset the captcha code in session for security after each request



81
82
83
# File 'lib/easy_captcha/controller_helpers.rb', line 81

def reset_last_captcha_code!
  session.delete(:captcha)
end

#speech_captcha_cache_pathObject

return cache path of speech captcha



40
41
42
# File 'lib/easy_captcha/controller_helpers.rb', line 40

def speech_captcha_cache_path
  "#{EasyCaptcha.cache_temp_dir}/#{current_captcha_code}.wav"
end