Class: UserSwitcher::Middlewares::SwitcherInserter

Inherits:
Object
  • Object
show all
Defined in:
lib/user_switcher/middlewares/switcher_inserter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, config = {}) ⇒ SwitcherInserter

Returns a new instance of SwitcherInserter.



7
8
9
10
11
12
# File 'lib/user_switcher/middlewares/switcher_inserter.rb', line 7

def initialize(app, config = {})
  @app = app

  @users = config[:users] || []
  @login_url = config[:login_url]
end

Instance Attribute Details

#login_urlObject (readonly)

Returns the value of attribute login_url.



5
6
7
# File 'lib/user_switcher/middlewares/switcher_inserter.rb', line 5

def 
  @login_url
end

#usersObject (readonly)

Returns the value of attribute users.



5
6
7
# File 'lib/user_switcher/middlewares/switcher_inserter.rb', line 5

def users
  @users
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/user_switcher/middlewares/switcher_inserter.rb', line 14

def call(env)
  status, headers, body = @app.call(env)

  content_type = headers['Content-Type'].to_s
  redirect = 300 <= status.to_i && status.to_i < 400

  if content_type.strip.start_with?("text/html") && !redirect
    case body
    when ActionDispatch::Response, ActionDispatch::Response::RackBody
      body = body.body
    when Array
      body = body[0]
    end
    body = body.dup if body.frozen?

    new_body = insert_form(body)
    new_body = insert_inner_head(new_body)
    headers['Content-Length'] &&= new_body.bytesize.to_s

    [status, headers, [new_body]]
  else
    [status, headers, body]
  end
end