Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
URL strategy accepts symbol for predefined rule sets
Browse files Browse the repository at this point in the history
  • Loading branch information
jistr committed Apr 18, 2012
1 parent 4e4d1c4 commit 5ea5253
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/mobvious/strategies/url.rb
Expand Up @@ -10,16 +10,22 @@ 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
# `disable_if_referer_matches: /regex/` disables the strategy if HTTP Referer matches
# 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,
Expand Down
10 changes: 10 additions & 0 deletions spec/mobvious/strategies/url_spec.rb
Expand Up @@ -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

0 comments on commit 5ea5253

Please sign in to comment.