class Rack::Accept::Context

Implements the Rack middleware interface.

Attributes

app[R]

Public Class Methods

new(app) { |self| ... } click to toggle source
   # File lib/rack/accept/context.rb
10 def initialize(app)
11   @app = app
12   @checks = {}
13   @check_headers = []
14   yield self if block_given?
15 end

Public Instance Methods

call(env) click to toggle source

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

   # File lib/rack/accept/context.rb
19 def call(env)
20   request = env['rack-accept.request'] ||= Request.new(env)
21   check!(request) unless @checks.empty?
22   @app.call(env)
23 rescue AcceptError
24   response = Response.new
25   response.not_acceptable!
26   response.finish
27 end
charsets=(charsets) click to toggle source

Defines the character sets this server is able to serve.

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

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

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

Defines the languages this server is able to serve.

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

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

   # File lib/rack/accept/context.rb
30 def media_types=(media_types)
31   add_check(:media_type, media_types)
32 end

Private Instance Methods

add_check(header_name, values) click to toggle source
   # File lib/rack/accept/context.rb
51 def add_check(header_name, values)
52   @checks[header_name] ||= []
53   @checks[header_name].concat(values)
54   @check_headers << header_name unless @check_headers.include?(header_name)
55 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/context.rb
59 def check!(request)
60   @check_headers.each do |header_name|
61     values = @checks[header_name]
62     header = request.send(header_name)
63     raise AcceptError unless values.any? {|v| header.accept?(v) }
64   end
65 end