Class: MessageBus::Rack::Diagnostics

Inherits:
Object
  • Object
show all
Defined in:
lib/message_bus/rack/diagnostics.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, config = {}) ⇒ Diagnostics

Returns a new instance of Diagnostics.



4
5
6
7
# File 'lib/message_bus/rack/diagnostics.rb', line 4

def initialize(app, config = {})
  @app = app
  @bus = config[:message_bus] || MessageBus
end

Instance Method Details

#asset_contents(asset) ⇒ Object



25
26
27
# File 'lib/message_bus/rack/diagnostics.rb', line 25

def asset_contents(asset)
  File.open(asset_path(asset)).read
end

#asset_path(asset) ⇒ Object



29
30
31
# File 'lib/message_bus/rack/diagnostics.rb', line 29

def asset_path(asset)
  File.expand_path("../../../../assets/#{asset}", __FILE__)
end

#call(env) ⇒ Object



63
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
# File 'lib/message_bus/rack/diagnostics.rb', line 63

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 = translate_handlebars(split[0],content)
    end
    return [200, {'content-type' => 'text/javascript;'}, [content]]
  end

  return [404, {}, ['not found']]
end

#file_hash(asset) ⇒ Object



20
21
22
23
# File 'lib/message_bus/rack/diagnostics.rb', line 20

def file_hash(asset)
  require 'digest/sha1'
  Digest::SHA1.hexdigest(asset_contents(asset))
end

#generate_script_tag(name) ⇒ Object



16
17
18
# File 'lib/message_bus/rack/diagnostics.rb', line 16

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



59
60
61
# File 'lib/message_bus/rack/diagnostics.rb', line 59

def indent(string)
  string.gsub(/$(.)/m, "\\1  ").strip
end

#indexObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/message_bus/rack/diagnostics.rb', line 33

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



9
10
11
12
13
14
# File 'lib/message_bus/rack/diagnostics.rb', line 9

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



54
55
56
# File 'lib/message_bus/rack/diagnostics.rb', line 54

def translate_handlebars(name, content)
  "Ember.TEMPLATES['#{name}'] = Ember.Handlebars.compile(#{indent(content).inspect});"
end