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.1"
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#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
-
#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?
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mobile_detect/core.rb', line 76 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
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/mobile_detect/core.rb', line 4 def data @data end |
#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
39 40 41 42 43 44 45 |
# File 'lib/mobile_detect/core.rb', line 39 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
#is?(key) ⇒ Boolean
Checks if the device is conforming to the provided key e.g detector.is?(“ios”) / detector.is?(“androidos”) / detector.is?(“iphone”)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mobile_detect/core.rb', line 58 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
27 28 29 30 |
# File 'lib/mobile_detect/core.rb', line 27 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
51 52 53 |
# File 'lib/mobile_detect/core.rb', line 51 def tablet? match_detection_rules_against_UA tablets end |