module EasyCaptcha::ControllerHelpers
helper class for ActionController
Public Instance Methods
captcha_cache_path(code)
click to toggle source
return cache path of captcha image
# File lib/easy_captcha/controller_helpers.rb, line 61 def captcha_cache_path(code) "#{EasyCaptcha.cache_temp_dir}/#{code}.png" end
captcha_valid?(code)
click to toggle source
validate given captcha code and re
# File lib/easy_captcha/controller_helpers.rb, line 81 def captcha_valid?(code) return false if session[:captcha].blank? or code.blank? session[:captcha].to_s.upcase == code.to_s.upcase 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 71 def current_captcha_code session[:captcha] end
generate_captcha()
click to toggle source
generate captcha image and return it as blob
# File lib/easy_captcha/controller_helpers.rb, line 12 def generate_captcha if EasyCaptcha.cache # create cache dir FileUtils.mkdir_p(EasyCaptcha.cache_temp_dir) # select all generated captchas from cache files = Dir.glob(EasyCaptcha.cache_temp_dir + "*.png") unless files.size < EasyCaptcha.cache_size file = File.open(files.at(Kernel.rand(files.size))) session[:captcha] = File.basename(file.path, '.*') if file.mtime < EasyCaptcha.cache_expire.ago File.unlink(file.path) # remove speech version File.unlink(file.path.gsub(/png\z/, "wav")) if File.exists?(file.path.gsub(/png\z/, "wav")) else return file.readlines.join end end generated_code = generate_captcha_code image = Captcha.new(generated_code).image # write captcha for caching File.open(captcha_cache_path(generated_code), 'w') { |f| f.write image } # write speech file if u create a new captcha image EasyCaptcha.espeak.generate(generated_code, speech_captcha_cache_path(generated_code)) if EasyCaptcha.espeak? # return image image else Captcha.new(generate_captcha_code).image end end
generate_captcha_code()
click to toggle source
generate captcha code, save in session and return
# File lib/easy_captcha/controller_helpers.rb, line 76 def generate_captcha_code session[:captcha] = EasyCaptcha.length.times.collect { EasyCaptcha.chars[rand(EasyCaptcha.chars.size)] }.join end
generate_speech_captcha()
click to toggle source
generate speech by captcha from session
# File lib/easy_captcha/controller_helpers.rb, line 49 def generate_speech_captcha raise RuntimeError, "espeak disabled" unless EasyCaptcha.espeak? if EasyCaptcha.cache File.read(speech_captcha_cache_path(current_captcha_code)) else wav_file = Tempfile.new("#{current_captcha_code}.wav") EasyCaptcha.espeak.generate(current_captcha_code, wav_file.path) File.read(wav_file.path) end end
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 88 def reset_last_captcha_code! session.delete(:captcha) end
speech_captcha_cache_path(code)
click to toggle source
return cache path of speech captcha
# File lib/easy_captcha/controller_helpers.rb, line 66 def speech_captcha_cache_path(code) "#{EasyCaptcha.cache_temp_dir}/#{code}.wav" end