Class: MessageBus::Rack::Diagnostics
- Inherits:
-
Object
- Object
- MessageBus::Rack::Diagnostics
- Defined in:
- lib/message_bus/rack/diagnostics.rb
Instance Method Summary collapse
- #asset_contents(asset) ⇒ Object
- #asset_path(asset) ⇒ Object
- #call(env) ⇒ Object
- #file_hash(asset) ⇒ Object
- #generate_script_tag(name) ⇒ Object
-
#indent(string) ⇒ Object
from ember-rails.
- #index ⇒ Object
-
#initialize(app, config = {}) ⇒ Diagnostics
constructor
A new instance of Diagnostics.
- #js_asset(name) ⇒ Object
- #translate_handlebars(name, content) ⇒ Object
Constructor Details
#initialize(app, config = {}) ⇒ Diagnostics
Returns a new instance of Diagnostics.
5 6 7 8 |
# File 'lib/message_bus/rack/diagnostics.rb', line 5 def initialize(app, config = {}) @app = app @bus = config[:message_bus] || MessageBus end |
Instance Method Details
#asset_contents(asset) ⇒ Object
26 27 28 |
# File 'lib/message_bus/rack/diagnostics.rb', line 26 def asset_contents(asset) File.open(asset_path(asset)).read end |
#asset_path(asset) ⇒ Object
30 31 32 |
# File 'lib/message_bus/rack/diagnostics.rb', line 30 def asset_path(asset) File.("../../../../assets/#{asset}", __FILE__) end |
#call(env) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/message_bus/rack/diagnostics.rb', line 64 def call(env) return @app.call(env) unless env['PATH_INFO'].start_with? '/message-bus/_diagnostics' route = env['PATH_INFO'].split('/message-bus/_diagnostics')[1] if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env) return [403, {}, ['not allowed']] end return index unless route if route == '/discover' user_id = @bus.user_id_lookup.call(env) @bus.publish('/_diagnostics/discover', user_id: user_id) return [200, {}, ['ok']] end if route =~ /^\/hup\// hostname, pid = route.split('/hup/')[1].split('/') @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i) return [200, {}, ['ok']] end asset = route.split('/assets/')[1] if asset && !asset !~ /\// content = asset_contents(asset) split = asset.split('.') if split[1] == 'handlebars' content = (split[0], content) end return [200, { 'content-type' => 'text/javascript;' }, [content]] end return [404, {}, ['not found']] end |
#file_hash(asset) ⇒ Object
21 22 23 24 |
# File 'lib/message_bus/rack/diagnostics.rb', line 21 def file_hash(asset) require 'digest/sha1' Digest::SHA1.hexdigest(asset_contents(asset)) end |
#generate_script_tag(name) ⇒ Object
17 18 19 |
# File 'lib/message_bus/rack/diagnostics.rb', line 17 def generate_script_tag(name) "<script src='/message-bus/_diagnostics/assets/#{name}?#{file_hash(name)}' type='text/javascript'></script>" end |
#indent(string) ⇒ Object
from ember-rails
60 61 62 |
# File 'lib/message_bus/rack/diagnostics.rb', line 60 def indent(string) string.gsub(/$(.)/m, "\\1 ").strip end |
#index ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/message_bus/rack/diagnostics.rb', line 34 def index html = <<HTML <!DOCTYPE html> <html> <head> </head> <body> <div id="app"></div> #{js_asset "jquery-1.8.2.js"} #{js_asset "handlebars.js"} #{js_asset "ember.js"} #{js_asset "message-bus.js"} #{js_asset "application.handlebars"} #{js_asset "index.handlebars"} #{js_asset "application.js"} </body> </html> HTML return [200, { "content-type" => "text/html;" }, [html]] end |
#js_asset(name) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/message_bus/rack/diagnostics.rb', line 10 def js_asset(name) return generate_script_tag(name) unless @bus.cache_assets @@asset_cache ||= {} @@asset_cache[name] ||= generate_script_tag(name) @@asset_cache[name] end |
#translate_handlebars(name, content) ⇒ Object
55 56 57 |
# File 'lib/message_bus/rack/diagnostics.rb', line 55 def (name, content) "Ember.TEMPLATES['#{name}'] = Ember.Handlebars.compile(#{indent(content).inspect});" end |