Class: Mobvious::Strategies::URL

Inherits:
Object
  • Object
show all
Defined in:
lib/mobvious/strategies/url.rb

Overview

Mobvious device detection strategy that uses URL pattern matching.

Constant Summary collapse

RULES_MOBILE_PATH =

Rule set with only one rule for domains that begin with m. matching as :mobile.

{ /^\w+:\/\/m\./ => :mobile }

Instance Method Summary collapse

Constructor Details

#initialize(rules = :mobile_path, options = {}) ⇒ URL

Creates a new URL strategy instance.

Parameters:

  • rules (defaults to: :mobile_path)

    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.
    or
    a symbol for one of predefined detection rules (:mobile_path)

  • options (defaults to: {})

    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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mobvious/strategies/url.rb', line 23

def initialize(rules = :mobile_path, options = {})
  if rules.is_a? Symbol
    @rules = eval("RULES_#{rules.to_s.upcase}")
  else
    @rules = rules
  end

  default_options = {
    disable_if_referer_set: false,
    disable_if_referer_matches: nil,
    disable_unless_referer_matches: nil
  }
  @options = default_options.merge(options)
end

Instance Method Details

#get_device_type(request) ⇒ Symbol

Gets device type using URL pattern matching. Returns nil if no match found.

Parameters:

  • request (Rack::Request)

Returns:

  • (Symbol)

    device type or nil



42
43
44
45
46
47
48
49
50
51
# File 'lib/mobvious/strategies/url.rb', line 42

def get_device_type(request)
  return nil if disabled_by_referer_set?(request) ||
                disabled_by_referer_matching?(request) ||
                disabled_by_referer_not_matching?(request)

  @rules.each do |regex, device_type|
    return device_type if request.url =~ regex
  end
  nil
end