Skip to content

Commit

Permalink
Add Browser::Base#chromium_based?. (#527)
Browse files Browse the repository at this point in the history
Close #523.
  • Loading branch information
fnando authored Jan 29, 2022
1 parent 5c51f18 commit fa4f685
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 126 deletions.
3 changes: 2 additions & 1 deletion FUNDING.yml → .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# These are supported funding model platforms

---
github: [fnando]
custom: ["https://paypal.me/nandovieira/🍕"]
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Add `Browser::Base#chromium_based?`.
- Change how browser detection works to avoid re-instantiating classes.

## 5.3.1

- Remove Stripe webhooks from bot list.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ browser = Browser.new("Some User Agent", accept_language: "en-us")
# General info
browser.bot?
browser.chrome?
browser.chromium_based?
browser.core_media?
browser.duck_duck_go?
browser.edge? # Newest MS browser
Expand Down
55 changes: 30 additions & 25 deletions lib/browser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ def device

# Detect if browser is Microsoft Internet Explorer.
def ie?(expected_version = nil)
InternetExplorer.new(ua).match? &&
instance_of?(InternetExplorer) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Microsoft Edge.
def edge?(expected_version = nil)
Edge.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Edge) && detect_version?(full_version, expected_version)
end

def compatibility_view?
Expand All @@ -82,32 +82,32 @@ def msie_version

# Detect if browser is Instagram.
def instagram?(expected_version = nil)
Instagram.new(ua).match? &&
instance_of?(Instagram) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Snapchat.
def snapchat?(expected_version = nil)
Snapchat.new(ua).match? &&
instance_of?(Snapchat) &&
detect_version?(full_version, expected_version)
end

# Detect if browser if Facebook.
def facebook?(expected_version = nil)
Facebook.new(ua).match? &&
instance_of?(Facebook) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Otter.
def otter?(expected_version = nil)
Otter.new(ua).match? &&
instance_of?(Otter) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is WebKit-based.
def webkit?(expected_version = nil)
ua.match?(/AppleWebKit/i) &&
(!edge? || Edge.new(ua).chrome_based?) &&
(!edge? || chromium_based?) &&
detect_version?(webkit_full_version, expected_version)
end

Expand All @@ -124,13 +124,13 @@ def core_media?(expected_version = nil)

# Detect if browser is PhantomJS
def phantom_js?(expected_version = nil)
PhantomJS.new(ua).match? &&
instance_of?(PhantomJS) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Safari.
def safari?(expected_version = nil)
Safari.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Safari) && detect_version?(full_version, expected_version)
end

def safari_webapp_mode?
Expand All @@ -139,55 +139,55 @@ def safari_webapp_mode?

# Detect if browser is Firefox.
def firefox?(expected_version = nil)
Firefox.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Firefox) && detect_version?(full_version, expected_version)
end

# Detect if browser is Chrome.
def chrome?(expected_version = nil)
Chrome.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Chrome) && detect_version?(full_version, expected_version)
end

# Detect if browser is Opera.
def opera?(expected_version = nil)
Opera.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Opera) && detect_version?(full_version, expected_version)
end

# Detect if browser is Sputnik.
def sputnik?(expected_version = nil)
Sputnik.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Sputnik) && detect_version?(full_version, expected_version)
end

# Detect if browser is Yandex.
def yandex?(expected_version = nil)
Yandex.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Yandex) && detect_version?(full_version, expected_version)
end
alias yandex_browser? yandex?

# Detect if browser is UCBrowser.
def uc_browser?(expected_version = nil)
UCBrowser.new(ua).match? &&
instance_of?(UCBrowser) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Nokia S40 Ovi Browser.
def nokia?(expected_version = nil)
Nokia.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Nokia) && detect_version?(full_version, expected_version)
end

# Detect if browser is MicroMessenger.
def micro_messenger?(expected_version = nil)
MicroMessenger.new(ua).match? &&
instance_of?(MicroMessenger) &&
detect_version?(full_version, expected_version)
end

alias wechat? micro_messenger?

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

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

# Detect if browser is Opera Mini.
Expand All @@ -210,29 +210,29 @@ def samsung_browser?(expected_version = nil)

# Detect if browser is Huawei.
def huawei_browser?(expected_version = nil)
HuaweiBrowser.new(ua).match? &&
instance_of?(HuaweiBrowser) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Xiaomi Miui.
def miui_browser?(expected_version = nil)
MiuiBrowser.new(ua).match? &&
instance_of?(MiuiBrowser) &&
detect_version?(full_version, expected_version)
end

# Detect if browser is Maxthon.
def maxthon?(expected_version = nil)
Maxthon.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Maxthon) && detect_version?(full_version, expected_version)
end

# Detect if browser is QQ.
def qq?(expected_version = nil)
QQ.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(QQ) && detect_version?(full_version, expected_version)
end

# Detect if browser is Sougou.
def sougou_browser?(expected_version = nil)
SougouBrowser.new(ua).match? &&
instance_of?(SougouBrowser) &&
detect_version?(full_version, expected_version)
end

Expand All @@ -241,6 +241,11 @@ def google_search_app?(expected_version = nil)
ua.include?("GSA") && detect_version?(full_version, expected_version)
end

# Detect if browser is Chromium-based.
def chromium_based?
false
end

def webkit_full_version
ua[%r{AppleWebKit/([\d.]+)}, 1] || "0.0"
end
Expand All @@ -260,7 +265,7 @@ def proxy?

# Detect if the browser is Electron.
def electron?(expected_version = nil)
Electron.new(ua).match? && detect_version?(full_version, expected_version)
instance_of?(Electron) && detect_version?(full_version, expected_version)
end

private def validate_size(subject, input)
Expand Down
7 changes: 4 additions & 3 deletions lib/browser/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ def self.matchers
end

def self.new(user_agent, **kwargs)
matchers
.map {|klass| klass.new(user_agent || EMPTY_STRING, **kwargs) }
.find(&:match?)
matchers.each do |matcher_class|
matcher = matcher_class.new(user_agent || EMPTY_STRING, **kwargs)
return matcher if matcher.match?
end
end
end
4 changes: 4 additions & 0 deletions lib/browser/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def full_version
"0.0"
end

def chromium_based?
true
end

def match?
ua.match?(/Chrome|CriOS/) &&
!ua.match?(/PhantomJS|FxiOS|ArchiveBot/) &&
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/edge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def match?
ua.match?(%r{((?:Edge|Edg|EdgiOS|EdgA)/[\d.]+|Trident/8)})
end

def chrome_based?
def chromium_based?
match? && ua.match?(/\bEdg\b/)
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/browser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class BrowserTest < Minitest::Test
assert_equal "Safari", browser.ua
end

test "don't fail with nil user agent" do
test "does not fail with nil user agent" do
browser = Browser.new(nil)
refute browser.known?
end
Expand All @@ -25,7 +25,7 @@ class BrowserTest < Minitest::Test
Safari
UCBrowser
].each do |ua|
test "don't fail when have no version info (#{ua})" do
test "does not fail when have no version info (#{ua})" do
browser = Browser.new(ua)
assert_equal "0", browser.version
assert_equal "0.0", browser.full_version
Expand Down
4 changes: 3 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require "simplecov"
SimpleCov.start
SimpleCov.start do
add_filter %r{/test/}
end

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require "bundler/setup"
Expand Down
6 changes: 3 additions & 3 deletions test/unit/android_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
class AndroidAppTest < Minitest::Test
let(:browser) { Browser.new(Browser["ANDROID_WEBVIEW"]) }

test "detect as android" do
test "detects as android" do
assert browser.platform.android?
end

test "detect as webview" do
test "detects as webview" do
assert browser.platform.android_webview?
end

test "non-webviews do not detect as webview" do
test "does not detect non-webviews as webview" do
%w[
ANDROID_CUPCAKE
ANDROID_DONUT
Expand Down
Loading

0 comments on commit fa4f685

Please sign in to comment.