browser 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e4f0f94511f5a4e169815e5b034e4fa3a53099d
4
- data.tar.gz: 90fb62dfa2b9ba5198ede34f771aa4f0a13e752d
3
+ metadata.gz: 3a1b14e0741db49c503fcd83bf58203f643538b7
4
+ data.tar.gz: a556e47aed22b522f909d3b5a4b715d73a131287
5
5
  SHA512:
6
- metadata.gz: 6de159fbcda822346be969345f46b7fca93231c624b163cda8b3f23e75589f2e8b2613d329a5d26feae03d3e554b70ceb54730261c9f371303ec60283f3e98f4
7
- data.tar.gz: fac02f1cab7ed3d2afd41c5dcd7ba0d3e67a809eb1e5fdaf6b365490ebdc089b15665ec57ff5b44ef5685eef1c9137c806de357e10e0f9e866007611133a56d3
6
+ metadata.gz: 561cc37a9c7184d528cbcb72106d0542fa4f568489354b747a6c8351a72b1ddb3129e2742aa8d38eac6625bc5758bdefef7ca413202e04f10f7ce6586717a78d
7
+ data.tar.gz: a15cc2f64759a2ed6078afdafe49966d464166e0997c9e3c887eb744a6fa0a8d370ea732cf7e87c4edf82b1f778b9d4eb3ec23bb5274ed2652511608b12763d0
data/README.md CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  [![Travis-CI](https://travis-ci.org/fnando/browser.png)](https://travis-ci.org/fnando/browser)
4
4
  [![CodeClimate](https://codeclimate.com/github/fnando/browser.png)](https://codeclimate.com/github/fnando/browser)
5
- [![Gem Version](https://badge.fury.io/rb/browser.svg)](http://badge.fury.io/rb/browser)
5
+ [![Gem](https://img.shields.io/gem/v/browser.svg)](https://rubygems.org/gems/browser)
6
+ [![Gem](https://img.shields.io/gem/dt/browser.svg)](https://rubygems.org/gems/browser)
6
7
 
7
8
  Do some browser detection with Ruby. Includes ActionController integration.
8
9
 
@@ -58,7 +59,6 @@ browser.device.id
58
59
  browser.device.name
59
60
  browser.device.blackberry_playbook?
60
61
  browser.device.console?
61
- browser.device.ios?
62
62
  browser.device.ipad?
63
63
  browser.device.iphone?
64
64
  browser.device.ipod_touch?
@@ -97,7 +97,7 @@ browser.platform.ios? # detect iOS
97
97
  browser.platform.ios?(9) # detect specific iOS version
98
98
  browser.platform.ios_app?
99
99
  browser.platform.ios_webview?
100
- browser.platform.ios_webview? # request performed by ios' app webview
100
+ browser.platform.ios_webview? # request performed by ios' app webview
101
101
  browser.platform.linux?
102
102
  browser.platform.mac?
103
103
  browser.platform.other?
@@ -284,6 +284,10 @@ The class constructor now has a different signature. Change the instantiation fr
284
284
  - `ua`: must be a string representing the user agent.
285
285
  - `options`: must be a hash (for now it only accepts the `accept_language` option).
286
286
 
287
+ ##### `NoMethodError: undefined method 'user_agent'`
288
+
289
+ `.ua` can now be used to retrieve the full User Agent string.
290
+
287
291
  ## Development
288
292
 
289
293
  ### Writing code
data/Rakefile CHANGED
@@ -8,6 +8,7 @@ Rake::TestTask.new(:test) do |t|
8
8
  t.libs << "test"
9
9
  t.test_files = FileList["test/**/*_test.rb"]
10
10
  t.verbose = true
11
+ t.warning = false
11
12
  t.ruby_opts = %w[-rubygems]
12
13
  end
13
14
 
@@ -79,6 +79,24 @@ module Browser
79
79
  @root ||= Pathname.new(File.expand_path("../..", __FILE__))
80
80
  end
81
81
 
82
+ # Hold the list of browser matchers.
83
+ # Order is important.
84
+ def self.matchers
85
+ @matchers ||= [
86
+ Nokia,
87
+ UCBrowser,
88
+ PhantomJS,
89
+ BlackBerry,
90
+ Opera,
91
+ Edge,
92
+ InternetExplorer,
93
+ Firefox,
94
+ Chrome,
95
+ Safari,
96
+ Generic
97
+ ]
98
+ end
99
+
82
100
  # Define the rules which define a modern browser.
83
101
  # A rule must be a proc/lambda or any object that implements the method
84
102
  # === and accepts the browser object.
@@ -103,19 +121,7 @@ module Browser
103
121
  end
104
122
 
105
123
  def self.new(user_agent, **kwargs)
106
- [
107
- Nokia,
108
- UCBrowser,
109
- PhantomJS,
110
- BlackBerry,
111
- Opera,
112
- Edge,
113
- InternetExplorer,
114
- Firefox,
115
- Chrome,
116
- Safari,
117
- Generic,
118
- ]
124
+ matchers
119
125
  .map {|klass| klass.new(user_agent || EMPTY_STRING, **kwargs) }
120
126
  .find(&:match?)
121
127
  end
@@ -15,10 +15,7 @@ module Browser
15
15
 
16
16
  # Return a meta info about this browser.
17
17
  def meta
18
- Meta.constants.each_with_object(Set.new) do |meta_name, meta|
19
- meta_class = Meta.const_get(meta_name)
20
- meta.merge(meta_class.new(self).to_a)
21
- end.to_a
18
+ Meta.get(self)
22
19
  end
23
20
 
24
21
  alias_method :to_a, :meta
@@ -5,6 +5,7 @@ module Browser
5
5
 
6
6
  def detect_version?(actual_version, expected_version)
7
7
  return true unless expected_version
8
+ return false if expected_version && !actual_version
8
9
 
9
10
  expected_version = parse_version(expected_version)
10
11
  actual_version = parse_version(actual_version)
@@ -3,30 +3,38 @@ module Browser
3
3
  class Device
4
4
  attr_reader :ua
5
5
 
6
+ # Hold the list of device matchers.
7
+ # Order is important.
8
+ def self.matchers
9
+ @matchers ||= [
10
+ XboxOne,
11
+ Xbox360,
12
+ Surface,
13
+ TV,
14
+ BlackBerryPlaybook,
15
+ WiiU,
16
+ Wii,
17
+ KindleFire,
18
+ Kindle,
19
+ PlayStation4,
20
+ PlayStation3,
21
+ PSVita,
22
+ PSP,
23
+ Iphone,
24
+ Ipad,
25
+ IpodTouch,
26
+ Unknown
27
+ ]
28
+ end
29
+
6
30
  def initialize(ua)
7
31
  @ua = ua
8
32
  end
9
33
 
10
34
  def subject
11
- @subject ||= [
12
- XboxOne.new(ua),
13
- Xbox360.new(ua),
14
- Surface.new(ua),
15
- TV.new(ua),
16
- BlackBerryPlaybook.new(ua),
17
- WiiU.new(ua),
18
- Wii.new(ua),
19
- KindleFire.new(ua),
20
- Kindle.new(ua),
21
- PlayStation4.new(ua),
22
- PlayStation3.new(ua),
23
- PSVita.new(ua),
24
- PSP.new(ua),
25
- Iphone.new(ua),
26
- Ipad.new(ua),
27
- IpodTouch.new(ua),
28
- Unknown.new(ua)
29
- ].find(&:match?)
35
+ @subject ||= self.class.matchers
36
+ .map {|matcher| matcher.new(ua) }
37
+ .find(&:match?)
30
38
  end
31
39
 
32
40
  def id
@@ -1,6 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
  module Browser
3
3
  module Meta
4
+ def self.rules
5
+ @rules ||= [
6
+ Device,
7
+ GenericBrowser,
8
+ Id,
9
+ IE,
10
+ IOS,
11
+ Mobile,
12
+ Modern,
13
+ Platform,
14
+ Proxy,
15
+ Safari,
16
+ Tablet,
17
+ Webkit
18
+ ]
19
+ end
20
+
21
+ def self.get(browser)
22
+ rules.each_with_object(Set.new) do |rule, meta|
23
+ meta.merge(rule.new(browser).to_a)
24
+ end.to_a
25
+ end
26
+
4
27
  class Base
5
28
  # Set the browser instance.
6
29
  attr_reader :browser
@@ -5,25 +5,33 @@ module Browser
5
5
 
6
6
  attr_reader :ua
7
7
 
8
+ # Hold the list of platform matchers.
9
+ # Order is important.
10
+ def self.matchers
11
+ @matchers ||= [
12
+ AdobeAir,
13
+ ChromeOS,
14
+ WindowsMobile,
15
+ WindowsPhone,
16
+ Android,
17
+ BlackBerry,
18
+ IOS,
19
+ Mac,
20
+ FirefoxOS,
21
+ Windows,
22
+ Linux,
23
+ Other
24
+ ]
25
+ end
26
+
8
27
  def initialize(ua)
9
28
  @ua = ua
10
29
  end
11
30
 
12
31
  def subject
13
- @subject ||= [
14
- AdobeAir.new(ua),
15
- ChromeOS.new(ua),
16
- WindowsMobile.new(ua),
17
- WindowsPhone.new(ua),
18
- Android.new(ua),
19
- BlackBerry.new(ua),
20
- IOS.new(ua),
21
- Mac.new(ua),
22
- FirefoxOS.new(ua),
23
- Windows.new(ua),
24
- Linux.new(ua),
25
- Other.new(ua)
26
- ].find(&:match?)
32
+ @subject ||= self.class.matchers
33
+ .map {|matcher| matcher.new(ua) }
34
+ .find(&:match?)
27
35
  end
28
36
 
29
37
  def adobe_air?
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Browser
3
- VERSION = "2.0.2"
3
+ VERSION = "2.0.3"
4
4
  end
@@ -100,29 +100,6 @@ class BrowserTest < Minitest::Test
100
100
  assert_equal "0", browser.version
101
101
  end
102
102
 
103
- test "sets meta" do
104
- browser = Browser.new(Browser["CHROME"])
105
- assert_kind_of Array, browser.meta
106
- end
107
-
108
- test "returns string representation" do
109
- browser = Browser.new(Browser["CHROME"])
110
- meta = browser.to_s
111
-
112
- assert meta.include?("chrome")
113
- assert meta.include?("webkit")
114
- assert meta.include?("mac")
115
- assert meta.include?("modern")
116
- end
117
-
118
- test "returns string representation for mobile" do
119
- browser = Browser.new(Browser["BLACKBERRY"])
120
- meta = browser.to_s
121
-
122
- assert meta.include?("blackberry")
123
- assert meta.include?("mobile")
124
- end
125
-
126
103
  test "detects unknown id" do
127
104
  browser = Browser.new("Unknown")
128
105
  assert_equal :generic, browser.id
@@ -27,6 +27,7 @@ CHROME_OS: 'Mozilla/5.0 (X11; CrOS x86_64 3701.81.0) AppleWebKit/537.31 (KHTML,
27
27
  COREMEDIA: 'Apple Mac OS X v10.6.4 CoreMedia v1.0.0.10F569'
28
28
  CUSTOM_APP: "Our App 0.0.1 (Linux; Android 4.0.3; HTC Ruby Build/IML74K; en_CA)"
29
29
  FIREFOX: 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8'
30
+ FIREFOX_ANDROID: 'Mozilla/5.0 (Android; Mobile; rv:40.0) Gecko/40.0 Firefox/40.0'
30
31
  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'
31
32
  FIREFOX_MODERN: 'Mozilla/5.0 (X11; Ubuntu; Linux armv7l; rv:17.0) Gecko/20100101 Firefox/17.0'
32
33
  FIREFOX_OS: 'Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0'
@@ -90,4 +90,11 @@ class AndroidTest < Minitest::Test
90
90
  browser = Browser.new(Browser["ANDROID_NEXUS_PLAYER"])
91
91
  assert browser.platform.android?
92
92
  end
93
+
94
+ test "detect firefox for android without android version" do
95
+ browser = Browser.new(Browser["FIREFOX_ANDROID"])
96
+
97
+ assert browser.platform.android?
98
+ refute browser.platform.android?(5)
99
+ end
93
100
  end
@@ -2,6 +2,26 @@
2
2
  require "test_helper"
3
3
 
4
4
  class DeviceTest < Minitest::Test
5
+ class CustomDevice < Browser::Device::Base
6
+ def match?
7
+ ua =~ /Custom/
8
+ end
9
+
10
+ def id
11
+ :custom
12
+ end
13
+ end
14
+
15
+ test "extend matchers" do
16
+ Browser::Device.matchers.unshift(CustomDevice)
17
+ device = Browser::Device.new("Custom")
18
+ assert_equal :custom, device.id
19
+
20
+ Browser::Device.matchers.shift
21
+ device = Browser::Device.new("Custom")
22
+ assert_equal :unknown, device.id
23
+ end
24
+
5
25
  test "detect generic device" do
6
26
  device = Browser::Device.new("")
7
27
 
@@ -0,0 +1,47 @@
1
+ require "test_helper"
2
+
3
+ class MetaTest < Minitest::Test
4
+ class CustomRule < Browser::Meta::Base
5
+ def meta
6
+ "custom" if browser.ua =~ /Custom/
7
+ end
8
+ end
9
+
10
+ test "extend rules" do
11
+ Browser::Meta.rules.unshift(CustomRule)
12
+
13
+ browser = Browser.new("Custom")
14
+ assert browser.meta.include?("custom")
15
+
16
+ browser = Browser.new("Safari")
17
+ refute browser.meta.include?("custom")
18
+
19
+ Browser::Meta.rules.shift
20
+
21
+ browser = Browser.new("Custom")
22
+ refute browser.meta.include?("custom")
23
+ end
24
+
25
+ test "sets meta" do
26
+ browser = Browser.new(Browser["CHROME"])
27
+ assert_kind_of Array, browser.meta
28
+ end
29
+
30
+ test "returns string representation" do
31
+ browser = Browser.new(Browser["CHROME"])
32
+ meta = browser.to_s
33
+
34
+ assert meta.include?("chrome")
35
+ assert meta.include?("webkit")
36
+ assert meta.include?("mac")
37
+ assert meta.include?("modern")
38
+ end
39
+
40
+ test "returns string representation for mobile" do
41
+ browser = Browser.new(Browser["BLACKBERRY"])
42
+ meta = browser.to_s
43
+
44
+ assert meta.include?("blackberry")
45
+ assert meta.include?("mobile")
46
+ end
47
+ end
@@ -2,6 +2,26 @@
2
2
  require "test_helper"
3
3
 
4
4
  class PlatformTest < Minitest::Test
5
+ class CustomPlatform < Browser::Platform::Base
6
+ def match?
7
+ ua =~ /Custom/
8
+ end
9
+
10
+ def id
11
+ :custom
12
+ end
13
+ end
14
+
15
+ test "extend matchers" do
16
+ Browser::Platform.matchers.unshift(CustomPlatform)
17
+ platform = Browser::Platform.new("Custom")
18
+ assert_equal :custom, platform.id
19
+
20
+ Browser::Platform.matchers.shift
21
+ platform = Browser::Platform.new("Custom")
22
+ assert_equal :other, platform.id
23
+ end
24
+
5
25
  test "detect other" do
6
26
  platform = Browser::Platform.new("Other")
7
27
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-02 00:00:00.000000000 Z
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -238,6 +238,7 @@ files:
238
238
  - test/unit/ios_app_test.rb
239
239
  - test/unit/ios_test.rb
240
240
  - test/unit/kindle_test.rb
241
+ - test/unit/meta_test.rb
241
242
  - test/unit/nokia_test.rb
242
243
  - test/unit/opera_test.rb
243
244
  - test/unit/platform_test.rb
@@ -292,6 +293,7 @@ test_files:
292
293
  - test/unit/ios_app_test.rb
293
294
  - test/unit/ios_test.rb
294
295
  - test/unit/kindle_test.rb
296
+ - test/unit/meta_test.rb
295
297
  - test/unit/nokia_test.rb
296
298
  - test/unit/opera_test.rb
297
299
  - test/unit/platform_test.rb