Class: Browser::Base
- Inherits:
-
Object
- Object
- Browser::Base
- Includes:
- DetectVersion
- Defined in:
- lib/browser/base.rb
Direct Known Subclasses
Alipay, BlackBerry, Chrome, Device::PSP, Device::TV, Device::Wii, DuckDuckGo, Electron, Facebook, Firefox, HuaweiBrowser, Instagram, InternetExplorer, Maxthon, Meta::IE, Meta::IOS, Meta::Id, MicroMessenger, MiuiBrowser, Nokia, Opera, Otter, PhantomJS, Platform::IOS, Platform::Mac, QQ, Safari, Snapchat, SougouBrowser, Sputnik, UCBrowser, Unknown, Weibo, Yandex
Instance Attribute Summary collapse
-
#ua ⇒ Object
readonly
Returns the value of attribute ua.
Instance Method Summary collapse
-
#accept_language ⇒ Object
Return an array with all preferred languages that this browser accepts.
- #alipay?(expected_version = nil) ⇒ Boolean
-
#bot ⇒ Object
Return the bot info.
-
#bot? ⇒ Boolean
Detect if current user agent is from a bot.
-
#chrome?(expected_version = nil) ⇒ Boolean
Detect if browser is Chrome.
-
#chromium_based? ⇒ Boolean
Detect if browser is Chromium-based.
- #compatibility_view? ⇒ Boolean
-
#core_media?(expected_version = nil) ⇒ Boolean
Detect if browser is Apple CoreMedia.
-
#device ⇒ Object
Return the device info.
-
#duck_duck_go?(expected_version = nil) ⇒ Boolean
Detect if browser is DuckDuckGo.
-
#edge?(expected_version = nil) ⇒ Boolean
Detect if browser is Microsoft Edge.
-
#electron?(expected_version = nil) ⇒ Boolean
Detect if the browser is Electron.
-
#facebook?(expected_version = nil) ⇒ Boolean
Detect if browser if Facebook.
-
#firefox?(expected_version = nil) ⇒ Boolean
Detect if browser is Firefox.
-
#google_search_app?(expected_version = nil) ⇒ Boolean
Detect if browser is Google Search App.
-
#huawei_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Huawei.
-
#ie?(expected_version = nil) ⇒ Boolean
Detect if browser is Microsoft Internet Explorer.
-
#initialize(ua, accept_language: nil) ⇒ Base
constructor
A new instance of Base.
-
#instagram?(expected_version = nil) ⇒ Boolean
Detect if browser is Instagram.
- #known? ⇒ Boolean
-
#maxthon?(expected_version = nil) ⇒ Boolean
Detect if browser is Maxthon.
-
#meta ⇒ Object
(also: #to_a)
Return a meta info about this browser.
-
#micro_messenger?(expected_version = nil) ⇒ Boolean
(also: #wechat?)
Detect if browser is MicroMessenger.
-
#miui_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Xiaomi Miui.
- #msie_full_version ⇒ Object
- #msie_version ⇒ Object
-
#nokia?(expected_version = nil) ⇒ Boolean
Detect if browser is Nokia S40 Ovi Browser.
-
#opera?(expected_version = nil) ⇒ Boolean
Detect if browser is Opera.
-
#opera_mini?(expected_version = nil) ⇒ Boolean
Detect if browser is Opera Mini.
-
#otter?(expected_version = nil) ⇒ Boolean
Detect if browser is Otter.
-
#phantom_js?(expected_version = nil) ⇒ Boolean
Detect if browser is PhantomJS.
-
#platform ⇒ Object
Return the platform.
-
#proxy? ⇒ Boolean
Detect if browser is a proxy browser.
-
#qq?(expected_version = nil) ⇒ Boolean
Detect if browser is QQ.
-
#quicktime?(expected_version = nil) ⇒ Boolean
Detect if browser is QuickTime.
-
#safari?(expected_version = nil) ⇒ Boolean
Detect if browser is Safari.
- #safari_webapp_mode? ⇒ Boolean
-
#samsung_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Samsung.
-
#snapchat?(expected_version = nil) ⇒ Boolean
Detect if browser is Snapchat.
-
#sougou_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Sougou.
-
#sputnik?(expected_version = nil) ⇒ Boolean
Detect if browser is Sputnik.
-
#to_s ⇒ Object
Return meta representation as string.
-
#uc_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is UCBrowser.
- #unknown? ⇒ Boolean
- #version ⇒ Object
-
#webkit?(expected_version = nil) ⇒ Boolean
Detect if browser is WebKit-based.
- #webkit_full_version ⇒ Object
- #weibo?(expected_version = nil) ⇒ Boolean
-
#yandex?(expected_version = nil) ⇒ Boolean
(also: #yandex_browser?)
Detect if browser is Yandex.
Constructor Details
#initialize(ua, accept_language: nil) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 14 |
# File 'lib/browser/base.rb', line 9 def initialize(ua, accept_language: nil) validate_size(:user_agent, ua.to_s) @ua = ua @accept_language_raw = accept_language.to_s end |
Instance Attribute Details
#ua ⇒ Object (readonly)
Returns the value of attribute ua.
7 8 9 |
# File 'lib/browser/base.rb', line 7 def ua @ua end |
Instance Method Details
#accept_language ⇒ Object
Return an array with all preferred languages that this browser accepts.
22 23 24 25 26 27 |
# File 'lib/browser/base.rb', line 22 def accept_language @accept_language ||= begin validate_size(:accept_language, @accept_language_raw) AcceptLanguage.parse(@accept_language_raw) end end |
#alipay?(expected_version = nil) ⇒ Boolean
189 190 191 |
# File 'lib/browser/base.rb', line 189 def alipay?(expected_version = nil) instance_of?(Alipay) && detect_version?(full_version, expected_version) end |
#bot ⇒ Object
Return the bot info.
46 47 48 |
# File 'lib/browser/base.rb', line 46 def bot @bot ||= Bot.new(ua) end |
#bot? ⇒ Boolean
Detect if current user agent is from a bot.
51 52 53 |
# File 'lib/browser/base.rb', line 51 def bot? bot.bot? end |
#chrome?(expected_version = nil) ⇒ Boolean
Detect if browser is Chrome.
146 147 148 |
# File 'lib/browser/base.rb', line 146 def chrome?(expected_version = nil) instance_of?(Chrome) && detect_version?(full_version, expected_version) end |
#chromium_based? ⇒ Boolean
Detect if browser is Chromium-based.
245 246 247 |
# File 'lib/browser/base.rb', line 245 def chromium_based? false end |
#compatibility_view? ⇒ Boolean
71 72 73 |
# File 'lib/browser/base.rb', line 71 def compatibility_view? false end |
#core_media?(expected_version = nil) ⇒ Boolean
Detect if browser is Apple CoreMedia.
120 121 122 123 |
# File 'lib/browser/base.rb', line 120 def core_media?(expected_version = nil) ua.include?("CoreMedia") && detect_version?(full_version, expected_version) end |
#device ⇒ Object
Return the device info.
56 57 58 |
# File 'lib/browser/base.rb', line 56 def device @device ||= Device.new(ua) end |
#duck_duck_go?(expected_version = nil) ⇒ Boolean
Detect if browser is DuckDuckGo.
200 201 202 203 |
# File 'lib/browser/base.rb', line 200 def duck_duck_go?(expected_version = nil) ua.include?("DuckDuckGo") && detect_version?(full_version, expected_version) end |
#edge?(expected_version = nil) ⇒ Boolean
Detect if browser is Microsoft Edge.
67 68 69 |
# File 'lib/browser/base.rb', line 67 def edge?(expected_version = nil) instance_of?(Edge) && detect_version?(full_version, expected_version) end |
#electron?(expected_version = nil) ⇒ Boolean
Detect if the browser is Electron.
267 268 269 |
# File 'lib/browser/base.rb', line 267 def electron?(expected_version = nil) instance_of?(Electron) && detect_version?(full_version, expected_version) end |
#facebook?(expected_version = nil) ⇒ Boolean
Detect if browser if Facebook.
96 97 98 99 |
# File 'lib/browser/base.rb', line 96 def facebook?(expected_version = nil) instance_of?(Facebook) && detect_version?(full_version, expected_version) end |
#firefox?(expected_version = nil) ⇒ Boolean
Detect if browser is Firefox.
141 142 143 |
# File 'lib/browser/base.rb', line 141 def firefox?(expected_version = nil) instance_of?(Firefox) && detect_version?(full_version, expected_version) end |
#google_search_app?(expected_version = nil) ⇒ Boolean
Detect if browser is Google Search App
240 241 242 |
# File 'lib/browser/base.rb', line 240 def google_search_app?(expected_version = nil) ua.include?("GSA") && detect_version?(full_version, expected_version) end |
#huawei_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Huawei.
212 213 214 215 |
# File 'lib/browser/base.rb', line 212 def huawei_browser?(expected_version = nil) instance_of?(HuaweiBrowser) && detect_version?(full_version, expected_version) end |
#ie?(expected_version = nil) ⇒ Boolean
Detect if browser is Microsoft Internet Explorer.
61 62 63 64 |
# File 'lib/browser/base.rb', line 61 def ie?(expected_version = nil) instance_of?(InternetExplorer) && detect_version?(full_version, expected_version) end |
#instagram?(expected_version = nil) ⇒ Boolean
Detect if browser is Instagram.
84 85 86 87 |
# File 'lib/browser/base.rb', line 84 def instagram?(expected_version = nil) instance_of?(Instagram) && detect_version?(full_version, expected_version) end |
#known? ⇒ Boolean
253 254 255 |
# File 'lib/browser/base.rb', line 253 def known? !unknown? end |
#maxthon?(expected_version = nil) ⇒ Boolean
Detect if browser is Maxthon.
224 225 226 |
# File 'lib/browser/base.rb', line 224 def maxthon?(expected_version = nil) instance_of?(Maxthon) && detect_version?(full_version, expected_version) end |
#meta ⇒ Object Also known as: to_a
Return a meta info about this browser.
17 18 19 |
# File 'lib/browser/base.rb', line 17 def Meta.get(self) end |
#micro_messenger?(expected_version = nil) ⇒ Boolean Also known as: wechat?
Detect if browser is MicroMessenger.
178 179 180 181 |
# File 'lib/browser/base.rb', line 178 def micro_messenger?(expected_version = nil) instance_of?(MicroMessenger) && detect_version?(full_version, expected_version) end |
#miui_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Xiaomi Miui.
218 219 220 221 |
# File 'lib/browser/base.rb', line 218 def miui_browser?(expected_version = nil) instance_of?(MiuiBrowser) && detect_version?(full_version, expected_version) end |
#msie_full_version ⇒ Object
75 76 77 |
# File 'lib/browser/base.rb', line 75 def msie_full_version "0.0" end |
#msie_version ⇒ Object
79 80 81 |
# File 'lib/browser/base.rb', line 79 def msie_version "0" end |
#nokia?(expected_version = nil) ⇒ Boolean
Detect if browser is Nokia S40 Ovi Browser.
173 174 175 |
# File 'lib/browser/base.rb', line 173 def nokia?(expected_version = nil) instance_of?(Nokia) && detect_version?(full_version, expected_version) end |
#opera?(expected_version = nil) ⇒ Boolean
Detect if browser is Opera.
151 152 153 |
# File 'lib/browser/base.rb', line 151 def opera?(expected_version = nil) instance_of?(Opera) && detect_version?(full_version, expected_version) end |
#opera_mini?(expected_version = nil) ⇒ Boolean
Detect if browser is Opera Mini.
194 195 196 197 |
# File 'lib/browser/base.rb', line 194 def opera_mini?(expected_version = nil) ua.include?("Opera Mini") && detect_version?(full_version, expected_version) end |
#otter?(expected_version = nil) ⇒ Boolean
Detect if browser is Otter.
102 103 104 105 |
# File 'lib/browser/base.rb', line 102 def otter?(expected_version = nil) instance_of?(Otter) && detect_version?(full_version, expected_version) end |
#phantom_js?(expected_version = nil) ⇒ Boolean
Detect if browser is PhantomJS
126 127 128 129 |
# File 'lib/browser/base.rb', line 126 def phantom_js?(expected_version = nil) instance_of?(PhantomJS) && detect_version?(full_version, expected_version) end |
#platform ⇒ Object
Return the platform.
41 42 43 |
# File 'lib/browser/base.rb', line 41 def platform @platform ||= Platform.new(ua) end |
#proxy? ⇒ Boolean
Detect if browser is a proxy browser.
262 263 264 |
# File 'lib/browser/base.rb', line 262 def proxy? nokia? || uc_browser? || opera_mini? end |
#qq?(expected_version = nil) ⇒ Boolean
Detect if browser is QQ.
229 230 231 |
# File 'lib/browser/base.rb', line 229 def qq?(expected_version = nil) instance_of?(QQ) && detect_version?(full_version, expected_version) end |
#quicktime?(expected_version = nil) ⇒ Boolean
Detect if browser is QuickTime
115 116 117 |
# File 'lib/browser/base.rb', line 115 def quicktime?(expected_version = nil) ua.match?(/QuickTime/i) && detect_version?(full_version, expected_version) end |
#safari?(expected_version = nil) ⇒ Boolean
Detect if browser is Safari.
132 133 134 |
# File 'lib/browser/base.rb', line 132 def safari?(expected_version = nil) instance_of?(Safari) && detect_version?(full_version, expected_version) end |
#safari_webapp_mode? ⇒ Boolean
136 137 138 |
# File 'lib/browser/base.rb', line 136 def safari_webapp_mode? (device.ipad? || device.iphone?) && ua.include?("AppleWebKit") end |
#samsung_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Samsung.
206 207 208 209 |
# File 'lib/browser/base.rb', line 206 def samsung_browser?(expected_version = nil) ua.include?("SamsungBrowser") && detect_version?(full_version, expected_version) end |
#snapchat?(expected_version = nil) ⇒ Boolean
Detect if browser is Snapchat.
90 91 92 93 |
# File 'lib/browser/base.rb', line 90 def snapchat?(expected_version = nil) instance_of?(Snapchat) && detect_version?(full_version, expected_version) end |
#sougou_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is Sougou.
234 235 236 237 |
# File 'lib/browser/base.rb', line 234 def sougou_browser?(expected_version = nil) instance_of?(SougouBrowser) && detect_version?(full_version, expected_version) end |
#sputnik?(expected_version = nil) ⇒ Boolean
Detect if browser is Sputnik.
156 157 158 |
# File 'lib/browser/base.rb', line 156 def sputnik?(expected_version = nil) instance_of?(Sputnik) && detect_version?(full_version, expected_version) end |
#to_s ⇒ Object
Return meta representation as string.
32 33 34 |
# File 'lib/browser/base.rb', line 32 def to_s .to_a.join(" ") end |
#uc_browser?(expected_version = nil) ⇒ Boolean
Detect if browser is UCBrowser.
167 168 169 170 |
# File 'lib/browser/base.rb', line 167 def uc_browser?(expected_version = nil) instance_of?(UCBrowser) && detect_version?(full_version, expected_version) end |
#unknown? ⇒ Boolean
257 258 259 |
# File 'lib/browser/base.rb', line 257 def unknown? id == :unknown_browser end |
#version ⇒ Object
36 37 38 |
# File 'lib/browser/base.rb', line 36 def version full_version.split(".").first end |
#webkit?(expected_version = nil) ⇒ Boolean
Detect if browser is WebKit-based.
108 109 110 111 112 |
# File 'lib/browser/base.rb', line 108 def webkit?(expected_version = nil) ua.match?(/AppleWebKit/i) && (!edge? || chromium_based?) && detect_version?(webkit_full_version, expected_version) end |
#webkit_full_version ⇒ Object
249 250 251 |
# File 'lib/browser/base.rb', line 249 def webkit_full_version ua[%r{AppleWebKit/([\d.]+)}, 1] || "0.0" end |
#weibo?(expected_version = nil) ⇒ Boolean
185 186 187 |
# File 'lib/browser/base.rb', line 185 def weibo?(expected_version = nil) instance_of?(Weibo) && detect_version?(full_version, expected_version) end |
#yandex?(expected_version = nil) ⇒ Boolean Also known as: yandex_browser?
Detect if browser is Yandex.
161 162 163 |
# File 'lib/browser/base.rb', line 161 def yandex?(expected_version = nil) instance_of?(Yandex) && detect_version?(full_version, expected_version) end |