Skip to content

Commit

Permalink
Add Facebook in-app browser. Close fnando#296.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Aug 9, 2017
1 parent 484b25b commit 7f3a4d2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add support for QQ Browser Mac & Mac Lite.
- Add support for Electron Framework.
- Add support for Facebook in-app browser.

## v2.4.0

Expand Down
6 changes: 6 additions & 0 deletions lib/browser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def msie_version
"0"
end

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

# Detect if browser is WebKit-based.
def webkit?(expected_version = nil)
ua =~ /AppleWebKit/i &&
Expand Down
2 changes: 2 additions & 0 deletions lib/browser/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require "browser/qq"
require "browser/alipay"
require "browser/electron"
require "browser/facebook"

require "browser/bot"
require "browser/middleware"
Expand Down Expand Up @@ -51,6 +52,7 @@ def self.matchers
Edge,
InternetExplorer,
Firefox,
Facebook, # must be placed before Chrome and Safari
Weibo, # must be placed before Chrome and Safari
QQ, # must be placed before Chrome and Safari
Alipay, # must be placed before Chrome and Safari
Expand Down
21 changes: 21 additions & 0 deletions lib/browser/facebook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Browser
class Facebook < Base
def id
:facebook
end

def name
"Facebook"
end

def full_version
ua[%r[FBAV/([\d.]+)], 1]
end

def match?
ua =~ /FBAV/
end
end
end
1 change: 1 addition & 0 deletions test/ua.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CHROME: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/53
CHROME_OS: 'Mozilla/5.0 (X11; CrOS x86_64 3701.81.0) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.57 Safari/537.31.'
COREMEDIA: 'Apple Mac OS X v10.6.4 CoreMedia v1.0.0.10F569'
CUSTOM_APP: "Our App 0.0.1 (Linux; Android 4.0.3; HTC Ruby Build/IML74K; en_CA)"
FACEBOOK: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 [FBAN/FBIOS;FBAV/135.0.0.45.90;FBBV/66877072;FBDV/iPhone9,3;FBMD/iPhone;FBSN/iOS;FBSV/10.3.3;FBSS/2;FBCR/AT&T;FBID/phone;FBLC/en_US;FBOP/5;FBRV/0]
FIREFOX: 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8'
FIREFOX_ANDROID: 'Mozilla/5.0 (Android; Mobile; rv:40.0) Gecko/40.0 Firefox/40.0'
FIREFOX_IOS: 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) FxiOS/1.2 Mobile/13C75 Safari/601.1.46'
Expand Down
19 changes: 19 additions & 0 deletions test/unit/facebook_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "test_helper"

class FacebookTest < Minitest::Test
test "detects facebook" do
browser = Browser.new(Browser["FACEBOOK"])

assert_equal "Facebook", browser.name
assert browser.facebook?
assert_equal "135.0.0.45.90", browser.full_version
assert_equal "135", browser.version
end

test "detects version by range" do
browser = Browser.new(Browser["FACEBOOK"])
assert browser.facebook?(%w[>=135])
end
end

0 comments on commit 7f3a4d2

Please sign in to comment.