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

MOBILE_PATH_RULES =

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_RULES) ⇒ URL

Creates a new URL strategy instance.

Parameters:

  • rules (defaults to: MOBILE_PATH_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.



14
15
16
# File 'lib/mobvious/strategies/url.rb', line 14

def initialize(rules = MOBILE_PATH_RULES)
  @rules = rules
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



22
23
24
25
26
27
# File 'lib/mobvious/strategies/url.rb', line 22

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