class SimpleCaptchaReloaded::Middleware
Constants
- DEFAULT_SEND_FILE_OPTIONS
Public Class Methods
new(app, options={})
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 7 def initialize(app, options={}) @app = app end
Public Instance Methods
call(env)
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 11 def call(env) @env = env if env["REQUEST_METHOD"] == "GET" && captcha_path?(env['PATH_INFO']) if request.params.present? && (code = request.params['code']) && code.present? make_image(code) else refresh_code end else @app.call(env) end end
Protected Instance Methods
captcha_path?(request_path)
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 56 def captcha_path?(request_path) request_path.include?(SimpleCaptchaReloaded::Config.captcha_path) end
make_image(code)
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 30 def make_image(code) if !d = SimpleCaptchaReloaded::Data.find_by_key(code) not_found else blob = SimpleCaptchaReloaded::Config.image.generate(d.value) send_data(blob, type: 'image/jpeg', disposition: 'inline', filename: 'simple_captcha.jpg') end end
not_found()
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 67 def not_found [404, {}, []] end
refresh_code()
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 39 def refresh_code code = SimpleCaptchaReloaded::Data.generate_captcha_id(old_key: request.session[:captcha]) request.session[:captcha] = code id = request.params['id'] || 'simple_captcha' url = SimpleCaptchaReloaded::Config.image_url(code, request) body = %Q{ $("##{id} img").attr('src', '#{url}'); $("##{id} input[type=hidden]").attr('value', '#{code}'); } headers = { 'Content-Type' => 'text/javascript; charset=utf-8', "Content-Disposition" => "inline; filename='captcha.js'", "Content-Length" => body.length.to_s } [200, headers, [body]] end
request()
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 26 def request Rack::Request.new(@env) end
send_data(response_body, options = {})
click to toggle source
# File lib/simple_captcha_reloaded/middleware.rb, line 60 def send_data(response_body, options = {}) status = options[:status] || 200 headers = {"Content-Disposition" => "#{options[:disposition]}; filename='#{options[:filename]}'", "Content-Type" => options[:type], 'Content-Transfer-Encoding' => 'binary', 'Cache-Control' => 'private'} [status, headers, [response_body]] end