diff --git a/lib/mobvious/strategies/url.rb b/lib/mobvious/strategies/url.rb index 322a748..dec5e79 100644 --- a/lib/mobvious/strategies/url.rb +++ b/lib/mobvious/strategies/url.rb @@ -10,7 +10,9 @@ class URL # @param rules # A hash containing regular expressions mapped to symbols. The regular expression # is evaluated against the whole URL of the request (including `http://`). If matching, - # the corresponding symbol is returned as the device type. + # the corresponding symbol is returned as the device type. + # **or** + # a symbol for one of predefined detection rules (`:mobile_path_rules`) # @param options # A hash with strategy options. # `disable_if_referer_set: true` disables the strategy if HTTP Referer header is set @@ -18,8 +20,12 @@ class URL # given regular expression # `disable_unless_referer_matches: /regex/` disables the strategy if HTTP Referer # doesn't match given regular expression - def initialize(rules = MOBILE_PATH_RULES, options = {}) - @rules = rules + def initialize(rules = :mobile_path_rules, options = {}) + if rules.is_a? Symbol + @rules = eval(rules.to_s.upcase) + else + @rules = rules + end default_options = { disable_if_referer_set: false, diff --git a/spec/mobvious/strategies/url_spec.rb b/spec/mobvious/strategies/url_spec.rb index 1be8876..54a408f 100644 --- a/spec/mobvious/strategies/url_spec.rb +++ b/spec/mobvious/strategies/url_spec.rb @@ -82,6 +82,16 @@ class URLSpec < MiniTest::Spec @strategy.get_device_type(@request).must_equal :mobile end end + + describe "using custom rules" do + before do + @strategy = URL.new(/\.foo\./ => :test) + end + + it "returns :test for any url" do + @strategy.get_device_type(@request).must_equal :test + end + end end end end