browser_detector 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Rakefile +19 -31
- data/VERSION +1 -1
- data/browser_detector.gemspec +19 -15
- data/lib/browser_detector.rb +1 -1
- data/lib/browser_detector/detector.rb +111 -98
- data/lib/browser_detector/rails/view_helpers.rb +27 -0
- data/spec/browser_detector/detector_shared_specs.rb +158 -0
- data/spec/browser_detector/detector_spec.rb +475 -0
- data/spec/browser_detector_spec.rb +7 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +9 -0
- metadata +51 -18
- data/test/browser_detector/detector_test.rb +0 -1425
- data/test/browser_detector/rails/view_helpers_test.rb +0 -97
- data/test/browser_detector_test.rb +0 -7
- data/test/test_helper.rb +0 -27
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -10,47 +10,35 @@ begin
|
|
10
10
|
gem.email = "jason@lookforwardenterprises.com"
|
11
11
|
gem.homepage = "http://github.com/midas/browser_detector"
|
12
12
|
gem.authors = ["C. Jason Harrelson (midas)"]
|
13
|
-
gem.add_development_dependency "
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_development_dependency "yard", ">= 0"
|
14
15
|
end
|
15
16
|
Jeweler::GemcutterTasks.new
|
16
17
|
rescue LoadError
|
17
18
|
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
19
|
end
|
19
20
|
|
20
|
-
require 'rake/
|
21
|
-
Rake::
|
22
|
-
|
23
|
-
|
24
|
-
test.verbose = true
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
test.pattern = 'test/**/*_test.rb'
|
32
|
-
test.verbose = true
|
33
|
-
end
|
34
|
-
rescue LoadError
|
35
|
-
task :rcov do
|
36
|
-
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
-
end
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
38
31
|
end
|
39
32
|
|
40
|
-
task :
|
33
|
+
task :spec => :check_dependencies
|
41
34
|
|
42
|
-
task :default => :
|
35
|
+
task :default => :spec
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
37
|
+
begin
|
38
|
+
require 'yard'
|
39
|
+
YARD::Rake::YardocTask.new
|
40
|
+
rescue LoadError
|
41
|
+
task :yardoc do
|
42
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
50
43
|
end
|
51
|
-
|
52
|
-
rdoc.rdoc_dir = 'rdoc'
|
53
|
-
rdoc.title = "browser_detector #{version}"
|
54
|
-
rdoc.rdoc_files.include('README*')
|
55
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
-
end
|
44
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/browser_detector.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{browser_detector}
|
8
|
-
s.version = "1.0
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["C. Jason Harrelson (midas)"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-11}
|
13
13
|
s.description = %q{Determines the name and version of the browser currently making a request.}
|
14
14
|
s.email = %q{jason@lookforwardenterprises.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -29,34 +29,38 @@ Gem::Specification.new do |s|
|
|
29
29
|
"lib/browser_detector/detector.rb",
|
30
30
|
"lib/browser_detector/rails.rb",
|
31
31
|
"lib/browser_detector/rails/view_helpers.rb",
|
32
|
-
"
|
33
|
-
"
|
34
|
-
"
|
35
|
-
"
|
32
|
+
"spec/browser_detector/detector_shared_specs.rb",
|
33
|
+
"spec/browser_detector/detector_spec.rb",
|
34
|
+
"spec/browser_detector_spec.rb",
|
35
|
+
"spec/spec.opts",
|
36
|
+
"spec/spec_helper.rb"
|
36
37
|
]
|
37
38
|
s.homepage = %q{http://github.com/midas/browser_detector}
|
38
39
|
s.rdoc_options = ["--charset=UTF-8"]
|
39
40
|
s.require_paths = ["lib"]
|
40
|
-
s.rubygems_version = %q{1.3.
|
41
|
+
s.rubygems_version = %q{1.3.7}
|
41
42
|
s.summary = %q{Determines the name and version of the browser currently making a request.}
|
42
43
|
s.test_files = [
|
43
|
-
"
|
44
|
-
"
|
45
|
-
"
|
46
|
-
"
|
44
|
+
"spec/browser_detector/detector_shared_specs.rb",
|
45
|
+
"spec/browser_detector/detector_spec.rb",
|
46
|
+
"spec/browser_detector_spec.rb",
|
47
|
+
"spec/spec_helper.rb"
|
47
48
|
]
|
48
49
|
|
49
50
|
if s.respond_to? :specification_version then
|
50
51
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
52
|
s.specification_version = 3
|
52
53
|
|
53
|
-
if Gem::Version.new(Gem::
|
54
|
-
s.add_development_dependency(%q<
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
56
|
+
s.add_development_dependency(%q<yard>, [">= 0"])
|
55
57
|
else
|
56
|
-
s.add_dependency(%q<
|
58
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
59
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
57
60
|
end
|
58
61
|
else
|
59
|
-
s.add_dependency(%q<
|
62
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
63
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
data/lib/browser_detector.rb
CHANGED
@@ -5,7 +5,7 @@ require 'browser_detector/detector'
|
|
5
5
|
require 'browser_detector/rails/view_helpers'
|
6
6
|
|
7
7
|
module BrowserDetector
|
8
|
-
VERSION =
|
8
|
+
VERSION = File.read( "#{File.dirname __FILE__}/../VERSION" ).chomp
|
9
9
|
end
|
10
10
|
|
11
11
|
ActionView::Base.send( :include, BrowserDetector::Rails::ViewHelpers ) if defined?( ActionView )
|
@@ -1,17 +1,33 @@
|
|
1
|
-
module BrowserDetector
|
2
|
-
|
3
|
-
# The Detector provides the ability to determine browser information from the user
|
1
|
+
module BrowserDetector
|
2
|
+
# The Detector provides the ability to determine browser information from the user
|
4
3
|
# agent string.
|
5
4
|
#
|
6
5
|
class Detector
|
7
|
-
|
6
|
+
VERSION_REGEX = /(\d*)\.(\d*)\.*(\d*)\.*(\d*)/
|
7
|
+
CHROME_REGEX = /.* Chrome\/([\d|.]*).*/i
|
8
|
+
FIREFOX_REGEX = /.* Firefox\/([\d|.]*).*/i
|
9
|
+
IE_REGEX = /.*; MSIE ([\d|.]*);.*/i
|
10
|
+
OPERA_REGEX = /Opera.*Version\/([\d|.]*)/i
|
11
|
+
SAFARI_REGEX = /.*Gecko\) Version\/([\d|.]*) Safari\/[\d|.]*/i
|
12
|
+
SAFARI_MOBILE_REGEX = /.*Gecko\) Version\/([\d|.]*) Mobile\/.* Safari\/[\d|.]*/i
|
13
|
+
|
8
14
|
attr_reader :ua
|
9
|
-
|
15
|
+
|
10
16
|
def initialize( user_agent )
|
11
17
|
@ua = user_agent.downcase unless user_agent.nil?
|
12
|
-
@version_regex = /(\d*)\.(\d*)\.*(\d*)\.*(\d*)/
|
13
18
|
end
|
14
|
-
|
19
|
+
|
20
|
+
def self.patterns
|
21
|
+
{
|
22
|
+
CHROME_REGEX => 'chrome',
|
23
|
+
IE_REGEX => 'ie',
|
24
|
+
FIREFOX_REGEX => 'firefox',
|
25
|
+
OPERA_REGEX => 'opera',
|
26
|
+
SAFARI_REGEX => 'safari',
|
27
|
+
SAFARI_MOBILE_REGEX => 'safari_mobile'
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
15
31
|
# Returns true if the browser matches the options ent in, otherwise returns false.
|
16
32
|
#
|
17
33
|
# === Request
|
@@ -30,7 +46,7 @@ module BrowserDetector
|
|
30
46
|
minor_version = options[:minor_version]
|
31
47
|
build_version = options[:build_version]
|
32
48
|
revision_version = options[:revision_version]
|
33
|
-
|
49
|
+
|
34
50
|
name ||= self.browser_name
|
35
51
|
version ||= self.browser_version
|
36
52
|
major_version ||= self.browser_version_major
|
@@ -44,45 +60,29 @@ module BrowserDetector
|
|
44
60
|
minor_version = minor_version.to_s.strip
|
45
61
|
build_version = build_version.to_s.strip
|
46
62
|
revision_version = revision_version.to_s.strip
|
47
|
-
|
63
|
+
|
48
64
|
self.browser_name == name && self.browser_version == version && self.browser_version_major == major_version &&
|
49
|
-
self.browser_version_minor == minor_version #&& self.browser_version_build == build_version &&
|
65
|
+
self.browser_version_minor == minor_version #&& self.browser_version_build == build_version &&
|
50
66
|
#self.browser_version_revision == revision_version
|
51
67
|
end
|
52
68
|
|
53
69
|
# Returns the name of the browser that is making this request. For example 'ie'.
|
54
70
|
#
|
55
|
-
# === Request
|
56
|
-
# * +request+ - The request object.
|
57
|
-
#
|
58
71
|
def browser_name
|
59
72
|
begin
|
60
73
|
@browser_name ||= begin
|
61
|
-
|
62
|
-
|
63
|
-
|
74
|
+
return 'unknown' if ua.nil?
|
75
|
+
|
76
|
+
Detector.patterns.each do |pattern, identifier|
|
77
|
+
return identifier if pattern.match( ua )
|
78
|
+
end
|
79
|
+
|
80
|
+
if ua.include?( 'konqueror' )
|
81
|
+
'konqueror'
|
82
|
+
elsif ua.include?( 'netscape' )
|
83
|
+
'netscape'
|
64
84
|
else
|
65
|
-
|
66
|
-
if ua.index( 'windows ce' )
|
67
|
-
'ie' + '_ce' #+ ua[ua.index( 'msie' ) + 5].chr
|
68
|
-
else
|
69
|
-
'ie' # + ua[ua.index( 'msie' ) + 5].chr
|
70
|
-
end
|
71
|
-
elsif ua.include?( 'opera' )
|
72
|
-
'opera'
|
73
|
-
elsif ua.include?( 'konqueror' )
|
74
|
-
'konqueror'
|
75
|
-
elsif ua.include?( 'applewebkit/' )
|
76
|
-
'safari'
|
77
|
-
elsif ua.include?( 'chrome' )
|
78
|
-
'chrome'
|
79
|
-
elsif ua.include?( 'firefox' )
|
80
|
-
'firefox'
|
81
|
-
elsif ua.include?( 'netscape' )
|
82
|
-
'netscape'
|
83
|
-
else
|
84
|
-
'unknown'
|
85
|
-
end
|
85
|
+
'unknown'
|
86
86
|
end
|
87
87
|
end
|
88
88
|
rescue
|
@@ -92,9 +92,6 @@ module BrowserDetector
|
|
92
92
|
|
93
93
|
# Returns the version of the browser that is making this request. For example '7'.
|
94
94
|
#
|
95
|
-
# === Request
|
96
|
-
# * +request+ - The request object.
|
97
|
-
#
|
98
95
|
def browser_version
|
99
96
|
@browser_version ||= begin
|
100
97
|
self.send( "resolve_version_for_#{self.browser_name}".to_sym )
|
@@ -102,29 +99,29 @@ module BrowserDetector
|
|
102
99
|
end
|
103
100
|
|
104
101
|
def browser_version_major
|
105
|
-
match =
|
102
|
+
match = VERSION_REGEX.match( browser_version )
|
106
103
|
return match[1].to_i.to_s unless match.nil? || match.size < 2
|
107
104
|
'0'
|
108
105
|
end
|
109
106
|
|
110
107
|
def browser_version_minor
|
111
|
-
match =
|
108
|
+
match = VERSION_REGEX.match( browser_version )
|
112
109
|
return match[2].to_i.to_s unless match.nil? || match.size < 3
|
113
110
|
'0'
|
114
111
|
end
|
115
|
-
|
112
|
+
|
116
113
|
def browser_version_build
|
117
|
-
match =
|
114
|
+
match = VERSION_REGEX.match( browser_version )
|
118
115
|
return match[3].to_i.to_s unless match.nil? || match.size < 4 || match[3].empty? || match[3].nil?
|
119
116
|
'0'
|
120
117
|
end
|
121
|
-
|
118
|
+
|
122
119
|
def browser_version_revision
|
123
|
-
match =
|
120
|
+
match = VERSION_REGEX.match( browser_version )
|
124
121
|
return match[4].to_i.to_s unless match.nil? || match.size < 5 || match[4].empty? || match[4].nil?
|
125
122
|
'0'
|
126
123
|
end
|
127
|
-
|
124
|
+
|
128
125
|
# Returns the browser name concatenated with the browser version. for example, 'ie7'.
|
129
126
|
#
|
130
127
|
# === Request
|
@@ -133,7 +130,7 @@ module BrowserDetector
|
|
133
130
|
def browser_full_name
|
134
131
|
self.send( "browser_full_name_for_#{self.browser_name}".to_sym )
|
135
132
|
end
|
136
|
-
|
133
|
+
|
137
134
|
# Returns the browser name concatenated with the browser version. for example, 'ie7'.
|
138
135
|
#
|
139
136
|
# === Request
|
@@ -142,100 +139,116 @@ module BrowserDetector
|
|
142
139
|
def browser_id
|
143
140
|
browser_name + browser_version.gsub( /\./, '' )
|
144
141
|
end
|
145
|
-
|
142
|
+
|
146
143
|
# Returns true if the current browser type can handle PNGs.
|
147
144
|
#
|
148
145
|
def can_use_png?
|
149
146
|
return browser_version.to_i >= 7 if browser_name== 'ie'
|
150
|
-
|
151
|
-
|
147
|
+
true
|
148
|
+
end
|
149
|
+
|
150
|
+
def webkit?
|
151
|
+
@is_webkit ||= !(/AppleWebKit/i.match( @ua ).nil?)
|
152
|
+
end
|
153
|
+
|
154
|
+
def iphone?
|
155
|
+
@is_iphone ||= (browser_name == Detector.patterns[SAFARI_MOBILE_REGEX])
|
156
|
+
end
|
152
157
|
|
153
158
|
# A list of all the browser types that Guilded recognizes.
|
154
159
|
#
|
155
160
|
def self.all_browsers
|
156
|
-
%w(
|
161
|
+
%w(chrome firefox ie55 ie60 ie70 ie80 konqueror netscape opera safari)
|
157
162
|
end
|
158
163
|
|
159
164
|
# A list of all the mobile browser types that Guilded recognizes.
|
160
165
|
#
|
161
166
|
def self.all_mobile_browsers
|
162
|
-
%w(
|
167
|
+
%w(ie_ce4)
|
163
168
|
end
|
164
|
-
|
169
|
+
|
165
170
|
def self.user_agents
|
166
|
-
{
|
167
|
-
:ie55
|
168
|
-
:ie60
|
169
|
-
:ie70
|
170
|
-
:ie80
|
171
|
-
:firefox2
|
172
|
-
:firefox3
|
173
|
-
:firefox35
|
174
|
-
:firefox35win
|
175
|
-
:opera10
|
176
|
-
:
|
177
|
-
:
|
178
|
-
:
|
179
|
-
:
|
180
|
-
:
|
171
|
+
{
|
172
|
+
:ie55 => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)',
|
173
|
+
:ie60 => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
|
174
|
+
:ie70 => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
|
175
|
+
:ie80 => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
|
176
|
+
:firefox2 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.17) Gecko/20080829 Firefox/2.0.0.17',
|
177
|
+
:firefox3 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.0.11) Gecko/2009060214 Firefox/3.0.11',
|
178
|
+
:firefox35 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3',
|
179
|
+
:firefox35win => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)',
|
180
|
+
:opera10 => 'Opera/9.80 (Macintosh; Intel Mac OS X; U; en) Presto/2.2.15 Version/10.00',
|
181
|
+
:safari40 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16',
|
182
|
+
:safari501 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-us) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8',
|
183
|
+
:chrome3 => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.27 Safari/532.0',
|
184
|
+
:chrome4 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Chrome/4.0.221.8 Safari/532.2',
|
185
|
+
:safari_mobile4 => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7C144 Safari/528.16',
|
186
|
+
:safari_mobile3 => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A535b Safari/419.3',
|
181
187
|
}
|
182
188
|
end
|
183
|
-
|
189
|
+
|
184
190
|
protected
|
185
|
-
|
191
|
+
|
186
192
|
def resolve_version_for_ie
|
187
|
-
match =
|
193
|
+
match = IE_REGEX.match( @ua )
|
188
194
|
return match.nil? ? '0' : match[1]
|
189
195
|
end
|
190
|
-
|
196
|
+
|
191
197
|
def resolve_version_for_firefox
|
192
|
-
match =
|
193
|
-
|
194
|
-
match = /.*\((.*); u;.*firefox\/(.*)/.match( @ua )
|
195
|
-
return match.nil? ? '0' : match[2]
|
196
|
-
end
|
197
|
-
return match.nil? ? '0' : match[2]
|
198
|
+
match = FIREFOX_REGEX.match( ua )
|
199
|
+
match.nil? ? 0 : match[1]
|
198
200
|
end
|
199
|
-
|
201
|
+
|
200
202
|
def resolve_version_for_opera
|
201
|
-
match =
|
202
|
-
return match.nil? ? '0' : match[
|
203
|
+
match = OPERA_REGEX.match( @ua )
|
204
|
+
return match.nil? ? '0' : match[1]
|
203
205
|
end
|
204
|
-
|
206
|
+
|
205
207
|
def resolve_version_for_safari
|
206
|
-
match =
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
208
|
+
match = SAFARI_REGEX.match( @ua )
|
209
|
+
match.nil? ? 0 : match[1]
|
210
|
+
end
|
211
|
+
|
212
|
+
def resolve_version_for_chrome
|
213
|
+
match = CHROME_REGEX.match( @ua )
|
214
|
+
return match.nil? ? '0' : match[1]
|
215
|
+
end
|
216
|
+
|
217
|
+
def resolve_version_for_safari_mobile
|
218
|
+
match = SAFARI_MOBILE_REGEX.match( ua )
|
219
|
+
match.nil? ? 0 : match[1]
|
215
220
|
end
|
216
|
-
|
221
|
+
|
217
222
|
def resolve_version_for_unknown
|
218
223
|
'0'
|
219
224
|
end
|
220
|
-
|
225
|
+
|
221
226
|
def browser_full_name_for_ie
|
222
227
|
"Internet Explorer #{browser_version}"
|
223
228
|
end
|
224
|
-
|
229
|
+
|
225
230
|
def browser_full_name_for_firefox
|
226
231
|
"Firefox #{browser_version}"
|
227
232
|
end
|
228
|
-
|
233
|
+
|
229
234
|
def browser_full_name_for_opera
|
230
235
|
"Opera #{browser_version}"
|
231
236
|
end
|
232
|
-
|
237
|
+
|
233
238
|
def browser_full_name_for_safari
|
234
239
|
"Safari #{browser_version}"
|
235
240
|
end
|
236
|
-
|
241
|
+
|
242
|
+
def browser_full_name_for_safari_mobile
|
243
|
+
"Safari Mobile #{browser_version}"
|
244
|
+
end
|
245
|
+
|
237
246
|
def browser_full_name_for_unknown
|
238
247
|
""
|
239
248
|
end
|
249
|
+
|
250
|
+
def browser_full_name_for_chrome
|
251
|
+
"Chrome #{browser_version}"
|
252
|
+
end
|
240
253
|
end
|
241
254
|
end
|