browser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ = Browser
2
+
3
+ Do some browser detection with Ruby. Includes ActionController integration.
4
+
5
+ == Installation
6
+
7
+ gem install browser
8
+
9
+ == Usage
10
+
11
+ require "rubygems"
12
+ require "browser"
13
+
14
+ browser = Browser.new(:ua => "some string", :accept_language => "en-us")
15
+ browser.safari?
16
+ browser.opera?
17
+ browser.mobile?
18
+ browser.firefox?
19
+ browser.ie?
20
+ browser.ie6? # this goes up to 9
21
+ browser.capable? # supports some CSS 3
22
+ browser.platform # return :mac, :windows, :linux or :other
23
+ browser.mac?
24
+ browser.windows?
25
+ browser.linux?
26
+ browser.meta # an array with several attributes
27
+ browser.to_s # the meta info joined by space
28
+
29
+ See the tests for more examples.
30
+
31
+ === Rails integration
32
+
33
+ Just add it to the Gemfile or `environment.rb`, depending of your Rails version.
34
+
35
+ gem "browser" #=> Rails 3
36
+ config.gem "browser" #=> Rails 2
37
+
38
+ This adds a helper method called `browser`, that inspects your current user agent.
39
+
40
+ <% if browser.ie6? %>
41
+ <p class="disclaimer">Your're running an older IE version. Please update it!</p>
42
+ <% end %>
43
+
44
+ == Maintainer
45
+
46
+ * Nando Vieira - http://nandovieira.com.br
47
+
48
+ == License
49
+
50
+ (The MIT License)
51
+
52
+ Permission is hereby granted, free of charge, to any person obtaining
53
+ a copy of this software and associated documentation files (the
54
+ 'Software'), to deal in the Software without restriction, including
55
+ without limitation the rights to use, copy, modify, merge, publish,
56
+ distribute, sublicense, and/or sell copies of the Software, and to
57
+ permit persons to whom the Software is furnished to do so, subject to
58
+ the following conditions:
59
+
60
+ The above copyright notice and this permission notice shall be
61
+ included in all copies or substantial portions of the Software.
62
+
63
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
64
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
65
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
66
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
67
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
68
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
69
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ require "rcov/rcovtask"
2
+ require "rake/testtask"
3
+ require "rake/rdoctask"
4
+ require "lib/browser/version"
5
+
6
+ Rcov::RcovTask.new do |t|
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ t.rcov_opts = ["--sort coverage", "--exclude .gem"]
9
+
10
+ t.output_dir = "coverage"
11
+ t.libs << "test"
12
+ t.verbose = true
13
+ end
14
+
15
+ Rake::TestTask.new do |t|
16
+ t.libs << "lib"
17
+ t.libs << "test"
18
+ t.test_files = FileList["test/**/*_test.rb"]
19
+ t.verbose = true
20
+ t.ruby_opts = %w[-rubygems]
21
+ end
22
+
23
+ Rake::RDocTask.new do |rdoc|
24
+ rdoc.main = "README.rdoc"
25
+ rdoc.rdoc_dir = "doc"
26
+ rdoc.title = "Browser API"
27
+ rdoc.options += %w[ --line-numbers --inline-source --charset utf-8 ]
28
+ rdoc.rdoc_files.include("README.rdoc")
29
+ rdoc.rdoc_files.include("lib/**/*.rb")
30
+ end
31
+
32
+ begin
33
+ require "jeweler"
34
+
35
+ Jeweler::Tasks.new do |gem|
36
+ gem.name = "browser"
37
+ gem.email = "fnando.vieira@gmail.com"
38
+ gem.homepage = "http://github.com/fnando/browser"
39
+ gem.authors = ["Nando Vieira"]
40
+ gem.version = Browser::Version::STRING
41
+ gem.summary = "Do some browser detection with Ruby."
42
+ gem.files = FileList["README.rdoc", "{lib,test}/**/*", "Rakefile"]
43
+ end
44
+
45
+ Jeweler::GemcutterTasks.new
46
+ rescue LoadError => e
47
+ puts "You need to install jeweler to build this gem."
48
+ end
@@ -0,0 +1,319 @@
1
+ class Browser
2
+ # Add Rails helper if ActionController::Base is available
3
+ require "browser/action_controller" if defined?(ActionController::Base)
4
+
5
+ # Set browser's UA string.
6
+ attr_accessor :user_agent
7
+
8
+ # Set browser's preferred language
9
+ attr_writer :accept_language
10
+
11
+ alias :ua :user_agent
12
+ alias :ua= :user_agent=
13
+
14
+ NAMES = {
15
+ :android => "Android",
16
+ :blackberry => "BlackBerry",
17
+ :chrome => "Chrome",
18
+ :firefox => "Firefox",
19
+ :ie => "Internet Explorer",
20
+ :ipad => "iPad",
21
+ :iphone => "iPhone",
22
+ :ipod => "iPod Touch",
23
+ :opera => "Opera",
24
+ :other => "Other"
25
+ }
26
+
27
+ LANGUAGES = {
28
+ "af" => "Afrikaans",
29
+ "sq" => "Albanian",
30
+ "eu" => "Basque",
31
+ "bg" => "Bulgarian",
32
+ "be" => "Byelorussian",
33
+ "ca" => "Catalan",
34
+ "zh" => "Chinese",
35
+ "zh-cn" => "Chinese/China",
36
+ "zh-tw" => "Chinese/Taiwan",
37
+ "zh-hk" => "Chinese/Hong Kong",
38
+ "zh-sg" => "Chinese/singapore",
39
+ "hr" => "Croatian",
40
+ "cs" => "Czech",
41
+ "da" => "Danish",
42
+ "nl" => "Dutch",
43
+ "nl-nl" => "Dutch/Netherlands",
44
+ "nl-be" => "Dutch/Belgium",
45
+ "en" => "English",
46
+ "en-gb" => "English/United Kingdom",
47
+ "en-us" => "English/United States",
48
+ "en-au" => "English/Australian",
49
+ "en-ca" => "English/Canada",
50
+ "en-nz" => "English/New Zealand",
51
+ "en-ie" => "English/Ireland",
52
+ "en-za" => "English/South Africa",
53
+ "en-jm" => "English/Jamaica",
54
+ "en-bz" => "English/Belize",
55
+ "en-tt" => "English/Trinidad",
56
+ "et" => "Estonian",
57
+ "fo" => "Faeroese",
58
+ "fa" => "Farsi",
59
+ "fi" => "Finnish",
60
+ "fr" => "French",
61
+ "fr-be" => "French/Belgium",
62
+ "fr-fr" => "French/France",
63
+ "fr-ch" => "French/Switzerland",
64
+ "fr-ca" => "French/Canada",
65
+ "fr-lu" => "French/Luxembourg",
66
+ "gd" => "Gaelic",
67
+ "gl" => "Galician",
68
+ "de" => "German",
69
+ "de-at" => "German/Austria",
70
+ "de-de" => "German/Germany",
71
+ "de-ch" => "German/Switzerland",
72
+ "de-lu" => "German/Luxembourg",
73
+ "de-li" => "German/Liechtenstein",
74
+ "el" => "Greek",
75
+ "he" => "Hebrew",
76
+ "he-il" => "Hebrew/Israel",
77
+ "hi" => "Hindi",
78
+ "hu" => "Hungarian",
79
+ "ie-ee" => "Internet Explorer/Easter Egg",
80
+ "is" => "Icelandic",
81
+ "id" => "Indonesian",
82
+ "in" => "Indonesian",
83
+ "ga" => "Irish",
84
+ "it" => "Italian",
85
+ "it-ch" => "Italian/ Switzerland",
86
+ "ja" => "Japanese",
87
+ "km" => "Khmer",
88
+ "km-kh" => "Khmer/Cambodia",
89
+ "ko" => "Korean",
90
+ "lv" => "Latvian",
91
+ "lt" => "Lithuanian",
92
+ "mk" => "Macedonian",
93
+ "ms" => "Malaysian",
94
+ "mt" => "Maltese",
95
+ "no" => "Norwegian",
96
+ "pl" => "Polish",
97
+ "pt" => "Portuguese",
98
+ "pt-br" => "Portuguese/Brazil",
99
+ "rm" => "Rhaeto-Romanic",
100
+ "ro" => "Romanian",
101
+ "ro-mo" => "Romanian/Moldavia",
102
+ "ru" => "Russian",
103
+ "ru-mo" => "Russian /Moldavia",
104
+ "gd" => "Scots Gaelic",
105
+ "sr" => "Serbian",
106
+ "sk" => "Slovack",
107
+ "sl" => "Slovenian",
108
+ "sb" => "Sorbian",
109
+ "es" => "Spanish",
110
+ "es-do" => "Spanish",
111
+ "es-ar" => "Spanish/Argentina",
112
+ "es-co" => "Spanish/Colombia",
113
+ "es-mx" => "Spanish/Mexico",
114
+ "es-es" => "Spanish/Spain",
115
+ "es-gt" => "Spanish/Guatemala",
116
+ "es-cr" => "Spanish/Costa Rica",
117
+ "es-pa" => "Spanish/Panama",
118
+ "es-ve" => "Spanish/Venezuela",
119
+ "es-pe" => "Spanish/Peru",
120
+ "es-ec" => "Spanish/Ecuador",
121
+ "es-cl" => "Spanish/Chile",
122
+ "es-uy" => "Spanish/Uruguay",
123
+ "es-py" => "Spanish/Paraguay",
124
+ "es-bo" => "Spanish/Bolivia",
125
+ "es-sv" => "Spanish/El salvador",
126
+ "es-hn" => "Spanish/Honduras",
127
+ "es-ni" => "Spanish/Nicaragua",
128
+ "es-pr" => "Spanish/Puerto Rico",
129
+ "sx" => "Sutu",
130
+ "sv" => "Swedish",
131
+ "sv-se" => "Swedish/Sweden",
132
+ "sv-fi" => "Swedish/Finland",
133
+ "ts" => "Thai",
134
+ "tn" => "Tswana",
135
+ "tr" => "Turkish",
136
+ "uk" => "Ukrainian",
137
+ "ur" => "Urdu",
138
+ "vi" => "Vietnamese",
139
+ "xh" => "Xshosa",
140
+ "ji" => "Yiddish",
141
+ "zu" => "Zulu"
142
+ }
143
+
144
+ # Create a new browser instance and set
145
+ # the UA and Accept-Language headers.
146
+ #
147
+ # browser = Browser.new({
148
+ # :ua => "Safari",
149
+ # :accept_language => "pt-br"
150
+ # })
151
+ #
152
+ def initialize(options = {}, &block)
153
+ @user_agent = (options[:user_agent] || options[:ua]).to_s
154
+ @accept_language = options[:accept_language].to_s
155
+
156
+ yield self if block_given?
157
+ end
158
+
159
+ # Get readable browser name.
160
+ def name
161
+ NAMES[id]
162
+ end
163
+
164
+ # Return a symbol that identifies the browser.
165
+ def id
166
+ case
167
+ when chrome? then :chrome
168
+ when iphone? then :iphone
169
+ when ipad? then :ipad
170
+ when ipod? then :ipod
171
+ when ie? then :ie
172
+ when opera? then :opera
173
+ when firefox? then :firefox
174
+ when android? then :android
175
+ when blackberry? then :blackberry
176
+ else
177
+ :other
178
+ end
179
+ end
180
+
181
+ # Return an array with all preferred languages that this browser accepts.
182
+ def accept_language
183
+ @accept_language.gsub(/;q=[\d.]+/, "").split(",").collect {|l| l.downcase.gsub(/\s/m, "")}
184
+ end
185
+
186
+ # Return major version.
187
+ def version
188
+ full_version.to_s.split(".").first
189
+ end
190
+
191
+ # Return the full version.
192
+ def full_version
193
+ _, v = *ua.match(/(?:Version|MSIE|Opera|Firefox|Chrome|BlackBerry[^\/]+)[\/ ]([\d.]+)/)
194
+ v || "0.0"
195
+ end
196
+
197
+ # Return true if browser supports some CSS 3 (Safari, Firefox, Opera & IE7+).
198
+ def capable?
199
+ safari? || firefox? || opera? || (ie? && version >= "7")
200
+ end
201
+
202
+ # Detect if browser is mobile.
203
+ def mobile?
204
+ !!(ua =~ /(Mobile|Symbian|MIDP|Windows CE)/) || blackberry?
205
+ end
206
+
207
+ # Detect if browser is BlackBerry
208
+ def blackberry?
209
+ !!(ua =~ /BlackBerry/)
210
+ end
211
+
212
+ # Detect if browser is Android.
213
+ def android?
214
+ !!(ua =~ /Android/)
215
+ end
216
+
217
+ # Detect if browser is iPhone.
218
+ def iphone?
219
+ !!(ua =~ /iPhone/)
220
+ end
221
+
222
+ # Detect if browser is iPad.
223
+ def ipad?
224
+ !!(ua =~ /iPad/)
225
+ end
226
+
227
+ # Detect if browser is iPod.
228
+ def ipod?
229
+ !!(ua =~ /iPod/)
230
+ end
231
+
232
+ # Detect if browser is Safari.
233
+ def safari?
234
+ !!(ua =~ /Safari/)
235
+ end
236
+
237
+ # Detect if browser is Firefox.
238
+ def firefox?
239
+ !!(ua =~ /Firefox/)
240
+ end
241
+
242
+ # Detect if browser is Chrome.
243
+ def chrome?
244
+ !!(ua =~ /Chrome/)
245
+ end
246
+
247
+ # Detect if browser is Internet Explorer.
248
+ def ie?
249
+ !!(ua =~ /MSIE/ && ua !~ /Opera/)
250
+ end
251
+
252
+ # Detect if browser is Internet Explorer 6.
253
+ def ie6?
254
+ ie? && version == "6"
255
+ end
256
+
257
+ # Detect if browser is Internet Explorer 7.
258
+ def ie7?
259
+ ie? && version == "7"
260
+ end
261
+
262
+ # Detect if browser is Internet Explorer 8.
263
+ def ie8?
264
+ ie? && version == "8"
265
+ end
266
+
267
+ # Detect if browser is Internet Explorer 9.
268
+ def ie9?
269
+ ie? && version == "9"
270
+ end
271
+
272
+ # Detect if browser is Opera.
273
+ def opera?
274
+ !!(ua =~ /Opera/)
275
+ end
276
+
277
+ # Detect if current platform is Macintosh.
278
+ def mac?
279
+ !!(ua =~ /Mac OS X/)
280
+ end
281
+
282
+ # Detect if current platform is Windows.
283
+ def windows?
284
+ !!(ua =~ /Windows/)
285
+ end
286
+
287
+ # Detect if current platform is Linux flavor.
288
+ def linux?
289
+ !!(ua =~ /Linux/)
290
+ end
291
+
292
+ # Return the platform.
293
+ def platform
294
+ case
295
+ when linux? then :linux
296
+ when mac? then :mac
297
+ when windows? then :windows
298
+ else
299
+ :other
300
+ end
301
+ end
302
+
303
+ # Return a meta info about this browser.
304
+ def meta
305
+ Array.new.tap do |m|
306
+ m << id
307
+ m << "safari safari#{version}" if safari?
308
+ m << "#{id}#{version}" unless safari?
309
+ m << platform
310
+ m << "capable" if capable?
311
+ m << "mobile" if mobile?
312
+ end
313
+ end
314
+
315
+ # Return meta representation as string.
316
+ def to_s
317
+ meta.join(" ")
318
+ end
319
+ end
@@ -0,0 +1,17 @@
1
+ class Browser
2
+ module ActionController # :nodoc: all
3
+ def self.included(base)
4
+ base.send :helper_method, :browser
5
+ end
6
+
7
+ private
8
+ def browser
9
+ @browser ||= Browser.new(
10
+ :accept_language => request.headers["ACCEPT_LANGUAGE"],
11
+ :ua => request.headers["USER_AGENT"]
12
+ )
13
+ end
14
+ end
15
+ end
16
+
17
+ ActionController::Base.send :include, Browser::ActionController
@@ -0,0 +1,8 @@
1
+ class Browser
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,272 @@
1
+ require "test/unit"
2
+
3
+ begin
4
+ require "action_controller"
5
+ rescue LoadError => e
6
+ require "rubygems"
7
+ require "action_controller"
8
+ end
9
+
10
+ require "browser"
11
+
12
+
13
+ class BrowserTest < Test::Unit::TestCase
14
+ IPHONE = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3"
15
+ IPOD = "Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3"
16
+ IPAD = "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10"
17
+ IE6 = "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"
18
+ IE7 = "Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)"
19
+ IE8 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
20
+ IE9 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
21
+ OPERA = "Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9"
22
+ FIREFOX = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8"
23
+ CHROME = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4"
24
+ ANDROID = "Android SDK 1.5r3: Mozilla/5.0 (Linux; U; Android 1.5; de-; sdk Build/CUPCAKE) AppleWebkit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1"
25
+ BLACKBERRY = "BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103"
26
+
27
+ def setup
28
+ @browser = Browser.new
29
+ end
30
+
31
+ def test_yield_self_when_block_is_given
32
+ browser = nil
33
+ Browser.new {|b| browser = b }
34
+ assert_kind_of Browser, browser
35
+ end
36
+
37
+ def test_respond_to_ua_methods
38
+ assert @browser.respond_to?(:ua)
39
+ assert @browser.respond_to?(:ua=)
40
+ end
41
+
42
+ def test_delegate_ua_methods
43
+ @browser.user_agent = "Safari"
44
+ assert_equal "Safari", @browser.ua
45
+
46
+ @browser.ua = "Mozilla"
47
+ assert_equal "Mozilla", @browser.user_agent
48
+ end
49
+
50
+ def test_set_accept_language_while_instantiating_object
51
+ @browser = Browser.new(:accept_language => "pt-br")
52
+ assert_equal ["pt-br"], @browser.accept_language
53
+ end
54
+
55
+ def test_set_user_agent_while_instantianting_object
56
+ @browser = Browser.new(:ua => "Safari")
57
+ assert_equal "Safari", @browser.ua
58
+
59
+ @browser = Browser.new(:user_agent => "Chrome")
60
+ assert_equal "Chrome", @browser.ua
61
+ end
62
+
63
+ def test_detect_iphone
64
+ @browser.ua = IPHONE
65
+
66
+ assert_equal "iPhone", @browser.name
67
+ assert @browser.iphone?
68
+ assert @browser.safari?
69
+ assert @browser.mobile?
70
+ assert @browser.capable?
71
+ assert_equal "3.0", @browser.full_version
72
+ assert_equal "3", @browser.version
73
+ end
74
+
75
+ def test_detect_ipod
76
+ @browser.ua = IPOD
77
+
78
+ assert_equal "iPod Touch", @browser.name
79
+ assert @browser.ipod?
80
+ assert @browser.safari?
81
+ assert @browser.mobile?
82
+ assert @browser.capable?
83
+ assert_equal "3.0", @browser.full_version
84
+ assert_equal "3", @browser.version
85
+ end
86
+
87
+ def test_detect_ipad
88
+ @browser.ua = IPAD
89
+
90
+ assert_equal "iPad", @browser.name
91
+ assert @browser.ipad?
92
+ assert @browser.safari?
93
+ assert @browser.capable?
94
+ assert_equal "4.0.4", @browser.full_version
95
+ assert_equal "4", @browser.version
96
+ end
97
+
98
+ def test_detect_ie6
99
+ @browser.ua = IE6
100
+
101
+ assert_equal "Internet Explorer", @browser.name
102
+ assert @browser.ie?
103
+ assert @browser.ie6?
104
+ assert @browser.capable? == false
105
+ assert_equal "6.0", @browser.full_version
106
+ assert_equal "6", @browser.version
107
+ end
108
+
109
+ def test_detect_ie7
110
+ @browser.ua = IE7
111
+
112
+ assert_equal "Internet Explorer", @browser.name
113
+ assert @browser.ie?
114
+ assert @browser.ie7?
115
+ assert @browser.capable?
116
+ assert_equal "7.0", @browser.full_version
117
+ assert_equal "7", @browser.version
118
+ end
119
+
120
+ def test_detect_ie8
121
+ @browser.ua = IE8
122
+
123
+ assert_equal "Internet Explorer", @browser.name
124
+ assert @browser.ie?
125
+ assert @browser.ie8?
126
+ assert @browser.capable?
127
+ assert_equal "8.0", @browser.full_version
128
+ assert_equal "8", @browser.version
129
+ end
130
+
131
+ def test_detect_ie9
132
+ @browser.ua = IE9
133
+
134
+ assert_equal "Internet Explorer", @browser.name
135
+ assert @browser.ie?
136
+ assert @browser.ie9?
137
+ assert @browser.capable?
138
+ assert_equal "9.0", @browser.full_version
139
+ assert_equal "9", @browser.version
140
+ end
141
+
142
+ def test_detect_opera
143
+ @browser.ua = OPERA
144
+
145
+ assert_equal "Opera", @browser.name
146
+ assert @browser.opera?
147
+ assert @browser.capable?
148
+ assert_equal "9.99", @browser.full_version
149
+ assert_equal "9", @browser.version
150
+ end
151
+
152
+ def test_detect_firefox
153
+ @browser.ua = FIREFOX
154
+
155
+ assert_equal "Firefox", @browser.name
156
+ assert @browser.firefox?
157
+ assert @browser.capable?
158
+ assert_equal "3.8", @browser.full_version
159
+ assert_equal "3", @browser.version
160
+ end
161
+
162
+ def test_detect_chrome
163
+ @browser.ua = CHROME
164
+
165
+ assert_equal "Chrome", @browser.name
166
+ assert @browser.chrome?
167
+ assert @browser.safari?
168
+ assert @browser.capable?
169
+ assert_equal "5.0.375.99", @browser.full_version
170
+ assert_equal "5", @browser.version
171
+ end
172
+
173
+ def test_detect_android
174
+ @browser.ua = ANDROID
175
+
176
+ assert_equal "Android", @browser.name
177
+ assert @browser.android?
178
+ assert @browser.safari?
179
+ assert @browser.mobile?
180
+ assert @browser.capable?
181
+ assert_equal "3.1.2", @browser.full_version
182
+ assert_equal "3", @browser.version
183
+ end
184
+
185
+ def test_detect_blackberry
186
+ @browser.ua = BLACKBERRY
187
+
188
+ assert_equal "BlackBerry", @browser.name
189
+ assert @browser.blackberry?
190
+ assert @browser.mobile?
191
+ assert @browser.capable? == false
192
+ assert_equal "4.1.0", @browser.full_version
193
+ assert_equal "4", @browser.version
194
+ end
195
+
196
+ def test_detect_other_mobiles
197
+ @browser.ua = "Symbian OS"
198
+ assert @browser.mobile?
199
+
200
+ @browser.ua = "MIDP-2.0"
201
+ assert @browser.mobile?
202
+
203
+ @browser.ua = "Windows CE"
204
+ assert @browser.mobile?
205
+ end
206
+
207
+ def test_return_a_zero_version
208
+ @browser.ua = "Bot"
209
+ assert_equal "0.0", @browser.full_version
210
+ assert_equal "0", @browser.version
211
+ end
212
+
213
+ def test_meta
214
+ @browser.ua = CHROME
215
+ assert_kind_of Array, @browser.meta
216
+ end
217
+
218
+ def test_return_string_representation
219
+ @browser.ua = CHROME
220
+ assert_equal "chrome safari safari5 mac capable", @browser.to_s
221
+ end
222
+
223
+ def test_return_string_representation_for_mobile
224
+ @browser.ua = IPHONE
225
+ assert_equal "iphone safari safari3 mac capable mobile", @browser.to_s
226
+ end
227
+
228
+ def test_return_string_representation_for_handcap
229
+ @browser.ua = IE6
230
+ assert_equal "ie ie6 windows", @browser.to_s
231
+ end
232
+
233
+ def test_detect_unknown_id
234
+ @browser.ua = "Unknown"
235
+ assert_equal :other, @browser.id
236
+ end
237
+
238
+ def test_detect_unknown_name
239
+ @browser.ua = "Unknown"
240
+ assert_equal "Other", @browser.name
241
+ end
242
+
243
+ def test_detect_mac_platform
244
+ @browser.ua = "Mac OS X"
245
+ assert_equal :mac, @browser.platform
246
+ end
247
+
248
+ def test_detect_windows_platform
249
+ @browser.ua = "Windows"
250
+ assert_equal :windows, @browser.platform
251
+ end
252
+
253
+ def test_detect_linux_platform
254
+ @browser.ua = "Linux"
255
+ assert_equal :linux, @browser.platform
256
+ end
257
+
258
+ def test_detect_unknown_platform
259
+ @browser.ua = "Unknown"
260
+ assert_equal :other, @browser.platform
261
+ end
262
+
263
+ def test_return_all_known_languages
264
+ @browser.accept_language = "en-us,en;q=0.8,pt-br;q=0.5,pt;q=0.3"
265
+ assert_equal ["en-us", "en", "pt-br", "pt"], @browser.accept_language
266
+ end
267
+
268
+ def test_pimp_action_controller
269
+ methods = ActionController::Base.private_instance_methods.collect {|m| m.to_sym}
270
+ assert methods.include?(:browser)
271
+ end
272
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: browser
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Nando Vieira
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-16 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: fnando.vieira@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - README.rdoc
32
+ - Rakefile
33
+ - lib/browser.rb
34
+ - lib/browser/action_controller.rb
35
+ - lib/browser/version.rb
36
+ - test/browser_test.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/fnando/browser
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Do some browser detection with Ruby.
71
+ test_files:
72
+ - test/browser_test.rb