module EasyCaptcha::ControllerHelpers

rubocop:disable Metrics/ModuleLength helper class for ActionController

Public Instance Methods

captcha_cache_path() click to toggle source

return cache path of captcha image

# 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) click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 75
def captcha_invalid?(code)
  !captcha_valid?(code)
end
Also aliased as: invalid_captcha?
captcha_valid?(code) click to toggle source

validate given captcha code

# 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
Also aliased as: valid_captcha?
current_captcha_code() click to toggle source

current active captcha from session

# File lib/easy_captcha/controller_helpers.rb, line 45
def current_captcha_code
  session[:captcha] ||= generate_captcha_code
end
generate_captcha() click to toggle source

generate captcha image and return it as blob

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

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

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

generate speech by captcha from session

# 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
invalid_captcha?(code)
Alias for: captcha_invalid?
reset_last_captcha_code!() click to toggle source

reset the captcha code in session for security after each request

# File lib/easy_captcha/controller_helpers.rb, line 81
def reset_last_captcha_code!
  session.delete(:captcha)
end
speech_captcha_cache_path() click to toggle source

return cache path of speech captcha

# File lib/easy_captcha/controller_helpers.rb, line 40
def speech_captcha_cache_path
  "#{EasyCaptcha.cache_temp_dir}/#{current_captcha_code}.wav"
end
valid_captcha?(code)
Alias for: captcha_valid?

Private Instance Methods

cache_audio_file_exists?() click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 154
def cache_audio_file_exists?
  File.exist?(speech_captcha_cache_path)
end
cache_image(image) click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 127
def cache_image(image)
  code = current_captcha_code
  # write captcha for caching
  File.open(captcha_cache_path(code), 'w') { |f| f.write image }
  # write speech file if u create a new captcha image
  EasyCaptcha.espeak.generate(code, speech_captcha_cache_path(code)) if EasyCaptcha.espeak?
end
cached_captcha_files() click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 92
def cached_captcha_files
  ensure_cache_dir
  # select all generated captcha image files from cache
  Dir.glob("#{EasyCaptcha.cache_temp_dir}*.png")
end
chose_image_from_cache() click to toggle source

Grab random file from the cached files, return its file data rubocop:disable Metrics/AbcSize

# File lib/easy_captcha/controller_helpers.rb, line 100
def chose_image_from_cache
  cache_files = cached_captcha_files

  return nil if cache_files.size < EasyCaptcha.cache_size

  file              = File.open(cache_files.samlpe)
  session[:captcha] = File.basename(file.path, '.*')

  if file.mtime < EasyCaptcha.cache_expire.ago
    # remove expired cached image
    remove_cached_image_file(file)
    # return nil
    session[:captcha] = nil
  else
    # return file data from cache
    file.readlines.join
  end
end
ensure_cache_dir() click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 87
def ensure_cache_dir
  # create cache dir
  FileUtils.mkdir_p(EasyCaptcha.cache_temp_dir)
end
generate_captcha_image() click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 135
def generate_captcha_image
  if EasyCaptcha.cache
    image = chose_image_from_cache
    return image if image
  end
  # Default, if cache not enabled or an expired cache image was chosen, make a new one
  Captcha.new(current_captcha_code).image
end
load_cache_audio_file() click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 144
def load_cache_audio_file
  File.read(speech_captcha_cache_path)
end
new_audio_captcha_file() click to toggle source
# File lib/easy_captcha/controller_helpers.rb, line 148
def new_audio_captcha_file
  wav_file = Tempfile.new("#{current_captcha_code}.wav")
  EasyCaptcha.espeak.generate(current_captcha_code, wav_file.path)
  File.read(wav_file.path)
end
remove_cached_image_file(file) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/easy_captcha/controller_helpers.rb, line 120
def remove_cached_image_file(file)
  File.unlink(file.path)
  # remove speech version
  audio_file = file.path.gsub(/png\z/, 'wav')
  File.unlink(audio_file) if File.exist?(audio_file)
end