Class: Rack::ForceStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/force-status/version.rb,
lib/rack/force-status/middleware.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ForceStatus.



3
4
5
6
7
# File 'lib/rack/force-status/middleware.rb', line 3

def initialize(app, options={})
  @app = app
  @param = options[:param] || 'force_status'
  @header = options[:header] || 'X-Original-Status-Code'
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack/force-status/middleware.rb', line 9

def call(env)
  request = Rack::Request.new(env)
  force_status = request.params.delete(@param).to_i
  
  status, headers, body = @app.call(env)
  
  if force_status > 0 && status != force_status
    headers[@header] = status.to_s
    status = force_status
  end
  
  [status, headers, body]
end