class Rack::AllocationStats::Middleware

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 8
def initialize(app, options = {})
  @app = app
end

Public Instance Methods

allocation_stats_response(body) click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 44
def allocation_stats_response(body)
  [200, headers(body), Array(body)]
end
call(env) click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 12
def call(env)
  @env = env
  action = choose_action
  action.act
  action.response
end
call_app(env) click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 19
def call_app(env)
  @app.call(env)
end
choose_action() click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 23
def choose_action
  request = Rack::Request.new(@env)
  if request.GET["ras"] && request.GET["ras"].has_key?("help")
    @content_type = "text/plain"
    help_text
  elsif request.GET["ras"] && request.GET["ras"]["trace"]
    @content_type = content_type(request.GET["ras"]["output"])
    Tracer.new(@env, self)
  else
    CallAppDirectly.new(@env, self)
  end
end
content_type(output_type) click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 36
def content_type(output_type)
  case output_type
  when "interactive" then "text/html"
  when "json"        then "application/json"
  else                    "text/plain"
  end
end
headers(body) click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 48
def headers(body)
  {
    "Content-Type"   => @content_type,
    "Content-Length" => body.inject(0) { |len, part| len + bytesize(part) }.to_s
  }
end
help_text() click to toggle source
# File lib/rack/allocation_stats/middleware.rb, line 55
def help_text
  app = OpenStruct.new
  app.body = [File.read(File.join(__dir__, "help.txt"))]
  app.response = allocation_stats_response(app.body)
  app
end