class Unity::Middleware::Response

Constants

Public Class Methods

new(app) click to toggle source
# File lib/unity/middleware/response.rb, line 9
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# 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

Private Instance Methods

bool(string) click to toggle source
# File lib/unity/middleware/response.rb, line 28
def bool(string)
  string && string =~ /^(true|1)$/i
end