Class: Wants::ValidateAcceptsMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/wants/validate_accepts_middleware.rb

Constant Summary collapse

DEFAULT_ON_NOT_ACCEPTABLE =
lambda do |env|
  [
    406,
    {
      'Content-Type' => 'text/plain',
      'Content-Length' => '14'
    },
    [ 'Not Acceptable' ]
  ]
end

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ ValidateAcceptsMiddleware

Returns a new instance of ValidateAcceptsMiddleware.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
# File 'lib/wants/validate_accepts_middleware.rb', line 17

def initialize(app, options)
  @app = app

  @mime_types = options[:mime_types]
  raise ArgumentError.new("#{self.class} requires option :mime_types") unless @mime_types

  @on_not_acceptable = options[:on_not_acceptable] || DEFAULT_ON_NOT_ACCEPTABLE
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/wants/validate_accepts_middleware.rb', line 26

def call(env)
  if MatchResult.new(env['HTTP_ACCEPT'], @mime_types).present?
    @app.call(env)
  else
    @on_not_acceptable.call(env)
  end
end