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

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.



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.



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