Class: Browser::Base

Inherits:
Object
  • Object
show all
Includes:
DetectVersion
Defined in:
lib/browser/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#uaObject (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_languageObject

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

Returns:

  • (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

#botObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


245
246
247
# File 'lib/browser/base.rb', line 245

def chromium_based?
  false
end

#compatibility_view?Boolean

Returns:

  • (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.

Returns:

  • (Boolean)


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

#deviceObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Returns:

  • (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.

Returns:

  • (Boolean)


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

#metaObject Also known as: to_a

Return a meta info about this browser.



17
18
19
# File 'lib/browser/base.rb', line 17

def meta
  Meta.get(self)
end

#micro_messenger?(expected_version = nil) ⇒ Boolean Also known as: wechat?

Detect if browser is MicroMessenger.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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_versionObject



75
76
77
# File 'lib/browser/base.rb', line 75

def msie_full_version
  "0.0"
end

#msie_versionObject



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

#platformObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Returns:

  • (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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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_sObject

Return meta representation as string.



32
33
34
# File 'lib/browser/base.rb', line 32

def to_s
  meta.to_a.join(" ")
end

#uc_browser?(expected_version = nil) ⇒ Boolean

Detect if browser is UCBrowser.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


257
258
259
# File 'lib/browser/base.rb', line 257

def unknown?
  id == :unknown_browser
end

#versionObject



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.

Returns:

  • (Boolean)


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_versionObject



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

Returns:

  • (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.

Returns:

  • (Boolean)


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