Class: RorVsWild::Local::Middleware

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

Constant Summary collapse

LOCAL_FOLDER =
File.expand_path(File.dirname(__FILE__))
JS_FOLDER =
File.join(LOCAL_FOLDER, "javascript")
CSS_FOLDER =
File.join(LOCAL_FOLDER, "stylesheet")
JS_FILES =
["vendor/mustache.js", "vendor/barber.js", "vendor/prism.js", "local.js"]
CSS_FILES =
["vendor/prism.css", "local.css"]

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
# File 'lib/rorvswild/local/middleware.rb', line 12

def call(env)
  env["REQUEST_URI"] == "/rorvswild" ? standalone_profiler(env) : embed_profiler(env)
end

#concatenate_assets(directory, files) ⇒ Object



74
75
76
# File 'lib/rorvswild/local/middleware.rb', line 74

def concatenate_assets(directory, files)
  files.map { |file| File.read(File.join(directory, file)) }.join("\n")
end

#concatenate_javascriptObject



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

def concatenate_javascript
  concatenate_assets(JS_FOLDER, JS_FILES)
end

#concatenate_stylesheetObject



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

def concatenate_stylesheet
  concatenate_assets(CSS_FOLDER, CSS_FILES)
end

#embed_profiler(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rorvswild/local/middleware.rb', line 21

def embed_profiler(env)
  status, headers, body = app.call(env)
  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

#empty_html_pageObject



78
79
80
# File 'lib/rorvswild/local/middleware.rb', line 78

def empty_html_page
  "<!DOCTYPE html>\n<html><head></head><body></body></html>"
end

#html_markup(data) ⇒ Object



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

def html_markup(data)
  html = File.read(File.join(LOCAL_FOLDER, "local.html"))
  html % {data: html_escape(data.to_json), javascript_source: concatenate_javascript}
end

#inject_into(html) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rorvswild/local/middleware.rb', line 38

def inject_into(html)
  markup = html_markup(RorVsWild.agent.queue.requests).encode(html.encoding)
  style = "<style type='text/css'> #{concatenate_stylesheet}</style>".encode(html.encoding)

  markup = markup.html_safe if markup.respond_to?(:html_safe)
  style = style.html_safe if markup.respond_to?(:html_safe)

  if index = html.index("</body>")
    html.insert(index, markup)
    html.insert(index, style)
  end
  html
rescue Encoding::UndefinedConversionError => ex
  log_incompatible_encoding_warning(ex)
  nil
end

#log_incompatible_encoding_warning(exception) ⇒ Object



88
89
90
91
# File 'lib/rorvswild/local/middleware.rb', line 88

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_warningObject



82
83
84
85
86
# File 'lib/rorvswild/local/middleware.rb', line 82

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

#standalone_profiler(env) ⇒ Object



16
17
18
19
# File 'lib/rorvswild/local/middleware.rb', line 16

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