Class: MobileDetect
- Inherits:
-
Object
- Object
- MobileDetect
- 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
-
#http_headers ⇒ Object
Returns the value of attribute http_headers.
-
#user_agent ⇒ Object
To access the user agent, retrieved from http_headers if not explicitly set.
Instance Method Summary collapse
- #data ⇒ Object
-
#initialize(http_headers = {}, user_agent = nil) ⇒ MobileDetect
constructor
Construct an instance of this class.
-
#is?(key) ⇒ Boolean
Checks if the device is conforming to the provided key e.g detector.is?(“ios”) / detector.is?(“androidos”) / detector.is?(“iphone”).
-
#method_missing(name, *args, &blk) ⇒ Object
Checks if the device is conforming to the provided key e.g detector.ios? / detector.androidos? / detector.iphone?.
-
#mobile? ⇒ Boolean
not including deprecated params Check if the device is mobile.
-
#tablet? ⇒ Boolean
Check if the device is a tablet.
Constructor Details
#initialize(http_headers = {}, user_agent = nil) ⇒ MobileDetect
Construct an instance of this class.
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?
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_headers ⇒ Object
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_agent ⇒ Object
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
#data ⇒ Object
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”)
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
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
55 56 57 |
# File 'lib/mobile_detect/core.rb', line 55 def tablet? match_detection_rules_against_UA tablets end |