Class: Rack::AllocationStats::Middleware

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/allocation_stats/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
# File 'lib/rack/allocation_stats/middleware.rb', line 8

def initialize(app, options = {})
  @app = app
end

Instance Method Details

#allocation_stats_response(body) ⇒ Object



44
45
46
# File 'lib/rack/allocation_stats/middleware.rb', line 44

def allocation_stats_response(body)
  [200, headers(body), Array(body)]
end

#call(env) ⇒ Object



12
13
14
15
16
17
# 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) ⇒ Object



19
20
21
# File 'lib/rack/allocation_stats/middleware.rb', line 19

def call_app(env)
  @app.call(env)
end

#choose_actionObject



23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



36
37
38
39
40
41
42
# 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) ⇒ Object



48
49
50
51
52
53
# 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_textObject



55
56
57
58
59
60
# 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