Class: Rack::ExpectationCascade

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/expectation_cascade.rb

Constant Summary collapse

Expect =
"HTTP_EXPECT".freeze
ContinueExpectation =
"100-continue".freeze
ExpectationFailed =
[417, {"content-type" => "text/html"}, []]
NotFound =
[404, {"content-type" => "text/html"}, []]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ ExpectationCascade

Returns a new instance of ExpectationCascade.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
# File 'lib/rack/contrib/expectation_cascade.rb', line 13

def initialize
  @apps = []
  yield self if block_given?
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



11
12
13
# File 'lib/rack/contrib/expectation_cascade.rb', line 11

def apps
  @apps
end

Instance Method Details

#<<(app) ⇒ Object



30
31
32
# File 'lib/rack/contrib/expectation_cascade.rb', line 30

def <<(app)
  @apps << app
end

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/contrib/expectation_cascade.rb', line 18

def call(env)
  set_expectation = env[Expect] != ContinueExpectation
  env[Expect] = ContinueExpectation if set_expectation
  @apps.each do |app|
    result = app.call(env)
    return result unless result[0].to_i == 417
  end
  set_expectation ? NotFound : ExpectationFailed
ensure
  env.delete(Expect) if set_expectation
end