browser 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ class Browser
2
+ module Meta
3
+ class Proxy < Base
4
+ def meta
5
+ "proxy" if browser.proxy?
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,51 @@
1
+ class Browser
2
+ module BlackBerry
3
+ # Return BlackBerry version.
4
+ def blackberry_version
5
+ ua[/BB(10)/, 1] ||
6
+ ua[/BlackBerry\d+\/(\d+)/, 1] ||
7
+ ua[/BlackBerry.*?Version\/(\d+)/, 1]
8
+ end
9
+
10
+ # Detect if browser is BlackBerry
11
+ def blackberry?(version = nil)
12
+ !!(ua =~ /(BlackBerry|BB10)/) && detect_version?(blackberry_version, version)
13
+ end
14
+
15
+ # Detect if is BlackBerry 4.
16
+ def blackberry4?
17
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
18
+ blackberry?(4)
19
+ end
20
+
21
+ # Detect if is BlackBerry 5.
22
+ def blackberry5?
23
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
24
+ blackberry?(5)
25
+ end
26
+
27
+ # Detect if is BlackBerry 6.
28
+ def blackberry6?
29
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
30
+ blackberry?(6)
31
+ end
32
+
33
+ # Detect if is BlackBerry 7.
34
+ def blackberry7?
35
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
36
+ blackberry?(7)
37
+ end
38
+
39
+ # Detect if is BlackBerry 10.
40
+ def blackberry10?
41
+ deprecate "Browser##{__method__} is deprecated; use Browser#blackberry?(version) instead"
42
+ blackberry?(10)
43
+ end
44
+
45
+ private
46
+
47
+ def blackberry_running_safari?
48
+ blackberry? && safari?
49
+ end
50
+ end
51
+ end
@@ -36,38 +36,44 @@ class Browser
36
36
  end
37
37
 
38
38
  # Detect if browser is Internet Explorer.
39
- def ie?
40
- msie? || modern_ie?
39
+ def ie?(version = nil)
40
+ (msie? || modern_ie?) && detect_version?(ie_version, version)
41
41
  end
42
42
 
43
43
  # Detect if browser is Internet Explorer 6.
44
44
  def ie6?
45
- ie? && version == "6"
45
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
46
+ ie?(6)
46
47
  end
47
48
 
48
49
  # Detect if browser is Internet Explorer 7.
49
50
  def ie7?
50
- ie? && version == "7"
51
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
52
+ ie?(7)
51
53
  end
52
54
 
53
55
  # Detect if browser is Internet Explorer 8.
54
56
  def ie8?
55
- ie? && version == "8"
57
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
58
+ ie?(8)
56
59
  end
57
60
 
58
61
  # Detect if browser is Internet Explorer 9.
59
62
  def ie9?
60
- ie? && version == "9"
63
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
64
+ ie?(9)
61
65
  end
62
66
 
63
67
  # Detect if browser is Internet Explorer 10.
64
68
  def ie10?
65
- ie? && version == "10"
69
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
70
+ ie?(10)
66
71
  end
67
72
 
68
73
  # Detect if browser is Internet Explorer 11.
69
74
  def ie11?
70
- ie? && version == "11"
75
+ deprecate "Browser##{__method__} is deprecated; use Browser#ie?(version) instead"
76
+ ie?(11)
71
77
  end
72
78
 
73
79
  # Detect if browser is Microsoft Edge.
@@ -10,16 +10,16 @@ class Browser
10
10
  !!(ua =~ /Opera Mini/)
11
11
  end
12
12
 
13
- # Detect if browser is BlackBerry
14
- def blackberry?
15
- !!(ua =~ /(BlackBerry)/)
16
- end
17
-
18
13
  # Detect if browser is Adobe AIR.
19
14
  def adobe_air?
20
15
  !!(ua =~ /adobeair/i)
21
16
  end
22
17
 
18
+ # Detect if browser is Nokia S40 Ovi Browser.
19
+ def nokia?
20
+ !!(ua =~ /S40OviBrowser/)
21
+ end
22
+
23
23
  private
24
24
 
25
25
  # Regex taken from http://detectmobilebrowsers.com
@@ -1,63 +1,69 @@
1
1
  class Browser
2
2
  module Platform
3
3
  # Detect if browser is Android.
4
- def android?
5
- !!(ua =~ /Android/ && !opera?)
4
+ def android?(version = nil)
5
+ !!(ua =~ /Android/ && !opera?) && detect_version?(android_version, version)
6
+ end
7
+
8
+ # Detect Android version.
9
+ def android_version
10
+ ua[/Android ([\d.]+)/, 1]
11
+ end
12
+
13
+ # Return the iOS version.
14
+ def ios_version
15
+ ua[/OS (\d)/, 1]
16
+ end
17
+
18
+ # Detect if running on iOS app webview.
19
+ def ios_app?
20
+ ios? && !ua.include?("Safari")
21
+ end
22
+
23
+ # Detect if is iOS webview.
24
+ def ios_webview?
25
+ ios_app?
6
26
  end
7
27
 
8
28
  # Detect if browser is ios?.
9
- def ios?
10
- ipod? || ipad? || iphone?
29
+ def ios?(version = nil)
30
+ (ipod? || ipad? || iphone?) && detect_version?(ios_version, version)
11
31
  end
12
32
 
13
33
  # Detect if is iOS4.
14
34
  def ios4?
15
- ios? && !!(ua =~ /OS 4/)
35
+ deprecate "Browser##{__method__} is deprecated; use Browser#ios?(version) instead"
36
+ ios?(4)
16
37
  end
17
38
 
18
39
  # Detect if is iOS5.
19
40
  def ios5?
20
- ios? && !!(ua =~ /OS 5/)
41
+ deprecate "Browser##{__method__} is deprecated; use Browser#ios?(version) instead"
42
+ ios?(5)
21
43
  end
22
44
 
23
45
  # Detect if is iOS6.
24
46
  def ios6?
25
- ios? && !!(ua =~ /OS 6/)
47
+ deprecate "Browser##{__method__} is deprecated; use Browser#ios?(version) instead"
48
+ ios?(6)
26
49
  end
27
50
 
28
51
  # Detect if is iOS7.
29
52
  def ios7?
30
- ios? && !!(ua =~ /OS 7/)
53
+ deprecate "Browser##{__method__} is deprecated; use Browser#ios?(version) instead"
54
+ ios?(7)
31
55
  end
32
56
 
33
57
  # Detect if is iOS8.
34
58
  def ios8?
35
- ios? && !!(ua =~ /OS 8/)
36
- end
37
-
38
- # Detect if is BlackBerry 4.
39
- def blackberry4?
40
- blackberry? && !!(ua =~ %r[BlackBerry\d+/4])
41
- end
42
-
43
- # Detect if is BlackBerry 5.
44
- def blackberry5?
45
- blackberry? && !!(ua =~ %r[BlackBerry\d+/5])
46
- end
47
-
48
- # Detect if is BlackBerry 6.
49
- def blackberry6?
50
- blackberry? && !!(ua =~ %r[Version/6])
51
- end
52
-
53
- # Detect if is BlackBerry 7.
54
- def blackberry7?
55
- blackberry? && !!(ua =~ %r[Version/7])
59
+ deprecate "Browser##{__method__} is deprecated; use Browser#ios?(version) instead"
60
+ ios?(8)
56
61
  end
57
62
 
58
- # Detect if is BlackBerry 10.
59
- def blackberry10?
60
- !!(ua =~ /BB10/)
63
+ # Detect if is iOS9.
64
+ def ios9?
65
+ deprecate "Browser##{__method__} is deprecated; use Browser#ios?(version) instead"
66
+ ios?(9)
61
67
  end
62
68
 
63
69
  # Detect if current platform is Macintosh.
@@ -111,7 +117,7 @@ class Browser
111
117
 
112
118
  # Detect if current platform is Windows in 64-bit architecture.
113
119
  def windows_x64?
114
- windows? && !!(ua =~ /(Win64|x64)/) && !!(ua =~ /x64/)
120
+ !!(windows? && ua =~ /(Win64|x64)/)
115
121
  end
116
122
 
117
123
  def windows_wow64?
@@ -0,0 +1,8 @@
1
+ class Browser
2
+ module Proxy
3
+ # Detect if browser is a proxy browser
4
+ def proxy?
5
+ nokia? || uc_browser? || opera_mini?
6
+ end
7
+ end
8
+ end
@@ -2,7 +2,7 @@ class Browser
2
2
  module Tv
3
3
  # Detect if browser is Television
4
4
  def tv?
5
- !!(ua =~ /tv/i)
5
+ !!(ua =~ /(tv|Android.*?ADT-1|Nexus Player)/i)
6
6
  end
7
7
  end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  class Browser
2
2
  module Version
3
3
  MAJOR = 1
4
- MINOR = 0
5
- PATCH = 1
4
+ MINOR = 1
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -0,0 +1,248 @@
1
+ require "test_helper"
2
+
3
+ class BrowserTest < Minitest::Test
4
+ setup do
5
+ @browser = Browser.new
6
+ end
7
+
8
+ test "yields self when block is given" do
9
+ browser = nil
10
+ Browser.new {|b| browser = b }
11
+ assert_kind_of Browser, browser
12
+ end
13
+
14
+ test "responds to ua methods" do
15
+ assert @browser.respond_to?(:ua)
16
+ assert @browser.respond_to?(:ua=)
17
+ end
18
+
19
+ test "delegates ua methods" do
20
+ @browser.user_agent = "Safari"
21
+ assert_equal "Safari", @browser.ua
22
+
23
+ @browser.ua = "Mozilla"
24
+ assert_equal "Mozilla", @browser.user_agent
25
+ end
26
+
27
+ test "sets accept language while instantiating object" do
28
+ @browser = Browser.new(:accept_language => "pt-br")
29
+ assert_equal ["pt-br"], @browser.accept_language
30
+ end
31
+
32
+ test "sets user agent while instantianting object" do
33
+ @browser = Browser.new(:ua => "Safari")
34
+ assert_equal "Safari", @browser.ua
35
+
36
+ @browser = Browser.new(:user_agent => "Chrome")
37
+ assert_equal "Chrome", @browser.ua
38
+ end
39
+
40
+ test "detects android" do
41
+ @browser.ua = $ua["ANDROID"]
42
+
43
+ assert_equal "Android", @browser.name
44
+ assert @browser.android?
45
+ refute @browser.safari?
46
+ assert @browser.webkit?
47
+ assert @browser.mobile?
48
+ refute @browser.tablet?
49
+ assert @browser.modern?
50
+ assert_equal "3.1.2", @browser.full_version
51
+ assert_equal "3", @browser.version
52
+ end
53
+
54
+ test "detects android tablet" do
55
+ @browser.ua = $ua["TABLOID"]
56
+
57
+ assert_equal "Android", @browser.name
58
+ assert @browser.android?
59
+ refute @browser.safari?
60
+ assert @browser.webkit?
61
+ refute @browser.mobile?
62
+ assert @browser.tablet?
63
+ assert @browser.modern?
64
+ assert_equal "4.0", @browser.full_version
65
+ assert_equal "4", @browser.version
66
+ end
67
+
68
+ test "detects surface tablet" do
69
+ @browser.ua = $ua["SURFACE"]
70
+
71
+ assert_equal "Internet Explorer", @browser.name
72
+ assert @browser.surface?
73
+ assert @browser.ie?
74
+ refute @browser.mobile?
75
+ assert @browser.tablet?
76
+ assert @browser.modern?
77
+ assert_equal "10.0", @browser.full_version
78
+ assert_equal "10", @browser.version
79
+ end
80
+
81
+ test "detects quicktime" do
82
+ @browser.ua = $ua["QUICKTIME"]
83
+
84
+ assert_equal "QuickTime", @browser.name
85
+ assert @browser.quicktime?
86
+ assert_equal "7.6.8", @browser.full_version
87
+ assert_equal "7", @browser.version
88
+ end
89
+
90
+ test "detects core media" do
91
+ @browser.ua = $ua["COREMEDIA"]
92
+
93
+ assert_equal "Apple CoreMedia", @browser.name
94
+ assert @browser.core_media?
95
+ assert_equal "1.0.0.10F569", @browser.full_version
96
+ assert_equal "1", @browser.version
97
+ end
98
+
99
+ test "detects phantom.js" do
100
+ @browser.ua = $ua["PHANTOM_JS"]
101
+
102
+ assert_equal "PhantomJS", @browser.name
103
+ assert @browser.phantom_js?
104
+ refute @browser.tablet?
105
+ refute @browser.mobile?
106
+ assert @browser.modern?
107
+ assert_equal "1.9.0", @browser.full_version
108
+ assert_equal "1", @browser.version
109
+ end
110
+
111
+ test "detects other mobiles" do
112
+ @browser.ua = "Symbian OS"
113
+ assert @browser.mobile?
114
+ refute @browser.tablet?
115
+
116
+ @browser.ua = "MIDP-2.0"
117
+ assert @browser.mobile?
118
+ refute @browser.tablet?
119
+ end
120
+
121
+ test "detects windows mobile" do
122
+ @browser.ua = $ua["WINDOWS_MOBILE"]
123
+
124
+ assert @browser.mobile?
125
+ assert @browser.windows?
126
+ assert @browser.windows_mobile?
127
+ refute @browser.windows_phone?
128
+ refute @browser.tablet?
129
+ end
130
+
131
+ test "returns a zero version" do
132
+ @browser.ua = "Bot"
133
+ assert_equal "0.0", @browser.full_version
134
+ assert_equal "0", @browser.version
135
+ end
136
+
137
+ test "sets meta" do
138
+ @browser.ua = $ua["CHROME"]
139
+ assert_kind_of Array, @browser.meta
140
+ end
141
+
142
+ test "returns string representation" do
143
+ @browser.ua = $ua["CHROME"]
144
+ meta = @browser.to_s
145
+
146
+ assert meta.include?("chrome")
147
+ assert meta.include?("webkit")
148
+ assert meta.include?("mac")
149
+ assert meta.include?("modern")
150
+ end
151
+
152
+ test "returns string representation for mobile" do
153
+ @browser.ua = $ua["BLACKBERRY"]
154
+ meta = @browser.to_s
155
+
156
+ assert meta.include?("blackberry")
157
+ assert meta.include?("blackberry4")
158
+ assert meta.include?("other")
159
+ assert meta.include?("mobile")
160
+ end
161
+
162
+ test "detects unknown id" do
163
+ @browser.ua = "Unknown"
164
+ assert_equal :other, @browser.id
165
+ end
166
+
167
+ test "detects unknown name" do
168
+ @browser.ua = "Unknown"
169
+ assert_equal "Other", @browser.name
170
+ end
171
+
172
+ test "detects mac platform" do
173
+ @browser.ua = "Mac OS X"
174
+ assert_equal :mac, @browser.platform
175
+ assert @browser.mac?
176
+ end
177
+
178
+ test "detects linux platform" do
179
+ @browser.ua = "Linux"
180
+ assert_equal :linux, @browser.platform
181
+ assert @browser.linux?
182
+ end
183
+
184
+ test "detects unknown platform" do
185
+ @browser.ua = "Unknown"
186
+ assert_equal :other, @browser.platform
187
+ end
188
+
189
+ test "returns all known languages" do
190
+ @browser.accept_language = "en-us,en;q=0.8,pt-br;q=0.5,pt;q=0.3"
191
+ assert_equal ["en-us", "en", "pt-br", "pt"], @browser.accept_language
192
+ end
193
+
194
+ test "detects xoom" do
195
+ @browser.ua = $ua["XOOM"]
196
+
197
+ assert @browser.android?
198
+ assert @browser.tablet?
199
+ refute @browser.mobile?
200
+ end
201
+
202
+ test "detects nexus tablet" do
203
+ @browser.ua = $ua["NEXUS_TABLET"]
204
+
205
+ assert @browser.android?
206
+ assert @browser.tablet?
207
+ refute @browser.mobile?
208
+ end
209
+
210
+ test "detects nook" do
211
+ @browser.ua = $ua["NOOK"]
212
+
213
+ assert @browser.tablet?
214
+ refute @browser.mobile?
215
+ end
216
+
217
+ test "detects samsung" do
218
+ @browser.ua = $ua["SAMSUNG"]
219
+
220
+ assert @browser.tablet?
221
+ refute @browser.mobile?
222
+ end
223
+
224
+ test "removes duplicate items" do
225
+ @browser.ua = $ua["SAFARI"]
226
+ assert_equal ["safari"], @browser.meta.select {|item| item == "safari" }
227
+ end
228
+
229
+ test "detects meta aliased as to_a" do
230
+ @browser.ua = $ua["SAFARI"]
231
+ assert_equal @browser.meta, @browser.to_a
232
+ end
233
+
234
+ test "detects tv" do
235
+ @browser.ua = $ua["SMART_TV"]
236
+ assert @browser.tv?
237
+ end
238
+
239
+ test "knows a supported browser" do
240
+ @browser.ua = "Chrome"
241
+ assert @browser.known?
242
+ end
243
+
244
+ test "does not know an unsupported browser" do
245
+ @browser.ua = "Fancy new browser"
246
+ refute @browser.known?
247
+ end
248
+ end