rack-useragent-filter 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,10 +13,23 @@ Let's say you don't support IE6 and want to prevent IE6 users from accessing to
13
13
 
14
14
  use Rack::UserAgent::Filter, [
15
15
  ["Internet Explorer", "7.0"]
16
- ]
16
+ ]
17
17
 
18
-
19
- Done! From now on users with IE version lower than 7.0 will get a nice error message saying that their browsers need to be updated... Ok, ok, not so nice message, you can personalize it by putting an <tt>upgrade.html</tt> file inside the <tt>/public</tt> directory of your application.
18
+ Done! From now on users with IE version lower than 7.0 will get a nice error message saying that their browsers need to be updated... Ok, ok, not so nice message, you can personalize it by passing custom message, as shown below:
19
+
20
+ use Rack::UserAgent::Filter, [
21
+ ["Internet Explorer", "7.0"]
22
+ ], :custom_message => "You are using old browser..."
23
+
24
+ <em> What about not just one sentence message ? </em>
25
+
26
+ You can specify source url of template
27
+
28
+ use Rack::UserAgent::Filter, [
29
+ ["Internet Explorer", "7.0"]
30
+ ], :template => "http://www.example.com/not_supported.html"
31
+
32
+ <em> url should be valid http||https </em>
20
33
 
21
34
  <em>- Cool!, what about something more dynamic... like ERB or HAML?</em>
22
35
 
@@ -31,8 +44,6 @@ Then you could show the browser version as part of your message:
31
44
  Sorry but <%= "#{@browser.browser} #{@browser.version}" %> is not supported
32
45
 
33
46
 
34
-
35
-
36
47
  <em>- But... what if IE6 user do wants to see how page looks like in his favourite browser?</em>
37
48
 
38
49
  You can show button "I take full responsibility of using IE6. Let me in!". User clicks button and with some little help
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.2.0
@@ -2,6 +2,8 @@ require 'user_agent'
2
2
  require 'erb'
3
3
  require 'tilt'
4
4
  require 'ostruct'
5
+ require 'net/http'
6
+ require 'uri'
5
7
 
6
8
  module Rack
7
9
  module UserAgent
@@ -12,6 +14,7 @@ module Rack
12
14
  @browsers = required_browsers.map{ |b| OpenStruct.new(:browser => b[0], :version => b[1]) }
13
15
  @template = options[:template]
14
16
  @force_with_cookie = options[:force_with_cookie]
17
+ @custom_message = options[:custom_message]
15
18
  end
16
19
 
17
20
  def call(env)
@@ -31,16 +34,27 @@ module Rack
31
34
  useragent && @browsers.detect { |browser| useragent < browser }
32
35
  end
33
36
 
37
+ def template_is_url?
38
+ (@template =~ URI::regexp(%w(http https))).nil? ? false : true
39
+ end
40
+
34
41
  def detection_disabled_by_cookie?(cookies)
35
42
  @force_with_cookie && cookies.keys.include?(@force_with_cookie)
36
43
  end
37
44
 
45
+ def render_default_message
46
+ @custom_message ? @custom_message : "Sorry, your browser is not supported. Please upgrade"
47
+ end
48
+
38
49
  def render_page(useragent)
39
50
  if @template && ::File.exists?(@template)
40
51
  @browser = useragent # for the template
41
52
  Tilt.new(@template).render(self)
53
+ elsif @template && template_is_url?
54
+ @browser = useragent
55
+ Net::HTTP.get(URI.parse(@template))
42
56
  else
43
- "Sorry, your browser is not supported. Please upgrade"
57
+ render_default_message
44
58
  end
45
59
  end
46
60
  end
@@ -1,44 +1,41 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rack-useragent-filter}
8
- s.version = "0.1.9"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tomasz Mazur", "Sergio Gil", "Luismi Cavall\303\251"]
12
- s.date = %q{2010-10-14}
12
+ s.date = %q{2011-03-23}
13
13
  s.email = %q{defkode@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
16
16
  ]
17
17
  s.files = [
18
- ".gitignore",
19
- "README.rdoc",
20
- "Rakefile",
21
- "VERSION",
22
- "examples/upgrade.html",
23
- "lib/rack/useragent/filter.rb",
24
- "rack-useragent-filter.gemspec",
25
- "test/fixtures/upgrade.erb",
26
- "test/fixtures/upgrade.haml",
27
- "test/test_helper.rb",
28
- "test/user_agent_filter_test.rb"
18
+ "README.rdoc",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "examples/upgrade.html",
22
+ "lib/rack/useragent/filter.rb",
23
+ "rack-useragent-filter.gemspec",
24
+ "test/fixtures/upgrade.erb",
25
+ "test/fixtures/upgrade.haml",
26
+ "test/test_helper.rb",
27
+ "test/user_agent_filter_test.rb"
29
28
  ]
30
29
  s.homepage = %q{http://github.com/defkode/rack-useragent}
31
- s.rdoc_options = ["--charset=UTF-8"]
32
30
  s.require_paths = ["lib"]
33
- s.rubygems_version = %q{1.3.7}
31
+ s.rubygems_version = %q{1.6.0}
34
32
  s.summary = %q{Rack Middleware for filtering by user agent}
35
33
  s.test_files = [
36
34
  "test/test_helper.rb",
37
- "test/user_agent_filter_test.rb"
35
+ "test/user_agent_filter_test.rb"
38
36
  ]
39
37
 
40
38
  if s.respond_to? :specification_version then
41
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
39
  s.specification_version = 3
43
40
 
44
41
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -1,6 +1,7 @@
1
1
  require "rubygems"
2
2
  require "rack/test"
3
3
  require "shoulda"
4
+ require 'webmock/test_unit'
4
5
 
5
6
  require File.dirname(__FILE__) + '/../lib/rack/useragent/filter'
6
7
 
@@ -71,6 +71,58 @@ class UserAgentFilterTest < Test::Unit::TestCase
71
71
  end
72
72
 
73
73
  end
74
+
75
+ context "custom message" do
76
+ setup do
77
+ def app
78
+ Rack::Builder.new do
79
+ use Rack::UserAgent::Filter, [["Internet Explorer", "7.0"]], :custom_message => "I'm custom"
80
+ run SampleApp.new
81
+ end
82
+ end
83
+ end
84
+
85
+ should "not work" do
86
+ header "User-Agent", @outdated_browser
87
+ get '/'
88
+ assert_equal "I'm custom", last_response.body
89
+ end
90
+ end
91
+
92
+ context "custom page (url as a template)" do
93
+ setup do
94
+ def app
95
+ Rack::Builder.new do
96
+ use Rack::UserAgent::Filter, [["Internet Explorer", "7.0"]], :template => "http://www.example.com"
97
+ run SampleApp.new
98
+ end
99
+ end
100
+ stub_request(:any, "http://www.example.com").to_return(:body => "test", :status => 200)
101
+ end
102
+
103
+ should "work" do
104
+ header "User-Agent", @outdated_browser
105
+ get '/'
106
+ assert_equal "test", last_response.body
107
+ end
108
+ end
109
+
110
+ context "custom page (with bad url)" do
111
+ setup do
112
+ def app
113
+ Rack::Builder.new do
114
+ use Rack::UserAgent::Filter, [["Internet Explorer", "7.0"]], :template => "ppp"
115
+ run SampleApp.new
116
+ end
117
+ end
118
+ end
119
+
120
+ should "not work" do
121
+ header "User-Agent", @outdated_browser
122
+ get '/'
123
+ assert_equal "Sorry, your browser is not supported. Please upgrade", last_response.body
124
+ end
125
+ end
74
126
 
75
127
  context "cookie" do
76
128
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-useragent-filter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 9
10
- version: 0.1.9
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tomasz Mazur
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-10-14 00:00:00 +02:00
20
+ date: 2011-03-23 00:00:00 +01:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -77,7 +77,6 @@ extensions: []
77
77
  extra_rdoc_files:
78
78
  - README.rdoc
79
79
  files:
80
- - .gitignore
81
80
  - README.rdoc
82
81
  - Rakefile
83
82
  - VERSION
@@ -93,8 +92,8 @@ homepage: http://github.com/defkode/rack-useragent
93
92
  licenses: []
94
93
 
95
94
  post_install_message:
96
- rdoc_options:
97
- - --charset=UTF-8
95
+ rdoc_options: []
96
+
98
97
  require_paths:
99
98
  - lib
100
99
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -118,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
117
  requirements: []
119
118
 
120
119
  rubyforge_project:
121
- rubygems_version: 1.3.7
120
+ rubygems_version: 1.6.0
122
121
  signing_key:
123
122
  specification_version: 3
124
123
  summary: Rack Middleware for filtering by user agent
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- pkg
2
- *.gem