browsernizer 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 CHANGED
@@ -18,7 +18,42 @@ Hook it up:
18
18
 
19
19
  Configure browser support in `config/initializers/browsernizer.rb` file.
20
20
 
21
- All browsers that are not listed in initializer file will be considered to be **supported**.
21
+
22
+ ## Configuration
23
+
24
+ Initializer file is pretty self explanatory. Here is the default one:
25
+
26
+ Rails.application.config.middleware.use Browsernizer::Router do |config|
27
+ config.supported "Internet Explorer", "9"
28
+ config.supported "Firefox", "4"
29
+ config.supported "Opera", "11.1"
30
+ config.supported "Chrome", "7"
31
+ config.location "/browser.html"
32
+ end
33
+
34
+ It states that IE9+, FF4+, Opera 11.1+ and Chrome 7+ are supported.
35
+ Non listed browsers are considered to be supported regardless of their version.
36
+ Unsupported browsers will be redirected to `/browser.html` page.
37
+
38
+ If you wish to completely prevent some browsers from accessing website
39
+ (regardless of their version), for now you can specify some really high
40
+ version number (e.g. "666").
41
+
42
+ Specifying location is optional. If you prefer handling unsupported browsers on
43
+ your own, you can access browsernizer info from `request.env['browsernizer']`
44
+ within your controller.
45
+
46
+ For example, you can set before filter to display flash notice:
47
+
48
+ before_filter :check_browser_support
49
+
50
+ def check_browser_support
51
+ unless request.env['browsernizer']['supported']
52
+ flash.notice = "Your browser is not supported"
53
+ end
54
+ end
55
+
56
+ You can also access `browser` and `version` variables from this env hash.
22
57
 
23
58
 
24
59
  ## Browsers
@@ -49,5 +84,5 @@ Browser detection is done using [useragent gem](https://github.com/josh/useragen
49
84
 
50
85
  Developed by Milovan Zogovic.
51
86
 
52
- I don't believe in licenses and other woody crap.
87
+ I don't believe in licenses and other legal crap.
53
88
  Do whatever you want with this :)
@@ -3,7 +3,8 @@ module Browsernizer
3
3
 
4
4
  def initialize
5
5
  @supported = []
6
- @location = "/unsupported-browser.html"
6
+ @location = nil
7
+ @handler = lambda { }
7
8
  end
8
9
 
9
10
  def supported(browser, version)
@@ -1,6 +1,8 @@
1
1
  module Browsernizer
2
2
 
3
3
  class Router
4
+ attr_reader :config
5
+
4
6
  def initialize(app, &block)
5
7
  @app = app
6
8
  @config = Config.new
@@ -9,17 +11,36 @@ module Browsernizer
9
11
 
10
12
  def call(env)
11
13
  @env = env
14
+ @env["browsernizer"] = {
15
+ "supported" => true,
16
+ "browser" => agent.browser.to_s,
17
+ "version" => agent.version.to_s
18
+ }
12
19
 
13
20
  if html_request? && !on_redirection_path? && unsupported?
14
- [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
21
+ handle_unsupported
15
22
  elsif html_request? && on_redirection_path? && !unsupported?
16
- [303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
23
+ handle_visits_by_accident
17
24
  else
18
25
  @app.call(env)
19
26
  end
20
27
  end
21
28
 
22
29
  private
30
+ def handle_unsupported
31
+ @env["browsernizer"]["supported"] = false
32
+
33
+ if @config.get_location
34
+ [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
35
+ else
36
+ @app.call(@env)
37
+ end
38
+ end
39
+
40
+ def handle_visits_by_accident
41
+ [303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
42
+ end
43
+
23
44
  def html_request?
24
45
  @env["HTTP_ACCEPT"] && @env["HTTP_ACCEPT"].include?("text/html")
25
46
  end
@@ -28,9 +49,12 @@ module Browsernizer
28
49
  @env["PATH_INFO"] && @env["PATH_INFO"] == @config.get_location
29
50
  end
30
51
 
52
+ def agent
53
+ ::UserAgent.parse @env["HTTP_USER_AGENT"]
54
+ end
55
+
31
56
  # supported by default
32
57
  def unsupported?
33
- agent = ::UserAgent.parse @env["HTTP_USER_AGENT"]
34
58
  @config.get_supported.detect do |supported_browser|
35
59
  if agent.browser.to_s.downcase == supported_browser.browser.to_s.downcase
36
60
  a = BrowserVersion.new agent.version.to_s
@@ -1,3 +1,3 @@
1
1
  module Browsernizer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/browsernizer.rb CHANGED
@@ -1,9 +1,3 @@
1
- #TODO: remove when useragent gem is fixed
2
- require 'rubygems'
3
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
4
- require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
-
6
-
7
1
  require "browsernizer/browser"
8
2
  require "browsernizer/browser_version"
9
3
  require "browsernizer/config"
@@ -4,13 +4,6 @@ describe Browsernizer::Config do
4
4
 
5
5
  subject { config = Browsernizer::Config.new }
6
6
 
7
- describe "new" do
8
- it "sets the defaults" do
9
- subject.get_supported.should == []
10
- subject.get_location.should == "/unsupported-browser.html"
11
- end
12
- end
13
-
14
7
  describe "supported(name, version)" do
15
8
  it "defines new supported browser" do
16
9
  subject.supported "Chrome", "16.0"
@@ -8,7 +8,6 @@ describe Browsernizer::Router do
8
8
  Browsernizer::Router.new(app) do |config|
9
9
  config.supported "Firefox", "4"
10
10
  config.supported "Chrome", "7.1"
11
- config.location "/browser.html"
12
11
  end
13
12
  end
14
13
 
@@ -21,8 +20,14 @@ describe Browsernizer::Router do
21
20
  end
22
21
 
23
22
  context "All Good" do
24
- it "propagates request" do
25
- app.should_receive(:call).with(default_env)
23
+ it "sets browsernizer env and propagates request" do
24
+ response = default_env.dup
25
+ response['browsernizer'] = {
26
+ 'supported' => true,
27
+ 'browser' => "Chrome",
28
+ 'version' => "7.1.1"
29
+ }
30
+ app.should_receive(:call).with(response)
26
31
  subject.call(default_env)
27
32
  end
28
33
  end
@@ -34,15 +39,30 @@ describe Browsernizer::Router do
34
39
  })
35
40
  end
36
41
 
37
- it "prevents propagation" do
38
- app.should_not_receive(:call)
42
+ it "updates 'browsernizer' env variable and propagates request" do
43
+ @response = @env.dup
44
+ @response['browsernizer'] = {
45
+ 'supported' => false,
46
+ 'browser' => "Chrome",
47
+ 'version' => "7"
48
+ }
49
+ app.should_receive(:call).with(@response)
39
50
  subject.call(@env)
40
51
  end
41
52
 
42
- it "redirects to proper location" do
43
- response = subject.call(@env)
44
- response[0].should == 307
45
- response[1]["Location"].should == "/browser.html"
53
+ context "location is set" do
54
+ before do
55
+ subject.config.location "/browser.html"
56
+ end
57
+ it "prevents propagation" do
58
+ app.should_not_receive(:call)
59
+ subject.call(@env)
60
+ end
61
+ it "redirects to proper location" do
62
+ response = subject.call(@env)
63
+ response[0].should == 307
64
+ response[1]["Location"].should == "/browser.html"
65
+ end
46
66
  end
47
67
 
48
68
  context "Non-html request" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browsernizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-06 00:00:00.000000000Z
12
+ date: 2012-02-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70114897960180 !ruby/object:Gem::Requirement
16
+ requirement: &70317243772500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70114897960180
24
+ version_requirements: *70317243772500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70114897959760 !ruby/object:Gem::Requirement
27
+ requirement: &70317243772080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70114897959760
35
+ version_requirements: *70317243772080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: useragent
38
- requirement: &70114897959260 !ruby/object:Gem::Requirement
38
+ requirement: &70317243771580 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 0.4.6
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70114897959260
46
+ version_requirements: *70317243771580
47
47
  description: Rack middleware for redirecting unsupported user agents to "please upgrade"
48
48
  page
49
49
  email: