spob_browser_detector 1.0.0 → 1.0.1

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ .idea
7
+ *.gemspec
8
+ *.gem
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ = 1.0.0 2009-10-19
2
+
3
+ * Initial release.
4
+ * Moved browser detector out of the Guilded framework to its own Gem.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "spob_browser_detector"
8
+ gem.summary = %Q{Determines the name and version of the browser currently making a request.}
9
+ gem.description = %Q{Determines the name and version of the browser currently making a request. I forked this because gemcutter did not appear to have the latest version of the gem.}
10
+ gem.email = "jason@lookforwardenterprises.com"
11
+ gem.homepage = "http://github.com/midas/browser_detector"
12
+ gem.authors = ["C. Jason Harrelson (midas)"]
13
+ gem.add_development_dependency "shoulda", ">= 2.10.2"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
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
25
+ end
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
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ 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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
@@ -0,0 +1,237 @@
1
+ module SpobBrowserDetector
2
+
3
+ # The Detector provides the ability to determine browser information from the user
4
+ # agent string.
5
+ #
6
+ class Detector
7
+
8
+ attr_reader :ua
9
+
10
+ def initialize( user_agent )
11
+ @ua = user_agent.downcase unless user_agent.nil?
12
+ @version_regex = /(\d*)\.(\d*)\.*(\d*)\.*(\d*)/
13
+ end
14
+
15
+ # Returns true if the browser matches the options ent in, otherwise returns false.
16
+ #
17
+ # === Request
18
+ # * +request+ - The request object.
19
+ #
20
+ # === Options
21
+ # * +:name+ - The name of the browser. For example 'ie' or :ie.
22
+ # * +:version+ - The version of the browser. For example '3.0.5'.
23
+ # * +:major_version+ - The major version of the browser. For example '3' or 3.
24
+ # * +:minor_version+ - The minor version of the browser. For example '0' or 0.
25
+ #
26
+ def browser_is?( options={} )
27
+ name = options[:name]
28
+ version = options[:version]
29
+ major_version = options[:major_version]
30
+ minor_version = options[:minor_version]
31
+ build_version = options[:build_version]
32
+ revision_version = options[:revision_version]
33
+
34
+ name ||= self.browser_name
35
+ version ||= self.browser_version
36
+ major_version ||= self.browser_version_major
37
+ minor_version ||= self.browser_version_minor
38
+ build_version ||= self.browser_version_build
39
+ revision_version ||= self.browser_version_revision
40
+
41
+ name = name.to_s.strip
42
+ version = version.to_s.strip
43
+ major_version = major_version.to_s.strip
44
+ minor_version = minor_version.to_s.strip
45
+ build_version = build_version.to_s.strip
46
+ revision_version = revision_version.to_s.strip
47
+
48
+ 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 &&
50
+ #self.browser_version_revision == revision_version
51
+ end
52
+
53
+ # Returns the name of the browser that is making this request. For example 'ie'.
54
+ #
55
+ # === Request
56
+ # * +request+ - The request object.
57
+ #
58
+ def browser_name
59
+ begin
60
+ @browser_name ||= begin
61
+ ua = @ua
62
+ if ua.nil?
63
+ 'unknown'
64
+ 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
86
+ end
87
+ end
88
+ rescue
89
+ 'unknown'
90
+ end
91
+ end
92
+
93
+ # Returns the version of the browser that is making this request. For example '7'.
94
+ #
95
+ # === Request
96
+ # * +request+ - The request object.
97
+ #
98
+ def browser_version
99
+ @browser_version ||= begin
100
+ self.send( "resolve_version_for_#{self.browser_name}".to_sym )
101
+ end
102
+ end
103
+
104
+ def browser_version_major
105
+ match = @version_regex.match( browser_version )
106
+ return match[1].to_i.to_s unless match.nil? || match.size < 2
107
+ '0'
108
+ end
109
+
110
+ def browser_version_minor
111
+ match = @version_regex.match( browser_version )
112
+ return match[2].to_i.to_s unless match.nil? || match.size < 3
113
+ '0'
114
+ end
115
+
116
+ def browser_version_build
117
+ match = @version_regex.match( browser_version )
118
+ return match[3].to_i.to_s unless match.nil? || match.size < 4 || match[3].empty? || match[3].nil?
119
+ '0'
120
+ end
121
+
122
+ def browser_version_revision
123
+ match = @version_regex.match( browser_version )
124
+ return match[4].to_i.to_s unless match.nil? || match.size < 5 || match[4].empty? || match[4].nil?
125
+ '0'
126
+ end
127
+
128
+ # Returns the browser name concatenated with the browser version. for example, 'ie7'.
129
+ #
130
+ # === Request
131
+ # * +request+ - The request object.
132
+ #
133
+ def browser_full_name
134
+ self.send( "browser_full_name_for_#{self.browser_name}".to_sym )
135
+ end
136
+
137
+ # Returns the browser name concatenated with the browser version. for example, 'ie7'.
138
+ #
139
+ # === Request
140
+ # * +request+ - The request object.
141
+ #
142
+ def browser_id
143
+ browser_name + browser_version.gsub( /\./, '' )
144
+ end
145
+
146
+ # Returns true if the current browser type can handle PNGs.
147
+ #
148
+ def can_use_png?
149
+ return browser_version.to_i >= 7 if browser_name== 'ie'
150
+ true
151
+ end
152
+
153
+ # A list of all the browser types that Guilded recognizes.
154
+ #
155
+ def self.all_browsers
156
+ %w( chrome firefox ie55 ie60 ie70 ie80 konqueror netscape opera safari )
157
+ end
158
+
159
+ # A list of all the mobile browser types that Guilded recognizes.
160
+ #
161
+ def self.all_mobile_browsers
162
+ %w( ie_ce4 iphone )
163
+ end
164
+
165
+ 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'
181
+ }
182
+ end
183
+
184
+ protected
185
+
186
+ def resolve_version_for_unknown
187
+ '0'
188
+ end
189
+
190
+ def resolve_version_for_ie
191
+ match = /.*msie (.*); windows nt 5.1/.match( @ua )
192
+ return match.nil? ? '0' : match[1]
193
+ end
194
+
195
+ def resolve_version_for_firefox
196
+ match = /.*\((.*); u;.*firefox\/(.*) \(.net.*/.match( @ua )
197
+ if match.nil?
198
+ match = /.*\((.*); u;.*firefox\/(.*)/.match( @ua )
199
+ return match.nil? ? '0' : match[2]
200
+ end
201
+ return match.nil? ? '0' : match[2]
202
+ end
203
+
204
+ def resolve_version_for_opera
205
+ match = /.*\((.*); intel.*version\/(.*)/.match( @ua )
206
+ return match.nil? ? '0' : match[2]
207
+ end
208
+
209
+ def resolve_version_for_safari
210
+ match = /.* safari\/(.*)/.match( @ua )
211
+ rev = match.nil? ? 0 : match[1]
212
+ return case rev
213
+ when '528.16' then '4.0.0'
214
+ when '530.18' then '4.0.1'
215
+ when '530.19' then '4.0.2'
216
+ when '531.9', '532.2' then '4.0.3'
217
+ else '0.0.0.0'
218
+ end
219
+ end
220
+
221
+ def browser_full_name_for_ie
222
+ "Internet Explorer #{browser_version}"
223
+ end
224
+
225
+ def browser_full_name_for_firefox
226
+ "Firefox #{browser_version}"
227
+ end
228
+
229
+ def browser_full_name_for_opera
230
+ "Opera #{browser_version}"
231
+ end
232
+
233
+ def browser_full_name_for_safari
234
+ "Safari #{browser_version}"
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,61 @@
1
+ module SpobBrowserDetector
2
+ module Rails
3
+ module ViewHelpers
4
+
5
+ # Returns the name of the browser that is making this request. For example 'ie'. When using
6
+ # the browser detector in the contoller you may put a :g_browser_detector variabe in the session if
7
+ # you wish to keep the BrowserDetector from being instantiated more than once per request.
8
+ #
9
+ def g_browser_name
10
+ setup_detector
11
+ @g_browser_detector.browser_name
12
+ end
13
+
14
+ # Returns the version of the browser that is making this request. For example '7'. When using
15
+ # the browser detector in the contoller you may put a :g_browser_detector variabe in the session if
16
+ # you wish to keep the BrowserDetector from being instantiated more than once per request.
17
+ #
18
+ def g_browser_version
19
+ setup_detector
20
+ @g_browser_detector.browser_version
21
+ end
22
+
23
+ # Returns the full browser name. For example, 'Internet Explorer 7.0'.
24
+ #
25
+ def g_browser_full_name
26
+ setup_detector
27
+ @g_browser_detector.browser_full_name
28
+ end
29
+
30
+ # Returns the browser name concatenated with the browser version. for example, 'ie7'. When using
31
+ # the browser detector in the contoller you may put a :g_browser_detector variabe in the session if
32
+ # you wish to keep the BrowserDetector from being instantiated more than once per request.
33
+ #
34
+ def g_browser_id
35
+ setup_detector
36
+ @g_browser_detector.browser_id
37
+ end
38
+
39
+ # Returns true if the browser matches the options ent in, otherwise returns false. When using
40
+ # the browser detector in the contoller you may put a :g_browser_detector variable in the session if
41
+ # you wish to keep the Detector from being instantiated more than once per request.
42
+ #
43
+ # === Options
44
+ # * +:name+ - The name of the browser. For example 'ie'.
45
+ # * +:version+ - The version of the browser. For example '7'.
46
+ #
47
+ def g_browser_is?( options={} )
48
+ setup_detector
49
+ @g_browser_detector.browser_is?( options )
50
+ end
51
+
52
+ protected
53
+
54
+ def setup_detector
55
+ @g_browser_detector = session[:g_browser_detector] if respond_to?( :session )
56
+ @g_browser_detector ||= SpobBrowserDetector::Detector.new( request.env['HTTP_USER_AGENT'] )
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ module SpobBrowserDetector
2
+ module Rails
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'spob_browser_detector/detector'
5
+ require 'spob_browser_detector/rails/view_helpers'
6
+
7
+ module SpobBrowserDetector
8
+ VERSION = '1.0.0'
9
+ end
10
+
11
+ ActionView::Base.send( :include, SpobBrowserDetector::Rails::ViewHelpers ) if defined?( ActionView )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spob_browser_detector
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - C. Jason Harrelson (midas)
@@ -32,8 +32,21 @@ extra_rdoc_files:
32
32
  - LICENSE
33
33
  - README.rdoc
34
34
  files:
35
+ - .document
36
+ - .gitignore
37
+ - History.txt
35
38
  - LICENSE
36
39
  - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - lib/spob_browser_detector.rb
43
+ - lib/spob_browser_detector/detector.rb
44
+ - lib/spob_browser_detector/rails.rb
45
+ - lib/spob_browser_detector/rails/view_helpers.rb
46
+ - test/browser_detector/detector_test.rb
47
+ - test/browser_detector/rails/view_helpers_test.rb
48
+ - test/browser_detector_test.rb
49
+ - test/test_helper.rb
37
50
  has_rdoc: true
38
51
  homepage: http://github.com/midas/browser_detector
39
52
  licenses: []