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.



10
11
12
# File 'lib/rorvswild/local/middleware.rb', line 10

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rorvswild/local/middleware.rb', line 14

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
  when "/rorvswild/requests.json" then serve_requests
  when "/rorvswild/jobs.json" then serve_jobs
  when "/rorvswild/errors.json" then serve_errors
  else serve_embed_profiler(env)
  end
end

#serve_embed_profiler(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rorvswild/local/middleware.rb', line 32

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) && widget_position != "hidden"
      content_length = 0
      @current_request =  RorVsWild.agent.queue.requests.first
      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_errorsObject



71
72
73
# File 'lib/rorvswild/local/middleware.rb', line 71

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

#serve_javascriptObject



55
56
57
# File 'lib/rorvswild/local/middleware.rb', line 55

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

#serve_jobsObject



67
68
69
# File 'lib/rorvswild/local/middleware.rb', line 67

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

#serve_jsonObject



59
60
61
# File 'lib/rorvswild/local/middleware.rb', line 59

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

#serve_requestsObject



63
64
65
# File 'lib/rorvswild/local/middleware.rb', line 63

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

#serve_standalone_profiler(env) ⇒ Object



27
28
29
30
# File 'lib/rorvswild/local/middleware.rb', line 27

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



51
52
53
# File 'lib/rorvswild/local/middleware.rb', line 51

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