Class: Rack::Unbasic

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

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ Unbasic

Create the middleware. You can pass a block to configure how to handle 401 and 400 responses from the downstream app. See #unauthorized and #bad_request.



8
9
10
11
12
# File 'lib/rack/unbasic.rb', line 8

def initialize(app, &block) # :yields: self
  @app = app
  @locations = {}
  block.call(self) if block
end

Instance Method Details

#bad_request(location) ⇒ Object

Set the redirect URL for 400 responses from the downstream app.



20
21
22
# File 'lib/rack/unbasic.rb', line 20

def bad_request(location)
  @locations["bad_request"] = location
end

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/unbasic.rb', line 24

def call(env) 
  @env = env

  clean_session_data

  authorize_from_params || authorize_from_session

  @response = @app.call(@env)

  case status_code
  when 401
    unauthorized_response
  when 400
    bad_request_response
  else
    store_credentials
    @response
  end
end

#unauthorized(location) ⇒ Object

Set the redirect URL for 401 responses from the downstream app.



15
16
17
# File 'lib/rack/unbasic.rb', line 15

def unauthorized(location)
  @locations["unauthorized"] = location
end