Module: EasyCaptcha::ControllerHelpers
- Defined in:
- lib/easy_captcha/controller_helpers.rb
Overview
helper class for ActionController
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#captcha_cache_path(code) ⇒ Object
return cache path of captcha image.
-
#captcha_valid?(code) ⇒ Boolean
(also: #valid_captcha?)
validate given captcha code and re.
-
#current_captcha_code ⇒ Object
current active captcha from session.
-
#generate_captcha ⇒ Object
generate captcha image and return it as blob.
-
#generate_captcha_code ⇒ Object
generate captcha code, save in session and return.
-
#generate_speech_captcha ⇒ Object
generate speech by captcha from session.
-
#reset_last_captcha_code! ⇒ Object
reset the captcha code in session for security after each request.
-
#speech_captcha_cache_path(code) ⇒ Object
return cache path of speech captcha.
Class Method Details
.included(base) ⇒ Object
:nodoc:
5 6 7 8 9 |
# File 'lib/easy_captcha/controller_helpers.rb', line 5 def self.included(base) #:nodoc: base.class_eval do helper_method :valid_captcha?, :captcha_valid? end end |
Instance Method Details
#captcha_cache_path(code) ⇒ Object
return cache path of captcha image
61 62 63 |
# File 'lib/easy_captcha/controller_helpers.rb', line 61 def captcha_cache_path(code) "#{EasyCaptcha.cache_temp_dir}/#{code}.png" end |
#captcha_valid?(code) ⇒ Boolean Also known as: valid_captcha?
validate given captcha code and re
81 82 83 84 |
# 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 |
#current_captcha_code ⇒ Object
current active captcha from session
71 72 73 |
# File 'lib/easy_captcha/controller_helpers.rb', line 71 def current_captcha_code session[:captcha] end |
#generate_captcha ⇒ Object
generate captcha image and return it as blob
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 ⇒ Object
generate captcha code, save in session and return
76 77 78 |
# 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 ⇒ Object
generate speech by captcha from session
49 50 51 52 53 54 55 56 57 58 |
# 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! ⇒ Object
reset the captcha code in session for security after each request
88 89 90 |
# File 'lib/easy_captcha/controller_helpers.rb', line 88 def reset_last_captcha_code! session.delete(:captcha) end |
#speech_captcha_cache_path(code) ⇒ Object
return cache path of speech captcha
66 67 68 |
# File 'lib/easy_captcha/controller_helpers.rb', line 66 def speech_captcha_cache_path(code) "#{EasyCaptcha.cache_temp_dir}/#{code}.wav" end |