FEATURE: Allow skip_paths config to contain regular expressions (#461)

FEATURE: Allow skip_paths config to contain regular expressions (#461)

diff --git a/README.md b/README.md
index 21798f7..d980f4b 100644
--- a/README.md
+++ b/README.md
@@ -365,7 +365,7 @@ Option|Default|Description
 -------|---|--------
 pre_authorize_cb|Rails: dev only<br>Rack: always on|A lambda callback that returns true to make mini_profiler visible on a given request.
 position|`'top-left'`|Display mini_profiler on `'top-right'`, `'top-left'`, `'bottom-right'` or `'bottom-left'`.
-skip_paths|`[]`|Paths that skip profiling.
+skip_paths|`[]`|An array of paths that skip profiling. Both `String` and `Regexp` are acceptable in the array.
 skip_schema_queries|Rails dev: `true`<br>Othwerwise: `false`|`true` to skip schema queries.
 auto_inject|`true`|`true` to inject the miniprofiler script in the page.
 backtrace_ignores|`[]`|Regexes of lines to be removed from backtraces.
diff --git a/lib/mini_profiler/profiler.rb b/lib/mini_profiler/profiler.rb
index ea327ed..3ed1f76 100644
--- a/lib/mini_profiler/profiler.rb
+++ b/lib/mini_profiler/profiler.rb
@@ -209,8 +209,16 @@ module Rack
       # Someone (e.g. Rails engine) could change the SCRIPT_NAME so we save it
       env['RACK_MINI_PROFILER_ORIGINAL_SCRIPT_NAME'] = ENV['PASSENGER_BASE_URI'] || env['SCRIPT_NAME']
 
-      skip_it = (@config.skip_paths && @config.skip_paths.any? { |p| path.start_with?(p) }) ||
-                query_string =~ /pp=skip/
+      skip_it = /pp=skip/.match?(query_string) || (
+        @config.skip_paths &&
+        @config.skip_paths.any? do |p|
+          if p.instance_of?(String)
+            path.start_with?(p)
+          elsif p.instance_of?(Regexp)
+            p.match?(path)
+          end
+        end
+      )
       if skip_it
         return client_settings.handle_cookie(@app.call(env))
       end
diff --git a/spec/integration/mini_profiler_spec.rb b/spec/integration/mini_profiler_spec.rb
index 190af72..e67742a 100644
--- a/spec/integration/mini_profiler_spec.rb
+++ b/spec/integration/mini_profiler_spec.rb
@@ -257,6 +257,14 @@ describe Rack::MiniProfiler do
       expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
     end
 
+    it "skip_paths can contain regular expressions" do
+      Rack::MiniProfiler.config.skip_paths = [/path[^1]/]
+      get '/path2/a'
+      expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
+      get '/path1/a'
+      expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
+    end
+
     it 'disables default functionality' do
       Rack::MiniProfiler.config.enabled = false
       get '/html'
diff --git a/spec/support/common_store_spec.rb b/spec/support/common_store_spec.rb
index a97ac58..4146800 100644
--- a/spec/support/common_store_spec.rb
+++ b/spec/support/common_store_spec.rb
@@ -119,7 +119,7 @@ RSpec.shared_examples "snapshots storage" do |store|
       page = Rack::MiniProfiler::TimerStruct::Page.new({})
       page[:root].record_time(400)
       store.push_snapshot(page, Rack::MiniProfiler::Config.default)
-      
+
       loaded = store.load_snapshot(page[:id])
       expect(loaded).to be_instance_of(Rack::MiniProfiler::TimerStruct::Page)
       expect(loaded[:id]).to eq(page[:id])
diff --git a/website/sample.rb b/website/sample.rb
index bd80f6e..954a0fb 100644
--- a/website/sample.rb
+++ b/website/sample.rb
@@ -19,7 +19,7 @@ class SampleStorage < Rack::MiniProfiler::AbstractStore
     @page_struct
   end
   alias_method :load_snapshot, :load
-  
+
   def save(*args)
   end
 
@@ -49,7 +49,7 @@ class SampleStorage < Rack::MiniProfiler::AbstractStore
         methods.sample,
         paths.sample,
         SecureRandom.rand * @multipliers.sample,
-        ((Time.new.to_f - @time_units.sample * @time_multipliers.sample) * 1000).round
+        ((Time.now.to_f - @time_units.sample * @time_multipliers.sample) * 1000).round
       )
     end
     @snapshots.each_slice(batch_size) { |batch| blk.call(batch) }

GitHub sha: 264b2e02

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