lib/message_bus/rack/diagnostics.rb in message_bus-2.1.6 vs lib/message_bus/rack/diagnostics.rb in message_bus-2.2.0.pre
- old
+ new
@@ -1,70 +1,23 @@
# frozen_string_literal: true
+
module MessageBus::Rack; end
+# Accepts requests from clients interested in using diagnostics functionality
+# @see MessageBus::Diagnostics
class MessageBus::Rack::Diagnostics
+ # @param [Proc] app the rack app
+ # @param [Hash] config
+ # @option config [MessageBus::Instance] :message_bus (`MessageBus`) a specific instance of message_bus
def initialize(app, config = {})
@app = app
@bus = config[:message_bus] || MessageBus
end
- 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
-
- def generate_script_tag(name)
- "<script src='/message-bus/_diagnostics/assets/#{name}?#{file_hash(name)}' type='text/javascript'></script>"
- end
-
- def file_hash(asset)
- require 'digest/sha1'
- Digest::SHA1.hexdigest(asset_contents(asset))
- end
-
- def asset_contents(asset)
- File.open(asset_path(asset)).read
- end
-
- def asset_path(asset)
- File.expand_path("../../../../assets/#{asset}", __FILE__)
- end
-
- 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
-
- def translate_handlebars(name, content)
- "Ember.TEMPLATES['#{name}'] = Ember.Handlebars.compile(#{indent(content).inspect});"
- end
-
- # from ember-rails
- def indent(string)
- string.gsub(/$(.)/m, "\\1 ").strip
- end
-
+ # Process an HTTP request from a subscriber client
+ # @param [Rack::Request::Env] env the request environment
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)
@@ -94,7 +47,64 @@
end
return [200, { 'content-type' => 'text/javascript;' }, [content]]
end
return [404, {}, ['not found']]
+ end
+
+ private
+
+ 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
+
+ def generate_script_tag(name)
+ "<script src='/message-bus/_diagnostics/assets/#{name}?#{file_hash(name)}' type='text/javascript'></script>"
+ end
+
+ def file_hash(asset)
+ require 'digest/sha1'
+ Digest::SHA1.hexdigest(asset_contents(asset))
+ end
+
+ def asset_contents(asset)
+ File.open(asset_path(asset)).read
+ end
+
+ def asset_path(asset)
+ File.expand_path("../../../../assets/#{asset}", __FILE__)
+ end
+
+ 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
+
+ def translate_handlebars(name, content)
+ "Ember.TEMPLATES['#{name}'] = Ember.Handlebars.compile(#{indent(content).inspect});"
+ end
+
+ # from ember-rails
+ def indent(string)
+ string.gsub(/$(.)/m, "\\1 ").strip
end
end