Class: Unity::Middleware::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/unity/middleware/response.rb

Constant Summary collapse

HEADER =
'HTTP_X_UNITY_RESPONSE'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Response

Returns a new instance of Response.



9
10
11
# File 'lib/unity/middleware/response.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/unity/middleware/response.rb', line 13

def call(env)
  status, headers, response = @app.call(env)
  return [status, headers, response] unless bool env[HEADER]

  body = response.to_enum.reduce(:+)
  unless headers.fetch('Content-Type', '').start_with?('application/json')
    body = JSON.dump body
  end

  response = [%Q({"status":#{status},"headers":#{JSON.dump(headers)},"body":#{body}})]
  return [200, { 'Content-Type' => 'application/json' }, response]
end