Class: RorVsWild::Local::Middleware

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/rorvswild/local/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, config) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
# File 'lib/rorvswild/local/middleware.rb', line 8

def initialize(app, config)
  @app, @config = app, config
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/rorvswild/local/middleware.rb', line 6

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/rorvswild/local/middleware.rb', line 6

def config
  @config
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rorvswild/local/middleware.rb', line 12

def call(env)
  case env["PATH_INFO"]
  when "/rorvswild" then serve_standalone_profiler(env)
  when "/rorvswild.css" then serve_stylesheet
  when "/rorvswild.js" then serve_javascript
  when "/rorvswild.json" then serve_json
  else serve_embed_profiler(env)
  end
end

#serve_embed_profiler(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rorvswild/local/middleware.rb', line 27

def serve_embed_profiler(env)
  status, headers, body = app.call(env)
  status = status.to_i
  if status >= 200 && status < 300 && headers["Content-Type"] && headers["Content-Type"].include?("text/html")
    if headers["Content-Encoding"]
      log_incompatible_middleware_warning
    elsif body.respond_to?(:each)
      content_length = 0
      body.each do |string|
        inject_into(string)
        content_length += string.size
      end
      headers["Content-Length"] = content_length.to_s if headers["Content-Length"]
    end
  end
  [status, headers, body]
end

#serve_javascriptObject



49
50
51
# File 'lib/rorvswild/local/middleware.rb', line 49

def serve_javascript
  [200, {"Content-Type" => "application/javascript"}, StringIO.new(concatenate_javascript)]
end

#serve_jsonObject



53
54
55
# File 'lib/rorvswild/local/middleware.rb', line 53

def serve_json
  [200, {"Content-Type" => "application/json"}, StringIO.new(RorVsWild.agent.queue.requests.to_json)]
end

#serve_standalone_profiler(env) ⇒ Object



22
23
24
25
# File 'lib/rorvswild/local/middleware.rb', line 22

def serve_standalone_profiler(env)
  html = inject_into(empty_html_page)
  [200, {"Content-Type" => "text/html; charset=utf-8"}, StringIO.new(html || empty_html_page)]
end

#serve_stylesheetObject



45
46
47
# File 'lib/rorvswild/local/middleware.rb', line 45

def serve_stylesheet
  [200, {"Content-Type" => "text/css"}, StringIO.new(concatenate_stylesheet)]
end