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.2.0"
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
-
#check_http_headers_for_mobile ⇒ Object
protected
Check the HTTP headers for signs of mobile.
- #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”).
- #load_json_data ⇒ Object protected
-
#match(key_regex, ua_string = user_agent) ⇒ Object
protected
This method will be used to check custom regexes against the User-Agent string.
-
#match_detection_rules_against_UA(rules = self.rules) ⇒ Object
protected
Find a detection rule that matches the current User-agent.
-
#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.
-
#parse_headers_for_user_agent ⇒ String
protected
Parse the headers for the user agent - uses a list of possible keys as provided by upstream.
- #rules ⇒ Object protected
-
#tablet? ⇒ Boolean
Check if the device is a tablet.
- #ua_http_headers ⇒ Object protected
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
#check_http_headers_for_mobile ⇒ Object (protected)
Check the HTTP headers for signs of mobile. This is the fastest mobile check possible; it’s used inside isMobile() method.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mobile_detect/core.rb', line 125 def check_http_headers_for_mobile data["headerMatch"].each do |mobile_header, match_type| if(http_headers[mobile_header]) return false if match_type.nil? || !match_type["matches"].is_a?(Array) Array(match_type["matches"]).each do |match| return true if http_headers[mobile_header].include? match end return false end end false end |
#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 |
#load_json_data ⇒ Object (protected)
93 94 95 96 97 |
# File 'lib/mobile_detect/core.rb', line 93 def load_json_data File.open(File.("../../../data/Mobile_Detect.json", __FILE__), "r") do |file| JSON.load(file) end end |
#match(key_regex, ua_string = user_agent) ⇒ Object (protected)
This method will be used to check custom regexes against the User-Agent string.
148 149 150 151 152 153 154 |
# File 'lib/mobile_detect/core.rb', line 148 def match key_regex, ua_string = user_agent # Escape the special character which is the delimiter. # _, regex = key_regex regex = Array(key_regex).last # accepts a plain regex or a pair of key,regex regex.gsub!("/", "\/") !! (ua_string =~ /#{regex}/iu) end |
#match_detection_rules_against_UA(rules = self.rules) ⇒ Object (protected)
Find a detection rule that matches the current User-agent. not including deprecated params
159 160 161 162 163 164 165 |
# File 'lib/mobile_detect/core.rb', line 159 def match_detection_rules_against_UA rules = self.rules # not sure why the empty check is needed here.. not doing it rules.each do |regex| return true if match regex end false 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 |
#parse_headers_for_user_agent ⇒ String (protected)
Parse the headers for the user agent - uses a list of possible keys as provided by upstream
105 106 107 |
# File 'lib/mobile_detect/core.rb', line 105 def parse_headers_for_user_agent ua_http_headers.map{|header| http_headers[header]}.compact.join(" ").strip end |
#rules ⇒ Object (protected)
116 117 118 |
# File 'lib/mobile_detect/core.rb', line 116 def rules @rules ||= phones.merge(tablets.merge(browsers.merge())) 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 |
#ua_http_headers ⇒ Object (protected)
99 100 101 |
# File 'lib/mobile_detect/core.rb', line 99 def ua_http_headers data["uaHttpHeaders"] end |