which_browser 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +8 -0
- data/LICENSE +27 -0
- data/README.md +28 -9
- data/lib/which_browser/version.rb +1 -1
- data/lib/which_browser.rb +9 -3
- data/test/dummy/app.rb +33 -31
- data/test/helper.rb +5 -12
- data/test/test_chrome.rb +2 -2
- data/test/test_firefox.rb +2 -2
- data/test/test_internet_explorer.rb +2 -2
- data/test/test_os.rb +25 -0
- data/test/test_safari.rb +2 -2
- data/which_browser.gemspec +6 -5
- metadata +34 -17
- data/test/test_requests.rb +0 -10
data/.travis.yml
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2011 Spencer Steffen and Citrus Media Group.
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
* Neither the name of Citrus Media Group nor the names of its
|
15
|
+
contributors may be used to endorse or promote products derived from this
|
16
|
+
software without specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
19
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
22
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
23
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
24
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
25
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
27
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
Which Browser
|
2
|
-
|
1
|
+
# Which Browser [![Build Status](https://secure.travis-ci.org/citrus/which_browser.png)](http://travis-ci.org/citrus/which_browser)
|
2
|
+
|
3
|
+
### Check for user agents like a boss!
|
4
|
+
|
3
5
|
|
4
6
|
Which Browser adds a few helper methods to a rack request so you can write cleaner browser-related logic.
|
5
7
|
|
@@ -7,6 +9,7 @@ For example, the age old battle against IE 5 & 6 can be handled like so:
|
|
7
9
|
|
8
10
|
stylesheet_link_tag "ie" if request.ie_lt7?
|
9
11
|
|
12
|
+
|
10
13
|
And for a nice browser warning:
|
11
14
|
|
12
15
|
- if request.ie6?
|
@@ -17,8 +20,7 @@ And for a nice browser warning:
|
|
17
20
|
|
18
21
|
Or in your rails controller, decide which layout:
|
19
22
|
|
20
|
-
render :layout => request.
|
21
|
-
|
23
|
+
render :layout => request.mobile? ? 'mobile' : 'application'
|
22
24
|
|
23
25
|
|
24
26
|
Here's some other popular helpers:
|
@@ -28,29 +30,46 @@ Here's some other popular helpers:
|
|
28
30
|
request.ie_lt6?
|
29
31
|
request.ie_gte7?
|
30
32
|
|
33
|
+
request.mobile?
|
31
34
|
request.ipad?
|
32
35
|
request.ipod?
|
33
36
|
request.iphone?
|
34
37
|
request.mobile_safari?
|
35
38
|
|
36
39
|
|
40
|
+
Installation
|
41
|
+
------------
|
42
|
+
|
43
|
+
As usual, just use the `gem install` command:
|
44
|
+
|
45
|
+
(sudo) gem install which_browser
|
46
|
+
|
47
|
+
Or add which_browser as a gem in your Gemfile:
|
48
|
+
|
49
|
+
gem 'which_browser', '>= 0.2.0'
|
50
|
+
|
51
|
+
Then run `bundle install`
|
52
|
+
|
37
53
|
|
38
54
|
Testing
|
39
55
|
-------
|
40
56
|
|
41
|
-
|
57
|
+
Tests can be run with:
|
42
58
|
|
43
59
|
git clone git://github.com/citrus/which_browser.git
|
44
|
-
|
60
|
+
cd which_browser
|
61
|
+
bundle
|
62
|
+
bundle exec rake
|
45
63
|
|
46
|
-
There is also a
|
64
|
+
There is also a sinatra app in `test/dummy` for a demo and real-life testing. Install sinatra (`gem install sinatra`) if you don't have it, then run:
|
47
65
|
|
66
|
+
cd which_browser
|
48
67
|
ruby test/dummy/app.rb
|
49
68
|
|
50
|
-
Now open your browser to [http://localhost:
|
69
|
+
Now open your browser to [http://localhost:4567](http://localhost:4567) and fiddle with your user agent.
|
51
70
|
|
52
71
|
|
53
72
|
License
|
54
73
|
-------
|
55
74
|
|
56
|
-
Copyright (c) 2011 Spencer Steffen, released under the New BSD License All rights reserved.
|
75
|
+
Copyright (c) 2011 Spencer Steffen and Citrus, released under the New BSD License All rights reserved.
|
data/lib/which_browser.rb
CHANGED
@@ -15,6 +15,10 @@ module WhichBrowser
|
|
15
15
|
test_user_agent(/Linux/)
|
16
16
|
end
|
17
17
|
|
18
|
+
def mobile?
|
19
|
+
test_user_agent(/Mobile|webOS/)
|
20
|
+
end
|
21
|
+
|
18
22
|
|
19
23
|
# =============================================
|
20
24
|
# iOS Methods
|
@@ -74,6 +78,8 @@ module WhichBrowser
|
|
74
78
|
end
|
75
79
|
|
76
80
|
|
81
|
+
|
82
|
+
|
77
83
|
private
|
78
84
|
|
79
85
|
def method_missing(method, *args, &block)
|
@@ -132,10 +138,10 @@ module WhichBrowser
|
|
132
138
|
end
|
133
139
|
|
134
140
|
def extract_version_with(regex)
|
135
|
-
v =
|
136
|
-
0 < v ? v : false
|
141
|
+
v = self.user_agent.to_s.match(regex)[1].to_f rescue 0
|
142
|
+
0 < v ? v : false
|
137
143
|
end
|
138
144
|
|
139
145
|
end
|
140
146
|
|
141
|
-
Rack::Request.send(:include, WhichBrowser)
|
147
|
+
Rack::Request.send(:include, WhichBrowser)
|
data/test/dummy/app.rb
CHANGED
@@ -1,34 +1,36 @@
|
|
1
|
-
|
2
|
-
require '
|
1
|
+
begin
|
2
|
+
require 'rubygems'
|
3
|
+
require 'sinatra'
|
4
|
+
rescue LoadError => e
|
5
|
+
puts "Please install sinatra before running this demo."
|
6
|
+
puts "Try: `gem install sinatra`"
|
7
|
+
end
|
3
8
|
|
4
9
|
require File.expand_path("../../../lib/which_browser", __FILE__)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
10
|
+
|
11
|
+
get "/" do
|
12
|
+
# add other methods you'd like to see to the list:
|
13
|
+
[
|
14
|
+
:user_agent,
|
15
|
+
:ie?,
|
16
|
+
:ie6?,
|
17
|
+
:ie_lt6?,
|
18
|
+
:ie_lte7?,
|
19
|
+
:ie_gte6?,
|
20
|
+
:safari?,
|
21
|
+
:safari_version,
|
22
|
+
:mobile_safari?,
|
23
|
+
:ff?,
|
24
|
+
:ff_version,
|
25
|
+
:opera?,
|
26
|
+
:chrome?,
|
27
|
+
:chrome?,
|
28
|
+
:pc?,
|
29
|
+
:mac?,
|
30
|
+
:linux?,
|
31
|
+
:mobile?
|
32
|
+
].collect{ |method|
|
33
|
+
v = request.send(method)
|
34
|
+
%(#{method} <span style="color: #{v == false ? 'red' : 'green' }; font-weight: #{v == false ? 'normal' : 'bold'}">#{v}</span>)
|
35
|
+
}.join("<br/>")
|
32
36
|
end
|
33
|
-
|
34
|
-
Ramaze.start(:mode => :dev)
|
data/test/helper.rb
CHANGED
@@ -1,15 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require 'shoulda'
|
7
|
-
require 'which_browser'
|
8
|
-
rescue LoadError => e
|
9
|
-
puts "Load error!"
|
10
|
-
puts e.inspect
|
11
|
-
exit
|
12
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'minitest/should'
|
4
|
+
require 'rack'
|
5
|
+
require 'which_browser'
|
13
6
|
|
14
7
|
USER_AGENTS = {}
|
15
8
|
|
data/test/test_chrome.rb
CHANGED
data/test/test_firefox.rb
CHANGED
data/test/test_os.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class TestOS < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
should "be on mac" do
|
6
|
+
request = get_request(:Chrome, '8.0.552.237')
|
7
|
+
assert request.mac?
|
8
|
+
end
|
9
|
+
|
10
|
+
should "be on pc" do
|
11
|
+
request = get_request(:IE, '6.0')
|
12
|
+
assert request.pc?
|
13
|
+
end
|
14
|
+
|
15
|
+
should "be on linux" do
|
16
|
+
request = get_request(:Chrome, '4.0.202.2')
|
17
|
+
assert request.linux?
|
18
|
+
end
|
19
|
+
|
20
|
+
should "be on mobile" do
|
21
|
+
request = get_request(:MobileSafari, 'iPhone')
|
22
|
+
assert request.mobile?
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/test/test_safari.rb
CHANGED
data/which_browser.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Spencer Steffen"]
|
10
10
|
s.email = ["spencer@citrusme.com"]
|
11
|
-
s.homepage = ""
|
12
|
-
s.summary = %q{Which Browser adds a few helper methods to a rack request.}
|
13
|
-
s.description = %q{Which Browser adds a few helper methods to a rack request.}
|
11
|
+
s.homepage = "https://github.com/citrus/which_browser"
|
12
|
+
s.summary = %q{Which Browser adds a few helper methods to a rack request making it easier to determine which browser is being used by the client.}
|
13
|
+
s.description = %q{Which Browser adds a few helper methods to a rack request making it easier to determine which browser is being used by the client.}
|
14
14
|
|
15
15
|
s.files = `git ls-files`.split("\n")
|
16
16
|
s.test_files = `git ls-files -- test/*`.split("\n")
|
@@ -19,7 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
|
20
20
|
s.add_dependency('rack', '>= 1.0.1')
|
21
21
|
|
22
|
-
s.add_development_dependency('
|
23
|
-
s.add_development_dependency('
|
22
|
+
s.add_development_dependency('rake', '>= 0.8.7')
|
23
|
+
s.add_development_dependency('minitest', '>= 2.1.0')
|
24
|
+
s.add_development_dependency('minitest_should', '>= 0.1.1')
|
24
25
|
|
25
26
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: which_browser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Spencer Steffen
|
@@ -10,12 +10,10 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02
|
14
|
-
default_executable:
|
13
|
+
date: 2011-12-02 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: rack
|
18
|
-
prerelease: false
|
19
17
|
requirement: &id001 !ruby/object:Gem::Requirement
|
20
18
|
none: false
|
21
19
|
requirements:
|
@@ -23,30 +21,42 @@ dependencies:
|
|
23
21
|
- !ruby/object:Gem::Version
|
24
22
|
version: 1.0.1
|
25
23
|
type: :runtime
|
24
|
+
prerelease: false
|
26
25
|
version_requirements: *id001
|
27
26
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
29
|
-
prerelease: false
|
27
|
+
name: rake
|
30
28
|
requirement: &id002 !ruby/object:Gem::Requirement
|
31
29
|
none: false
|
32
30
|
requirements:
|
33
31
|
- - ">="
|
34
32
|
- !ruby/object:Gem::Version
|
35
|
-
version:
|
33
|
+
version: 0.8.7
|
36
34
|
type: :development
|
35
|
+
prerelease: false
|
37
36
|
version_requirements: *id002
|
38
37
|
- !ruby/object:Gem::Dependency
|
39
|
-
name:
|
40
|
-
prerelease: false
|
38
|
+
name: minitest
|
41
39
|
requirement: &id003 !ruby/object:Gem::Requirement
|
42
40
|
none: false
|
43
41
|
requirements:
|
44
42
|
- - ">="
|
45
43
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
44
|
+
version: 2.1.0
|
47
45
|
type: :development
|
46
|
+
prerelease: false
|
48
47
|
version_requirements: *id003
|
49
|
-
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: minitest_should
|
50
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 0.1.1
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *id004
|
59
|
+
description: Which Browser adds a few helper methods to a rack request making it easier to determine which browser is being used by the client.
|
50
60
|
email:
|
51
61
|
- spencer@citrusme.com
|
52
62
|
executables: []
|
@@ -57,7 +67,9 @@ extra_rdoc_files: []
|
|
57
67
|
|
58
68
|
files:
|
59
69
|
- .gitignore
|
70
|
+
- .travis.yml
|
60
71
|
- Gemfile
|
72
|
+
- LICENSE
|
61
73
|
- README.md
|
62
74
|
- Rakefile
|
63
75
|
- lib/which_browser.rb
|
@@ -67,11 +79,10 @@ files:
|
|
67
79
|
- test/test_chrome.rb
|
68
80
|
- test/test_firefox.rb
|
69
81
|
- test/test_internet_explorer.rb
|
70
|
-
- test/
|
82
|
+
- test/test_os.rb
|
71
83
|
- test/test_safari.rb
|
72
84
|
- which_browser.gemspec
|
73
|
-
|
74
|
-
homepage: ""
|
85
|
+
homepage: https://github.com/citrus/which_browser
|
75
86
|
licenses: []
|
76
87
|
|
77
88
|
post_install_message:
|
@@ -84,25 +95,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
95
|
requirements:
|
85
96
|
- - ">="
|
86
97
|
- !ruby/object:Gem::Version
|
98
|
+
hash: -4551958323863309221
|
99
|
+
segments:
|
100
|
+
- 0
|
87
101
|
version: "0"
|
88
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
103
|
none: false
|
90
104
|
requirements:
|
91
105
|
- - ">="
|
92
106
|
- !ruby/object:Gem::Version
|
107
|
+
hash: -4551958323863309221
|
108
|
+
segments:
|
109
|
+
- 0
|
93
110
|
version: "0"
|
94
111
|
requirements: []
|
95
112
|
|
96
113
|
rubyforge_project:
|
97
|
-
rubygems_version: 1.5
|
114
|
+
rubygems_version: 1.8.5
|
98
115
|
signing_key:
|
99
116
|
specification_version: 3
|
100
|
-
summary: Which Browser adds a few helper methods to a rack request.
|
117
|
+
summary: Which Browser adds a few helper methods to a rack request making it easier to determine which browser is being used by the client.
|
101
118
|
test_files:
|
102
119
|
- test/dummy/app.rb
|
103
120
|
- test/helper.rb
|
104
121
|
- test/test_chrome.rb
|
105
122
|
- test/test_firefox.rb
|
106
123
|
- test/test_internet_explorer.rb
|
107
|
-
- test/
|
124
|
+
- test/test_os.rb
|
108
125
|
- test/test_safari.rb
|