Class: SimpleCaptcha::Middleware

Inherits:
Object
  • Object
show all
Includes:
ImageHelpers, ViewHelper
Defined in:
lib/simple_captcha/middleware.rb

Constant Summary collapse

DEFAULT_SEND_FILE_OPTIONS =
{
  :type         => 'application/octet-stream'.freeze,
  :disposition  => 'attachment'.freeze,
}.freeze
REFRESH_FORMATS =
{
  :jquery       => %Q{
      $("#%{id}").attr('src', '%{url}');
      $("#%{captcha_hidden_field_id}").attr('value', '%{key}');
  }.freeze,
  :plain_javascript => %Q{
      var img = document.getElementById("%{id}");
      if (img != null) {
        img.src = "%{url}";
      }
      var hidden = document.getElementById("%{captcha_hidden_field_id}");
      if (hidden != null) {
        hidden.value = "%{key}";
      }
  }.freeze,
  :prototype    => %Q{
      $("%{id}").setAttribute('src', '%{url}');
      $("%{captcha_hidden_field_id}").setAttribute('value', '%{key}');
  }.freeze,
}.freeze

Constants included from ImageHelpers

ImageHelpers::DEFAULT_IMPLODE, ImageHelpers::DISTORTIONS, ImageHelpers::IMPLODES

Instance Method Summary collapse

Methods included from ViewHelper

#show_simple_captcha, #simple_captcha_options

Methods included from ImageHelpers

distortion, image_params, implode

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.


33
34
35
36
# File 'lib/simple_captcha/middleware.rb', line 33

def initialize(app, options={})
  @app = app
  self
end

Instance Method Details

#call(env) ⇒ Object

:nodoc:


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/simple_captcha/middleware.rb', line 38

def call(env) # :nodoc:
  if env["REQUEST_METHOD"] == "GET" && captcha_path?(env['PATH_INFO'])
    request = Rack::Request.new(env)
    if request.params.present? && request.params['code'].present?
      make_image(env)
    else
      refresh_code(env)
    end
  else
    @app.call(env)
  end
end