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.


12
13
14
15
# File 'lib/browser/base.rb', line 12

def initialize(ua, accept_language: nil)
  @ua = ua
  @accept_language = AcceptLanguage.parse(accept_language)
end

Instance Attribute Details

#accept_languageObject (readonly)

Return an array with all preferred languages that this browser accepts.


10
11
12
# File 'lib/browser/base.rb', line 10

def accept_language
  @accept_language
end

#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

#alipay?(expected_version = nil) ⇒ Boolean

Returns:

  • (Boolean)

181
182
183
# File 'lib/browser/base.rb', line 181

def alipay?(expected_version = nil)
  Alipay.new(ua).match? && detect_version?(full_version, expected_version)
end

#botObject

Return the bot info.


39
40
41
# File 'lib/browser/base.rb', line 39

def bot
  @bot ||= Bot.new(ua)
end

#bot?Boolean

Detect if current user agent is from a bot.

Returns:

  • (Boolean)

44
45
46
# File 'lib/browser/base.rb', line 44

def bot?
  bot.bot?
end

#chrome?(expected_version = nil) ⇒ Boolean

Detect if browser is Chrome.

Returns:

  • (Boolean)

138
139
140
# File 'lib/browser/base.rb', line 138

def chrome?(expected_version = nil)
  Chrome.new(ua).match? && detect_version?(full_version, expected_version)
end

#compatibility_view?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'lib/browser/base.rb', line 64

def compatibility_view?
  false
end

#core_media?(expected_version = nil) ⇒ Boolean

Detect if browser is Apple CoreMedia.

Returns:

  • (Boolean)

113
114
115
# File 'lib/browser/base.rb', line 113

def core_media?(expected_version = nil)
  ua =~ /CoreMedia/ && detect_version?(full_version, expected_version)
end

#deviceObject

Return the device info.


49
50
51
# File 'lib/browser/base.rb', line 49

def device
  @device ||= Device.new(ua)
end

#duck_duck_go?(expected_version = nil) ⇒ Boolean

Detect if browser is DuckDuckGo.

Returns:

  • (Boolean)

191
192
193
# File 'lib/browser/base.rb', line 191

def duck_duck_go?(expected_version = nil)
  ua =~ /DuckDuckGo/ && detect_version?(full_version, expected_version)
end

#edge?(expected_version = nil) ⇒ Boolean

Detect if browser is Microsoft Edge.

Returns:

  • (Boolean)

60
61
62
# File 'lib/browser/base.rb', line 60

def edge?(expected_version = nil)
  Edge.new(ua).match? && detect_version?(full_version, expected_version)
end

#electron?(expected_version = nil) ⇒ Boolean

Detect if the browser is Electron.

Returns:

  • (Boolean)

227
228
229
# File 'lib/browser/base.rb', line 227

def electron?(expected_version = nil)
  Electron.new(ua).match? && detect_version?(full_version, expected_version)
end

#facebook?(expected_version = nil) ⇒ Boolean

Detect if browser if Facebook.

Returns:

  • (Boolean)

89
90
91
92
# File 'lib/browser/base.rb', line 89

def facebook?(expected_version = nil)
  Facebook.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#firefox?(expected_version = nil) ⇒ Boolean

Detect if browser is Firefox.

Returns:

  • (Boolean)

133
134
135
# File 'lib/browser/base.rb', line 133

def firefox?(expected_version = nil)
  Firefox.new(ua).match? && detect_version?(full_version, expected_version)
end

#google_search_app?(expected_version = nil) ⇒ Boolean

Detect if browser is Google Search App

Returns:

  • (Boolean)

205
206
207
# File 'lib/browser/base.rb', line 205

def google_search_app?(expected_version = nil)
  ua =~ /GSA/ && detect_version?(full_version, expected_version)
end

#ie?(expected_version = nil) ⇒ Boolean

Detect if browser is Microsoft Internet Explorer.

Returns:

  • (Boolean)

54
55
56
57
# File 'lib/browser/base.rb', line 54

def ie?(expected_version = nil)
  InternetExplorer.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#instagram?(expected_version = nil) ⇒ Boolean

Detect if browser is Instagram.

Returns:

  • (Boolean)

77
78
79
80
# File 'lib/browser/base.rb', line 77

def instagram?(expected_version = nil)
  Instagram.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#known?Boolean

Returns:

  • (Boolean)

213
214
215
# File 'lib/browser/base.rb', line 213

def known?
  !unknown?
end

#maxthon?(expected_version = nil) ⇒ Boolean

Returns:

  • (Boolean)

200
201
202
# File 'lib/browser/base.rb', line 200

def maxthon?(expected_version = nil)
  Maxthon.new(ua).match? && detect_version?(full_version, expected_version)
end

#metaObject Also known as: to_a

Return a meta info about this browser.


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

def meta
  Meta.get(self)
end

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

Detect if browser is MicroMessenger.

Returns:

  • (Boolean)

170
171
172
173
# File 'lib/browser/base.rb', line 170

def micro_messenger?(expected_version = nil)
  MicroMessenger.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#msie_full_versionObject


68
69
70
# File 'lib/browser/base.rb', line 68

def msie_full_version
  "0.0"
end

#msie_versionObject


72
73
74
# File 'lib/browser/base.rb', line 72

def msie_version
  "0"
end

#nokia?(expected_version = nil) ⇒ Boolean

Detect if browser is Nokia S40 Ovi Browser.

Returns:

  • (Boolean)

165
166
167
# File 'lib/browser/base.rb', line 165

def nokia?(expected_version = nil)
  Nokia.new(ua).match? && detect_version?(full_version, expected_version)
end

#opera?(expected_version = nil) ⇒ Boolean

Detect if browser is Opera.

Returns:

  • (Boolean)

143
144
145
# File 'lib/browser/base.rb', line 143

def opera?(expected_version = nil)
  Opera.new(ua).match? && detect_version?(full_version, expected_version)
end

#opera_mini?(expected_version = nil) ⇒ Boolean

Detect if browser is Opera Mini.

Returns:

  • (Boolean)

186
187
188
# File 'lib/browser/base.rb', line 186

def opera_mini?(expected_version = nil)
  ua =~ /Opera Mini/ && detect_version?(full_version, expected_version)
end

#otter?(expected_version = nil) ⇒ Boolean

Detect if browser is Otter.

Returns:

  • (Boolean)

95
96
97
98
# File 'lib/browser/base.rb', line 95

def otter?(expected_version = nil)
  Otter.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#phantom_js?(expected_version = nil) ⇒ Boolean

Detect if browser is PhantomJS

Returns:

  • (Boolean)

118
119
120
121
# File 'lib/browser/base.rb', line 118

def phantom_js?(expected_version = nil)
  PhantomJS.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#platformObject

Return the platform.


34
35
36
# File 'lib/browser/base.rb', line 34

def platform
  @platform ||= Platform.new(ua)
end

#proxy?Boolean

Detect if browser is a proxy browser.

Returns:

  • (Boolean)

222
223
224
# File 'lib/browser/base.rb', line 222

def proxy?
  nokia? || uc_browser? || opera_mini?
end

#quicktime?(expected_version = nil) ⇒ Boolean

Detect if browser is QuickTime

Returns:

  • (Boolean)

108
109
110
# File 'lib/browser/base.rb', line 108

def quicktime?(expected_version = nil)
  ua =~ /QuickTime/i && detect_version?(full_version, expected_version)
end

#safari?(expected_version = nil) ⇒ Boolean

Detect if browser is Safari.

Returns:

  • (Boolean)

124
125
126
# File 'lib/browser/base.rb', line 124

def safari?(expected_version = nil)
  Safari.new(ua).match? && detect_version?(full_version, expected_version)
end

#safari_webapp_mode?Boolean

Returns:

  • (Boolean)

128
129
130
# File 'lib/browser/base.rb', line 128

def safari_webapp_mode?
  (device.ipad? || device.iphone?) && ua =~ /AppleWebKit/
end

#samsung_browser?(expected_version = nil) ⇒ Boolean

Detect if browser is Samsung.

Returns:

  • (Boolean)

196
197
198
# File 'lib/browser/base.rb', line 196

def samsung_browser?(expected_version = nil)
  ua =~ /SamsungBrowser/ && detect_version?(full_version, expected_version)
end

#snapchat?(expected_version = nil) ⇒ Boolean

Detect if browser is Snapchat.

Returns:

  • (Boolean)

83
84
85
86
# File 'lib/browser/base.rb', line 83

def snapchat?(expected_version = nil)
  Snapchat.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#sputnik?(expected_version = nil) ⇒ Boolean

Detect if browser is Sputnik.

Returns:

  • (Boolean)

148
149
150
# File 'lib/browser/base.rb', line 148

def sputnik?(expected_version = nil)
  Sputnik.new(ua).match? && detect_version?(full_version, expected_version)
end

#to_sObject

Return meta representation as string.


25
26
27
# File 'lib/browser/base.rb', line 25

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

#uc_browser?(expected_version = nil) ⇒ Boolean

Detect if browser is UCBrowser.

Returns:

  • (Boolean)

159
160
161
162
# File 'lib/browser/base.rb', line 159

def uc_browser?(expected_version = nil)
  UCBrowser.new(ua).match? &&
    detect_version?(full_version, expected_version)
end

#unknown?Boolean

Returns:

  • (Boolean)

217
218
219
# File 'lib/browser/base.rb', line 217

def unknown?
  id == :unknown_browser
end

#versionObject


29
30
31
# File 'lib/browser/base.rb', line 29

def version
  full_version.split(".").first
end

#webkit?(expected_version = nil) ⇒ Boolean

Detect if browser is WebKit-based.

Returns:

  • (Boolean)

101
102
103
104
105
# File 'lib/browser/base.rb', line 101

def webkit?(expected_version = nil)
  ua =~ /AppleWebKit/i &&
    (!edge? || Edge.new(ua).chrome_based?) &&
    detect_version?(webkit_full_version, expected_version)
end

#webkit_full_versionObject


209
210
211
# File 'lib/browser/base.rb', line 209

def webkit_full_version
  ua[%r{AppleWebKit/([\d.]+)}, 1] || "0.0"
end

#weibo?(expected_version = nil) ⇒ Boolean

Returns:

  • (Boolean)

177
178
179
# File 'lib/browser/base.rb', line 177

def weibo?(expected_version = nil)
  Weibo.new(ua).match? && detect_version?(full_version, expected_version)
end

#yandex?(expected_version = nil) ⇒ Boolean Also known as: yandex_browser?

Detect if browser is Yandex.

Returns:

  • (Boolean)

153
154
155
# File 'lib/browser/base.rb', line 153

def yandex?(expected_version = nil)
  Yandex.new(ua).match? && detect_version?(full_version, expected_version)
end