class Rack::AcceptHeaders::Context

Implements the Rack middleware interface.

Attributes

app[R]

Public Class Methods

new(app) { |self| ... } click to toggle source
# File lib/rack/accept_headers/context.rb, line 10
def initialize(app)
  @app = app
  @checks = {}
  @check_headers = []
  yield self if block_given?
end

Public Instance Methods

call(env) click to toggle source

Inserts a new Rack::AcceptHeaders::Request object into the environment before handing the request to the app immediately downstream.

# File lib/rack/accept_headers/context.rb, line 19
def call(env)
  request = env['rack-accept_headers.request'] ||= Request.new(env)
  check!(request) unless @checks.empty?
  @app.call(env)
rescue AcceptError
  response = Response.new
  response.not_acceptable!
  response.finish
end
charsets=(charsets) click to toggle source

Defines the character sets this server is able to serve.

# File lib/rack/accept_headers/context.rb, line 35
def charsets=(charsets)
  add_check(:charset, charsets)
end
encodings=(encodings) click to toggle source

Defines the types of encodings this server is able to serve.

# File lib/rack/accept_headers/context.rb, line 40
def encodings=(encodings)
  add_check(:encoding, encodings)
end
languages=(languages) click to toggle source

Defines the languages this server is able to serve.

# File lib/rack/accept_headers/context.rb, line 45
def languages=(languages)
  add_check(:language, languages)
end
media_types=(media_types) click to toggle source

Defines the types of media this server is able to serve.

# File lib/rack/accept_headers/context.rb, line 30
def media_types=(media_types)
  add_check(:media_type, media_types)
end

Private Instance Methods

add_check(header_name, values) click to toggle source
# File lib/rack/accept_headers/context.rb, line 51
def add_check(header_name, values)
  @checks[header_name] ||= []
  @checks[header_name].concat(values)
  @check_headers << header_name unless @check_headers.include?(header_name)
end
check!(request) click to toggle source

Raises an AcceptError if this server is not able to serve an acceptable response.

# File lib/rack/accept_headers/context.rb, line 59
def check!(request)
  @check_headers.each do |header_name|
    values = @checks[header_name]
    header = request.send(header_name)
    raise AcceptError unless values.any? {|v| header.accept?(v) }
  end
end