which_browser 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ Which Browser
2
+ -------------
3
+
4
+ Which Browser adds a few helper methods to a rack request so you can write cleaner logic in your views.
5
+
6
+ For example, the age old battle against IE 5 & 6 can be handled like so:
7
+
8
+ stylesheet_link_tag "ie" if request.old_ie?
9
+
10
+
11
+ Here's some other popular helpers:
12
+
13
+ request.ie?
14
+ request.ie6?
15
+ request.ipad?
16
+ request.ipod?
17
+ request.iphone?
18
+ request.mobile_safari?
19
+
20
+
21
+
22
+ License
23
+ -------
24
+
25
+ Copyright (c) 2011 Spencer Steffen, released under the New BSD License All rights reserved.
@@ -1,3 +1,3 @@
1
1
  module WhichBrowser
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/which_browser.rb CHANGED
@@ -1,10 +1,5 @@
1
1
  module WhichBrowser
2
2
 
3
- def test_user_agent(regex)
4
- self.env["HTTP_USER_AGENT"] && self.env["HTTP_USER_AGENT"].match(regex) != nil
5
- end
6
-
7
-
8
3
  # =============================================
9
4
  # OS Methods
10
5
 
@@ -60,25 +55,86 @@ module WhichBrowser
60
55
  test_user_agent(/Opera/)
61
56
  end
62
57
 
63
-
64
- # =============================================
65
- # IE Methods
66
-
67
58
  def ie?
68
59
  test_user_agent(/MSIE\s[56789]/)
69
60
  end
70
61
 
71
62
  def old_ie?
72
- test_user_agent(/MSIE\s[56]/)
63
+ test_user_agent(/MSIE\s[456]/)
73
64
  end
74
65
 
75
- [5,6,7,8].each do |num|
76
- define_method "ie#{num}?" do
77
- test_user_agent Regexp.new("MSIE\s#{num}")
78
- end
66
+
67
+ # =============================================
68
+ # Versions
69
+
70
+
71
+ def test_user_agent(regex)
72
+ self.user_agent && self.user_agent.match(regex) != nil
79
73
  end
80
74
 
75
+
76
+ private
77
+
78
+ def method_missing(method, *args, &block)
79
+ if matches = method.to_s.match(/^(ie|safari|ios|ff|firefox|chrome|opera)\_version$/)
81
80
 
81
+ get_browser_version(matches[1])
82
+
83
+ elsif matches = method.to_s.match(/^(ie|safari|ff|firefox|chrome|opera)(_[l|g]te?)?([0-9]+)\?$/)
84
+
85
+ browser_v = self.send(matches[1] + "_version")
86
+
87
+ # method really is missing
88
+ super if browser_v == nil
89
+
90
+ # browser version not found
91
+ return false unless browser_v
92
+
93
+ operator = matches[2].to_s.sub('_', '')
94
+ version = matches[3].to_f
95
+
96
+ case operator
97
+ when 'lt'
98
+ browser_v < version
99
+ when 'gt'
100
+ browser_v > version
101
+ when 'lte'
102
+ browser_v <= version
103
+ when 'gte'
104
+ browser_v >= version
105
+ else
106
+ test_user_agent Regexp.new("MSIE\s#{version.to_i}")
107
+ end
108
+
109
+ else
110
+ super
111
+ end
112
+ end
113
+
114
+
115
+ def get_browser_version(name)
116
+ case name
117
+ when 'ie'
118
+ extract_version_with(/MSIE\s?([0-9\.]+)/)
119
+ when 'safari'
120
+ extract_version_with(/Version\/([0-9\.]+)\sSafari/)
121
+ when 'ios'
122
+ extract_version_with(/Version\/([0-9\.]+)\sMobile/)
123
+ when 'ff', 'firefox'
124
+ extract_version_with(/Firefox\/([0-9\.]+)/)
125
+ when 'chrome'
126
+ extract_version_with(/Chrome\/([0-9\.]+)/)
127
+ when 'opera'
128
+ return false unless opera?
129
+ extract_version_with(/Version\/([0-9\.]+)$/)
130
+ end
131
+ end
132
+
133
+ def extract_version_with(regex)
134
+ v = (self.user_agent.match(regex) || [])[1].to_f
135
+ 0 < v ? v : false
136
+ end
137
+
82
138
  end
83
139
 
84
140
  Rack::Request.send(:include, WhichBrowser)
data/test/dummy/app.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'ramaze'
3
+
4
+ require File.expand_path("../../../lib/which_browser", __FILE__)
5
+
6
+ class MainController < Ramaze::Controller
7
+ def index
8
+ {
9
+
10
+ "USER AGENT" => request.user_agent,
11
+ "IE" => request.ie?,
12
+ "IE 6?" => request.ie6?,
13
+ "IE 7?" => request.ie7?,
14
+ "IE LT 6?" => request.ie_lt6?,
15
+ "IE LT 7?" => request.ie_lt7?,
16
+ "IE GT 7?" => request.ie_gt7?,
17
+ "IE LTE 6?" => request.ie_lte6?,
18
+ "IE LTE 7?" => request.ie_lte7?,
19
+ "IE GTE 7?" => request.ie_gte7?
20
+ }.to_a.collect!{ |key,value|
21
+ "#{key}: \t\t #{value}"
22
+ }.join("<br/>")
23
+ end
24
+ end
25
+
26
+ Ramaze.start(:mode => :dev)
@@ -0,0 +1,25 @@
1
+ require_relative 'helper'
2
+
3
+ class TestChrome < Test::Unit::TestCase
4
+
5
+ # chrome tests
6
+
7
+ should "be chrome on mac" do
8
+ request = get_request(:Chrome, '8.0.552.237')
9
+ assert request.chrome?
10
+ assert request.mac?
11
+ end
12
+
13
+ should "be chrome on pc" do
14
+ request = get_request(:Chrome, '0.2.149.30')
15
+ assert request.chrome?
16
+ assert request.pc?
17
+ end
18
+
19
+ should "be chrome on linux" do
20
+ request = get_request(:Chrome, '4.0.202.2')
21
+ assert request.chrome?
22
+ assert request.linux?
23
+ end
24
+
25
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'helper'
2
+
3
+ class TestFirefox < Test::Unit::TestCase
4
+
5
+ # firefox tests
6
+
7
+ should "be firefox on mac" do
8
+ request = get_request(:Firefox, '3.6')
9
+ assert request.firefox?
10
+ assert request.mac?
11
+ end
12
+
13
+ should "be firefox on pc" do
14
+ request = get_request(:Firefox, '3.1')
15
+ assert request.firefox?
16
+ assert request.pc?
17
+ end
18
+
19
+ should "be firefox on linux" do
20
+ request = get_request(:Firefox, '3.5')
21
+ assert request.firefox?
22
+ assert request.linux?
23
+ end
24
+
25
+ end
@@ -0,0 +1,83 @@
1
+ require_relative 'helper'
2
+
3
+ class TestInternetExplorer < Test::Unit::TestCase
4
+
5
+ # ie tests
6
+
7
+ should "be ie 5" do
8
+ request = get_request(:IE, '5.5')
9
+ assert request.ie?
10
+ assert request.old_ie?
11
+ assert request.ie5?
12
+ assert request.pc?
13
+ end
14
+
15
+ should "be ie 6" do
16
+ request = get_request(:IE, '6.0')
17
+ assert request.ie?
18
+ assert request.old_ie?
19
+ assert request.ie6?
20
+ assert request.pc?
21
+ end
22
+
23
+ should "be ie 7" do
24
+ request = get_request(:IE, '7.0b')
25
+ assert request.ie?
26
+ assert !request.old_ie?
27
+ assert request.ie7?
28
+ assert request.pc?
29
+ end
30
+
31
+ should "be ie 8" do
32
+ request = get_request(:IE, '8.0')
33
+ assert request.ie?
34
+ assert !request.old_ie?
35
+ assert request.ie8?
36
+ assert request.pc?
37
+ end
38
+
39
+ should "be less than ie 6" do
40
+ request = get_request(:IE, '5.5')
41
+ assert !request.ie_lt5?
42
+ assert request.ie_lt6?
43
+ assert request.ie_lt7?
44
+ assert request.ie_lt8?
45
+ assert request.ie_lt9?
46
+ end
47
+
48
+ should "be greater than ie 6" do
49
+ request = get_request(:IE, '6.0')
50
+ assert request.ie_gt5?
51
+ assert !request.ie_gt6?
52
+ assert !request.ie_gt7?
53
+ assert !request.ie_gt8?
54
+ assert !request.ie_gt9?
55
+ end
56
+
57
+ should "be greater than or equal to ie 6" do
58
+ request = get_request(:IE, '6.0')
59
+ assert request.ie_gte5?
60
+ assert request.ie_gte6?
61
+ assert !request.ie_gte7?
62
+ assert !request.ie_gte8?
63
+ assert !request.ie_gte9?
64
+ end
65
+
66
+ should "be greater than or equal to ie 8" do
67
+ request = get_request(:IE, '8.0')
68
+ assert request.ie_gte5?
69
+ assert request.ie_gte6?
70
+ assert request.ie_gte7?
71
+ assert request.ie_gte8?
72
+ assert !request.ie_gte9?
73
+ end
74
+
75
+ should "be false if not ie" do
76
+ request = get_request(:Safari, '4.0.1')
77
+ assert !request.ie_lt5?
78
+ assert !request.ie_gt6?
79
+ assert !request.ie_lte7?
80
+ assert !request.ie_gte8?
81
+ end
82
+
83
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'helper'
2
+
3
+ class TestSafari < Test::Unit::TestCase
4
+
5
+ # safari tests
6
+
7
+ should "be safari on mac" do
8
+ request = get_request(:Safari, '4.0.1')
9
+ assert request.safari?
10
+ assert request.mac?
11
+ end
12
+
13
+ should "be safari on windows" do
14
+ request = get_request(:Safari, '4.0.3')
15
+ assert request.safari?
16
+ assert request.pc?
17
+ end
18
+
19
+ # mobile safari tests
20
+
21
+ should "be safari ipad" do
22
+ request = get_request(:MobileSafari, 'iPad')
23
+ assert !request.safari?
24
+ assert request.mobile_safari?
25
+ assert request.ipad?
26
+ end
27
+
28
+ should "be safari ipod" do
29
+ request = get_request(:MobileSafari, 'iPod')
30
+ assert !request.safari?
31
+ assert request.mobile_safari?
32
+ assert request.ipod?
33
+ end
34
+
35
+ should "be safari iphone" do
36
+ request = get_request(:MobileSafari, 'iPhone')
37
+ assert !request.safari?
38
+ assert request.mobile_safari?
39
+ assert request.iphone?
40
+ end
41
+
42
+ end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: which_browser
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 0
9
- version: 0.1.0
4
+ prerelease:
5
+ version: 0.1.1
10
6
  platform: ruby
11
7
  authors:
12
8
  - Spencer Steffen
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-01-28 00:00:00 -08:00
13
+ date: 2011-02-08 00:00:00 -08:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -25,10 +21,6 @@ dependencies:
25
21
  requirements:
26
22
  - - ">="
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 1
30
- - 0
31
- - 1
32
24
  version: 1.0.1
33
25
  type: :runtime
34
26
  version_requirements: *id001
@@ -40,10 +32,6 @@ dependencies:
40
32
  requirements:
41
33
  - - ">="
42
34
  - !ruby/object:Gem::Version
43
- segments:
44
- - 2
45
- - 11
46
- - 3
47
35
  version: 2.11.3
48
36
  type: :development
49
37
  version_requirements: *id002
@@ -59,11 +47,17 @@ extra_rdoc_files: []
59
47
  files:
60
48
  - .gitignore
61
49
  - Gemfile
50
+ - README.md
62
51
  - Rakefile
63
52
  - lib/which_browser.rb
64
53
  - lib/which_browser/version.rb
54
+ - test/dummy/app.rb
65
55
  - test/helper.rb
56
+ - test/test_chrome.rb
57
+ - test/test_firefox.rb
58
+ - test/test_internet_explorer.rb
66
59
  - test/test_requests.rb
60
+ - test/test_safari.rb
67
61
  - which_browser.gemspec
68
62
  has_rdoc: true
69
63
  homepage: ""
@@ -79,24 +73,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
73
  requirements:
80
74
  - - ">="
81
75
  - !ruby/object:Gem::Version
82
- segments:
83
- - 0
84
76
  version: "0"
85
77
  required_rubygems_version: !ruby/object:Gem::Requirement
86
78
  none: false
87
79
  requirements:
88
80
  - - ">="
89
81
  - !ruby/object:Gem::Version
90
- segments:
91
- - 0
92
82
  version: "0"
93
83
  requirements: []
94
84
 
95
85
  rubyforge_project:
96
- rubygems_version: 1.3.7
86
+ rubygems_version: 1.5.0
97
87
  signing_key:
98
88
  specification_version: 3
99
89
  summary: Which Browser adds a few helper methods to a rack request.
100
90
  test_files:
91
+ - test/dummy/app.rb
101
92
  - test/helper.rb
93
+ - test/test_chrome.rb
94
+ - test/test_firefox.rb
95
+ - test/test_internet_explorer.rb
102
96
  - test/test_requests.rb
97
+ - test/test_safari.rb