Memoize the bus routes

Memoize the bus routes

he stops the strings being created on each request

diff --git a/lib/message_bus/rack/middleware.rb b/lib/message_bus/rack/middleware.rb
index 8b7893d..809bfc1 100644
--- a/lib/message_bus/rack/middleware.rb
+++ b/lib/message_bus/rack/middleware.rb
@@ -39,7 +39,10 @@ class MessageBus::Rack::Middleware
     @bus = config[:message_bus] || MessageBus
     @connection_manager = MessageBus::ConnectionManager.new(@bus)
     @started_listener = false
-    @base_route_length = "#{@bus.base_route}message-bus/".length
+    @base_route = "#{@bus.base_route}message-bus/"
+    @base_route_length = @base_route.length
+    @diagnostics_route = "#{@base_route}_diagnostics"
+    @broadcast_route = "#{@base_route}broadcast"
     start_listener unless @bus.off?
   end
 
@@ -55,7 +58,7 @@ class MessageBus::Rack::Middleware
   # 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? "#{@bus.base_route}message-bus/"
+    return @app.call(env) unless env['PATH_INFO'].start_with? @base_route
 
     handle_request(env)
   end
@@ -64,13 +67,13 @@ class MessageBus::Rack::Middleware
 
   def handle_request(env)
     # special debug/test route
-    if @bus.allow_broadcast? && env['PATH_INFO'] == "#{@bus.base_route}message-bus/broadcast"
+    if @bus.allow_broadcast? && env['PATH_INFO'] == @broadcast_route
       parsed = Rack::Request.new(env)
       @bus.publish parsed["channel"], parsed["data"]
       return [200, { "Content-Type" => "text/html" }, ["sent"]]
     end
 
-    if env['PATH_INFO'].start_with? "#{@bus.base_route}message-bus/_diagnostics"
+    if env['PATH_INFO'].start_with? @diagnostics_route
       diags = MessageBus::Rack::Diagnostics.new(@app, message_bus: @bus)
       return diags.call(env)
     end
diff --git a/spec/lib/message_bus/rack/middleware_spec.rb b/spec/lib/message_bus/rack/middleware_spec.rb
index d83ffc0..46ca7f2 100644
--- a/spec/lib/message_bus/rack/middleware_spec.rb
+++ b/spec/lib/message_bus/rack/middleware_spec.rb
@@ -8,11 +8,13 @@ require 'rack/test'
 describe MessageBus::Rack::Middleware do
   include Rack::Test::Methods
   let(:extra_middleware) { nil }
+  let(:base_route) { nil }
 
   before do
     bus = @bus = MessageBus::Instance.new
     @bus.configure(MESSAGE_BUS_CONFIG)
     @bus.long_polling_enabled = false
+    @bus.base_route = base_route if base_route
 
     e_m = extra_middleware
     builder = Rack::Builder.new {
@@ -44,16 +46,18 @@ describe MessageBus::Rack::Middleware do
       @bus.long_polling_enabled = true
     end
 
-    it "should respond right away if dlp=t" do
-      post "/message-bus/ABC?dlp=t", '/foo1' => 0
-      @async_middleware.in_async?.must_equal false
-      last_response.ok?.must_equal true
-    end
+    describe "with altered base_route" do
+      let(:base_route) { "/base/route/" }
 
-    it "should respond ok when the base route is altered" do
-      @bus.base_route = "/base/route/"
+      it "should respond as normal" do
+        post "/base/route/message-bus/ABC?dlp=t", '/foo1' => 0
+        @async_middleware.in_async?.must_equal false
+        last_response.ok?.must_equal true
+      end
+    end
 
-      post "/base/route/message-bus/ABC?dlp=t", '/foo1' => 0
+    it "should respond right away if dlp=t" do
+      post "/message-bus/ABC?dlp=t", '/foo1' => 0
       @async_middleware.in_async?.must_equal false
       last_response.ok?.must_equal true
     end
@@ -154,14 +158,17 @@ describe MessageBus::Rack::Middleware do
       last_response.status.must_equal 200
     end
 
-    it "should get a 200 with html for an authorized user on a different base route" do
-      def @bus.is_admin_lookup
-        proc { |_| true }
-      end
-      @bus.base_route = "/base/route/"
+    describe "with an altered base_route" do
+      let(:base_route) { "/base/route/" }
 
-      get "/base/route/message-bus/_diagnostics"
-      last_response.status.must_equal 200
+      it "should get a 200 with html for an authorized user" do
+        def @bus.is_admin_lookup
+          proc { |_| true }
+        end
+
+        get "/base/route/message-bus/_diagnostics"
+        last_response.status.must_equal 200
+      end
     end
 
     it "should get the script it asks for" do

GitHub sha: 2e8b14c0

This commit appears in #222 which was merged by SamSaffron.