class RorVsWild::Local::Middleware

Constants

CSS_FILES
CSS_FOLDER
JS_FILES
JS_FOLDER
LOCAL_FOLDER

Attributes

app[R]
config[R]

Public Class Methods

new(app, config) click to toggle source
# File lib/rorvswild/local/middleware.rb, line 10
def initialize(app, config)
  @app, @config = app, config
end

Public Instance Methods

call(env) click to toggle source
# 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) click to toggle source
# 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_errors() click to toggle source
# 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_javascript() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 55
def serve_javascript
  [200, {"Content-Type" => "application/javascript"}, StringIO.new(concatenate_javascript)]
end
serve_jobs() click to toggle source
# 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_json() click to toggle source
# 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_requests() click to toggle source
# 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) click to toggle source
# 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_stylesheet() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 51
def serve_stylesheet
  [200, {"Content-Type" => "text/css"}, StringIO.new(concatenate_stylesheet)]
end

Private Instance Methods

concatenate_assets(directory, files) click to toggle source
# File lib/rorvswild/local/middleware.rb, line 114
def concatenate_assets(directory, files)
  files.map { |file| File.read(File.join(directory, file)) }.join("\n")
end
concatenate_javascript() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 104
def concatenate_javascript
  js = File.read(File.join(JS_FOLDER, "application.js"))
  js = js.split("// include javascript here")
  js[0] + concatenate_assets(JS_FOLDER, JS_FILES) + js[1]
end
concatenate_stylesheet() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 110
def concatenate_stylesheet
  concatenate_assets(CSS_FOLDER, CSS_FILES)
end
editor_url() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 81
def editor_url
  RorVsWild.agent.config[:editor_url]
end
empty_html_page() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 118
def empty_html_page
  "<!DOCTYPE html>\n<html><head></head><body></body></html>".dup
end
inject_into(html) click to toggle source
# File lib/rorvswild/local/middleware.rb, line 85
def inject_into(html)
  if index = html.index("</body>")
    markup = File.read(File.join(LOCAL_FOLDER, "local.html.erb"))
    markup = ERB.new(markup).result(binding)
    markup = markup.html_safe if markup.respond_to?(:html_safe)
    html.insert(index, markup)
  end
  html
rescue Encoding::UndefinedConversionError => ex
  log_incompatible_encoding_warning(ex)
  nil
end
log_incompatible_encoding_warning(exception) click to toggle source
# File lib/rorvswild/local/middleware.rb, line 128
def log_incompatible_encoding_warning(exception)
  RorVsWild.logger.warn("RorVsWild::Local cannot be embeded into your HTML page because of incompatible #{exception.message}." +
    " However you can just visit the /rorvswild page to see the profiler.")
end
log_incompatible_middleware_warning() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 122
def log_incompatible_middleware_warning
  RorVsWild.logger.warn("RorVsWild::Local cannot be embeded into your HTML page because of compression." +
    " Try to disable Rack::Deflater in development only." +
    " In the meantime just visit the /rorvswild page to see the profiler.")
end
widget_position() click to toggle source
# File lib/rorvswild/local/middleware.rb, line 77
def widget_position
  (config = RorVsWild.agent.config) && config[:widget]
end