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 CHANGED
@@ -3,3 +3,4 @@
3
3
  coverage
4
4
  rdoc
5
5
  pkg
6
+ /.rake_tasks~
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 "shoulda", ">= 2.10.2"
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/testtask'
21
- Rake::TestTask.new(:test) do |test|
22
- test.libs << 'lib' << 'test'
23
- test.pattern = 'test/**/*_test.rb'
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
- begin
28
- require 'rcov/rcovtask'
29
- Rcov::RcovTask.new do |test|
30
- test.libs << 'test'
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 :test => :check_dependencies
33
+ task :spec => :check_dependencies
41
34
 
42
- task :default => :test
35
+ task :default => :spec
43
36
 
44
- require 'rake/rdoctask'
45
- Rake::RDocTask.new do |rdoc|
46
- if File.exist?('VERSION')
47
- version = File.read('VERSION')
48
- else
49
- version = ""
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.1.0
@@ -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.1"
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-02-04}
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
- "test/browser_detector/detector_test.rb",
33
- "test/browser_detector/rails/view_helpers_test.rb",
34
- "test/browser_detector_test.rb",
35
- "test/test_helper.rb"
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.5}
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
- "test/browser_detector/detector_test.rb",
44
- "test/browser_detector/rails/view_helpers_test.rb",
45
- "test/browser_detector_test.rb",
46
- "test/test_helper.rb"
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::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
- s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
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<shoulda>, [">= 2.10.2"])
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<shoulda>, [">= 2.10.2"])
62
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
63
+ s.add_dependency(%q<yard>, [">= 0"])
60
64
  end
61
65
  end
62
66
 
@@ -5,7 +5,7 @@ require 'browser_detector/detector'
5
5
  require 'browser_detector/rails/view_helpers'
6
6
 
7
7
  module BrowserDetector
8
- VERSION = '1.0.1'
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
- ua = @ua
62
- if ua.nil?
63
- 'unknown'
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
- if ua.index( 'msie' ) && !ua.index( 'opera' ) && !ua.index( 'webtv' )
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 = @version_regex.match( browser_version )
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 = @version_regex.match( browser_version )
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 = @version_regex.match( browser_version )
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 = @version_regex.match( browser_version )
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
- true
151
- end
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( chrome firefox ie55 ie60 ie70 ie80 konqueror netscape opera safari )
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( ie_ce4 iphone )
167
+ %w(ie_ce4)
163
168
  end
164
-
169
+
165
170
  def self.user_agents
166
- {
167
- :ie55 => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)',
168
- :ie60 => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
169
- :ie70 => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
170
- :ie80 => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
171
- :firefox2 => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.17) Gecko/20080829 Firefox/2.0.0.17',
172
- :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',
173
- :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',
174
- :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)',
175
- :opera10 => 'Opera/9.80 (Macintosh; Intel Mac OS X; U; en) Presto/2.2.15 Version/10.00',
176
- :safari403 => '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',
177
- :iphone3 => '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',
178
- :iphone2 => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A535b Safari/419.3',
179
- :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',
180
- :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'
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 = /.*msie (.*); windows nt 5.1/.match( @ua )
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 = /.*\((.*); u;.*firefox\/(.*) \(.net.*/.match( @ua )
193
- if match.nil?
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 = /.*\((.*); intel.*version\/(.*)/.match( @ua )
202
- return match.nil? ? '0' : match[2]
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 = /.* safari\/(.*)/.match( @ua )
207
- rev = match.nil? ? 0 : match[1]
208
- return case rev
209
- when '528.16' then '4.0.0'
210
- when '530.18' then '4.0.1'
211
- when '530.19' then '4.0.2'
212
- when '531.9', '532.2' then '4.0.3'
213
- else '0.0.0.0'
214
- end
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