rack-caniuse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +29 -0
  2. data/Rakefile +8 -0
  3. data/lib/rack/caniuse.rb +60 -0
  4. metadata +80 -0
@@ -0,0 +1,29 @@
1
+ # About
2
+
3
+
4
+
5
+ # Installation.
6
+
7
+ Add to your Gemfile.
8
+
9
+ ```ruby
10
+ gem 'rack-caniuse', require: 'rack/caniuse'
11
+ ```
12
+
13
+ # Rails Usage
14
+
15
+ Add to your config/application.rb.
16
+
17
+ ```ruby
18
+ config.middleware.use Rack::CanIUse
19
+ ```
20
+
21
+ # Sinatra Usage
22
+
23
+ ```ruby
24
+ use Rack::BrowserCheck, redirect: '/noieplease.html'
25
+ ```
26
+
27
+ # License
28
+
29
+ MIT.
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => [:test]
4
+
5
+ Rake::TestTask.new do |task|
6
+ task.pattern = 'test/test.rb'
7
+ task.warning, task.verbose = true, true
8
+ end
@@ -0,0 +1,60 @@
1
+ module Rack
2
+
3
+ class CanIUse
4
+
5
+ require 'json'
6
+ require 'browser'
7
+
8
+ DefaultOptions = {
9
+ features: [],
10
+ redirect: 'http://browsehappy.com/'
11
+ }
12
+
13
+ def initialize(app, options = {})
14
+ @features = {}
15
+ options.reverse_merge!(DefaultOptions)
16
+ options[:features].each do |feature|
17
+ path = File.join('../caniuse',
18
+ 'features-json', feature + '.json')
19
+ unless File.readable?(path)
20
+ puts path
21
+ raise "Invalid feature #{feature}."
22
+ end
23
+ hash = JSON.parse(File.read(path))
24
+ @features[feature] = hash
25
+ end
26
+ @app, @options = app, options
27
+ end
28
+
29
+ def call(env)
30
+ agent, lang = env['HTTP_USER_AGENT'], env['Accept-Language']
31
+ browser = Browser.new(ua: agent, accept_language: lang)
32
+ validate_support(browser)
33
+ end
34
+
35
+ private
36
+
37
+ def validate_support(browser)
38
+ name, version = browser.name, browser.version
39
+ unsupported_features = []
40
+ @features.each do |feature,info|
41
+ browser_stats = info['stats'][name]
42
+ support = browser_stats[version]
43
+ unless support == 'y'
44
+ unsupported_feautres << info['title']
45
+ end
46
+ end
47
+ unless unsupported_features.empty?
48
+ redirect(unsupported_features)
49
+ end
50
+ end
51
+
52
+ def redirect(unsupported_features)
53
+ msg = "Browser does not support the following features:"
54
+ unsupported_features.each { |f| msg << "\n - #{f}" }
55
+ [301, {'Location' => @options[:redirect]}, [msg]]
56
+ end
57
+
58
+ end
59
+
60
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-caniuse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Louis Mullie
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: browser
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ''
47
+ email:
48
+ - louis.mullie@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - Rakefile
55
+ - lib/rack/caniuse.rb
56
+ homepage: ''
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.25
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: ''
80
+ test_files: []