Class: MobileDetect

Inherits:
Object
  • Object
show all
Defined in:
lib/mobile_detect/core.rb,
lib/mobile_detect/version.rb

Constant Summary collapse

VERSION =

mobile_detect version

"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_headers = {}, user_agent = nil) ⇒ MobileDetect

Construct an instance of this class.

Parameters:

  • http_headers (hash) (defaults to: {})

    Specify the http headers of the request.

  • user_agent (string) (defaults to: nil)

    Specify the User-Agent header. If null, will use HTTP_USER_AGENT from the http_headers hash instead.



12
13
14
15
16
# File 'lib/mobile_detect/core.rb', line 12

def initialize(http_headers = {}, user_agent = nil)
  @@data ||= load_json_data
  self.http_headers = http_headers
  self.user_agent = user_agent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object

Checks if the device is conforming to the provided key e.g detector.ios? / detector.androidos? / detector.iphone?

Returns:

  • bool



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mobile_detect/core.rb', line 80

def method_missing(name, *args, &blk)
  unless(Array(args).empty?)
    puts "Args to #{name} method ignored"
  end

  unless(name[-1] == "?")
    super
  end

  is? name[0..-2]
end

Instance Attribute Details

#http_headersObject

Returns the value of attribute http_headers.



4
5
6
# File 'lib/mobile_detect/core.rb', line 4

def http_headers
  @http_headers
end

#user_agentObject

To access the user agent, retrieved from http_headers if not explicitly set



43
44
45
46
47
48
49
# File 'lib/mobile_detect/core.rb', line 43

def user_agent
  @user_agent ||= parse_headers_for_user_agent

  raise "User agent needs to be set before this module can function" unless @user_agent

  @user_agent
end

Instance Method Details

#dataObject



18
19
20
# File 'lib/mobile_detect/core.rb', line 18

def data
  @@data
end

#is?(key) ⇒ Boolean

Checks if the device is conforming to the provided key e.g detector.is?(“ios”) / detector.is?(“androidos”) / detector.is?(“iphone”)

Returns:

  • (Boolean)

    bool



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mobile_detect/core.rb', line 62

def is?(key)
  # Make the keys lowercase so we can match: is?("Iphone"), is?("iPhone"), is?("iphone"), etc.
  key = key.downcase

  lc_rules = Hash[rules.map do |k, v|
    [k.downcase, v.downcase]
  end]

  unless lc_rules[key]
    raise NoMethodError, "Provided key, #{key}, is invalid"
  end

  match lc_rules[key]
end

#mobile?Boolean

not including deprecated params Check if the device is mobile. Returns true if any type of mobile device detected, including special ones

Returns:

  • (Boolean)

    bool



31
32
33
34
# File 'lib/mobile_detect/core.rb', line 31

def mobile?
  (check_http_headers_for_mobile or
  match_detection_rules_against_UA)
end

#tablet?Boolean

Check if the device is a tablet. Return true if any type of tablet device is detected. not including deprecated params

Returns:

  • (Boolean)

    bool



55
56
57
# File 'lib/mobile_detect/core.rb', line 55

def tablet?
  match_detection_rules_against_UA tablets
end