which_browser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .DS_Store
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in which_browser.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "lib"
7
+ t.verbose = true
8
+ end
9
+
10
+ desc "Default Task"
11
+ task :default => [ :test ]
@@ -0,0 +1,84 @@
1
+ module WhichBrowser
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
+ # =============================================
9
+ # OS Methods
10
+
11
+ def mac?
12
+ test_user_agent(/Macintosh/)
13
+ end
14
+
15
+ def pc?
16
+ test_user_agent(/Windows/)
17
+ end
18
+
19
+ def linux?
20
+ test_user_agent(/Linux/)
21
+ end
22
+
23
+
24
+ # =============================================
25
+ # iOS Methods
26
+
27
+ def mobile_safari?
28
+ test_user_agent(/Mobile\/.+Safari/)
29
+ end
30
+
31
+ def iphone?
32
+ mobile_safari? && test_user_agent(/iPhone/)
33
+ end
34
+
35
+ def ipod?
36
+ mobile_safari? && test_user_agent(/iPod/)
37
+ end
38
+
39
+ def ipad?
40
+ mobile_safari? && test_user_agent(/iPad/)
41
+ end
42
+
43
+
44
+ # =============================================
45
+ # Browsers
46
+
47
+ def safari?
48
+ test_user_agent(/Safari/) && !mobile_safari?
49
+ end
50
+
51
+ def firefox?
52
+ test_user_agent(/Firefox/)
53
+ end
54
+
55
+ def chrome?
56
+ test_user_agent(/Chrome/)
57
+ end
58
+
59
+ def opera?
60
+ test_user_agent(/Opera/)
61
+ end
62
+
63
+
64
+ # =============================================
65
+ # IE Methods
66
+
67
+ def ie?
68
+ test_user_agent(/MSIE\s[56789]/)
69
+ end
70
+
71
+ def old_ie?
72
+ test_user_agent(/MSIE\s[56]/)
73
+ end
74
+
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
79
+ end
80
+
81
+
82
+ end
83
+
84
+ Rack::Request.send(:include, WhichBrowser)
@@ -0,0 +1,3 @@
1
+ module WhichBrowser
2
+ VERSION = "0.1.0"
3
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,89 @@
1
+ begin
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'test/unit'
5
+ require 'rack'
6
+ require 'shoulda'
7
+ require 'which_browser'
8
+ rescue LoadError => e
9
+ puts "Load error!"
10
+ puts e.inspect
11
+ exit
12
+ end
13
+
14
+ USER_AGENTS = {}
15
+
16
+ def register(name, version, os, engine, engine_version, user_agent)
17
+ USER_AGENTS[name] ||= {}
18
+ USER_AGENTS[name][version] = {
19
+ :os => os,
20
+ :engine => engine,
21
+ :engine_version => engine_version,
22
+ :user_agent => user_agent
23
+ }
24
+ end
25
+
26
+ register :Safari, '4.0dp1', :'Windows XP', :webkit, '526.9', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en) AppleWebKit/526.9 (KHTML, like Gecko) Version/4.0dp1 Safari/526.8'
27
+ register :Safari, '4.0.3', :'Windows Vista', :webkit, '531.9', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9'
28
+ register :Safari, '4.0.2', :'Windows 7', :webkit, '532', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532+ (KHTML, like Gecko) Version/4.0.2 Safari/530.19.1'
29
+ register :Safari, '4.0.1', :'OS X 10.5', :webkit, '531.2', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/4.0.1 Safari/530.18'
30
+ register :Safari, '4.0', :'Windows Vista', :webkit, '528.16', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru-RU) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16'
31
+ register :Safari, '3.2.3', :'Windows XP', :webkit, '525.28.3', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; cs-CZ) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.29'
32
+
33
+ register :MobileSafari, 'iPhone', :'iPhone OS 4_1', :webkit, '6531.22.7', 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7'
34
+ register :MobileSafari, 'iPod', :'iPhone OS 4_1', :webkit, '6531.22.7', 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7'
35
+ register :MobileSafari, 'iPad', :'OS 3_2_2', :webkit, '531.21.10', 'Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.10'
36
+
37
+ register :IE, '8.0', :'Windows 7', :msie, '8.0', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)'
38
+ register :IE, '7.0b', :'Windows 2003', :msie, '7.0b', 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)'
39
+ register :IE, '6.0b', :'Windows XP', :msie, '6.0b', 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)'
40
+ register :IE, '6.0', :'Windows XP', :msie, '6.0', 'Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'
41
+ register :IE, '5.5', :'Windows 98', :msie, '4.0', 'Mozilla/4.0 (compatible; MSIE 5.50; Windows NT; SiteKiosk 4.9; SiteCoach 1.0)'
42
+
43
+
44
+ register :Opera, '9.99', :'Windows XP', :presto, '9.9.9', 'Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9'
45
+ register :Opera, '9.70', :Linux, :gecko, '20061208', 'Mozilla/5.0 (Linux i686 ; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.70'
46
+ register :Opera, '9.64', :Linux, :presto, '2.1.1', 'Opera/9.64 (X11; Linux i686; U; Linux Mint; it) Presto/2.1.1'
47
+ register :Opera, '9.00', :Wii, :unknown, nil, 'Opera/9.00 (Nintindo Wii; U; ; 103858; Wii Shop Channel/1.0; en)'
48
+
49
+ register :Chrome, '4.0.202.2', :Linux, :webkit, '532.0', 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/4.0.202.2 Safari/532.0'
50
+ register :Chrome, '0.2.149.27', :'Windows 2003', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13'
51
+ register :Chrome, '0.2.149.30', :'Windows Vista', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.30 Safari/525.13'
52
+ register :Chrome, '8.0.552.237', :'Mac OS X 10_6_6', :webkit, '534.10', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10'
53
+
54
+
55
+
56
+ register :Konqueror, '4.2', :Linux, :khtml, '4.2.4', 'Mozilla/5.0 (compatible; Konqueror/4.2; Linux; X11; x86_64) KHTML/4.2.4 (like Gecko) Fedora/4.2.4-2.fc11'
57
+ register :Konqueror, '3.1-rc6', :Linux, :konqueror, '3.1-rc6', 'Mozilla/5.0 (compatible; Konqueror/3.1-rc6; i686 Linux; 20021105)'
58
+
59
+ register :PS3, '2.00', :Playstation, :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 2.00)'
60
+ register :PS3, '1.10', :Playstation, :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 1.10)'
61
+
62
+ register :PSP, '2.00', :Playstation, :unknown, nil, 'PSP (PlayStation Portable); 2.00'
63
+ register :PSP, '2.00', :Playstation, :unknown, nil, 'Mozilla/4.0 (PSP (PlayStation Portable); 2.00)'
64
+
65
+ register :Firefox, '3.5', :Linux, :gecko, '20090624', 'Mozilla/5.0 (X11;U; Linux i686; en-GB; rv:1.9.1) Gecko/20090624 Ubuntu/9.04 (jaunty) Firefox/3.5'
66
+ #register :Firefox, '3.5', :'Windows 7', :gecko, '20090612', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1) Gecko/20090612 Firefox/3.5'
67
+ register :Firefox, '3.1', :'Windows XP', :gecko, '2009011606', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6pre) Gecko/2009011606 Firefox/3.1'
68
+ register :Firefox, '3.0', :Linux, :gecko, '2008062315', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9) Gecko/2008062315 (Gentoo) Firefox/3.0'
69
+ register :Firefox, '2.0', :Linux, :gecko, '20061202', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20061202 Firefox/2.0'
70
+ register :Firefox, '3.6', :'OS X 10.6', :gecko, '20100914', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10'
71
+
72
+
73
+
74
+
75
+ def get_request(name, version=nil)
76
+ browser = USER_AGENTS[name.to_sym]
77
+ if version
78
+ browser = browser[version]
79
+ else
80
+ browser = browser.values.first
81
+ end
82
+ user_agent = browser[:user_agent] rescue nil
83
+ raise "User Agent Not Found #{name} - #{version}" unless user_agent
84
+
85
+ env = { "HTTP_USER_AGENT" => user_agent }
86
+
87
+ Rack::Request.new( env )
88
+
89
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'helper'
2
+
3
+ class TestRequests < Test::Unit::TestCase
4
+
5
+ should "return test user agent" do
6
+ request = get_request(:Safari, '4.0.1')
7
+ assert_equal true, request.test_user_agent(/Safari/)
8
+ end
9
+
10
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "which_browser/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "which_browser"
7
+ s.version = WhichBrowser::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Spencer Steffen"]
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.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- test/*`.split("\n")
17
+
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency('rack', '>= 1.0.1')
21
+ s.add_development_dependency('shoulda', '>= 2.11.3')
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: which_browser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Spencer Steffen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-28 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rack
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 1
32
+ version: 1.0.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: shoulda
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 11
46
+ - 3
47
+ version: 2.11.3
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: Which Browser adds a few helper methods to a rack request.
51
+ email:
52
+ - spencer@citrusme.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - Rakefile
63
+ - lib/which_browser.rb
64
+ - lib/which_browser/version.rb
65
+ - test/helper.rb
66
+ - test/test_requests.rb
67
+ - which_browser.gemspec
68
+ has_rdoc: true
69
+ homepage: ""
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Which Browser adds a few helper methods to a rack request.
100
+ test_files:
101
+ - test/helper.rb
102
+ - test/test_requests.rb