rack-www 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG.rdoc +5 -0
  2. data/README.rdoc +21 -3
  3. data/lib/rack/www.rb +19 -5
  4. metadata +7 -6
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ == 1.1
2
+ * Added possibility to redirects with www or without www.
3
+ * Added possibility to set a param :message to show while redirecting.
4
+ == 1.0
5
+ * Redirects all traffic to www.
data/README.rdoc CHANGED
@@ -1,16 +1,34 @@
1
1
  == rack-www
2
2
 
3
- Redirects all trafic from http://example.com to http://www.example.com
3
+ Rack middleware to force redirects all traffic to a single domain with or without www.
4
4
 
5
5
  === Installation
6
6
 
7
- gem install rack-www #default installation
8
- gem 'rack-www' #when using bundler
7
+ #default installation
8
+ gem install rack-www
9
+
10
+ #when using bundler
11
+ gem 'rack-www'
9
12
 
10
13
  === Usage
11
14
 
15
+ Default usage:
16
+
17
+ #redirects all traffic to www
12
18
  config.middleware.use Rack::WWW
13
19
 
20
+ You can also customize the :www option to true or false:
21
+
22
+ #redirects all traffic to www
23
+ config.middleware.use Rack::WWW, :www => true
24
+
25
+ #redirects all traffic to the same domain without www
26
+ config.middleware.use Rack::WWW, :www => false
27
+
28
+ If you like it's also possible to show a message while redirecting the user:
29
+
30
+ config.middleware.use Rack::WWW, :www => false, :message => "You are being redirected..."
31
+
14
32
  === License
15
33
 
16
34
  MIT License. Copyright 2011 Jhimy Fernandes Villar. http://www.stjhimy.com
data/lib/rack/www.rb CHANGED
@@ -4,20 +4,22 @@ require 'rack/request'
4
4
  module Rack
5
5
  class WWW
6
6
  def initialize(app, options = {})
7
+ @options = {:www => true}.merge(options)
7
8
  @app = app
8
- @options = options
9
+ @www = @options[:www]
10
+ @message = @options[:message]
9
11
  end
10
12
 
11
13
  def call(env)
12
14
  status, headers, body = @app.call(env)
13
15
  req = Request.new(env)
14
16
  host = URI(req.host).to_s
15
- if already_www?(host)
16
- [status, headers, body]
17
+ if (already_www?(host) && @www == true) || (!already_www?(host) && @www == false)
18
+ [status, headers, @message || body]
17
19
  else
18
- url = URI(req.url).scheme + "://www." + host + URI(req.path).to_s
20
+ url = prepare_url(req)
19
21
  headers = headers.merge('Location' => url)
20
- [301, headers, body]
22
+ [301, headers, @message || body]
21
23
  end
22
24
  end
23
25
 
@@ -25,5 +27,17 @@ module Rack
25
27
  def already_www?(host)
26
28
  host.downcase =~ /^(www.)/
27
29
  end
30
+
31
+ def prepare_url(req)
32
+ scheme = URI(req.url).scheme
33
+ host = URI(req.host).to_s.gsub(/^(www.)/, "")
34
+ path = URI(req.path).to_s
35
+ if @www == true
36
+ host = "://www." + host
37
+ else
38
+ host = "://" + host
39
+ end
40
+ scheme + host + path
41
+ end
28
42
  end
29
43
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-www
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jhimy Fernandes Villar
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-06 00:00:00 -03:00
18
+ date: 2011-05-10 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: "0"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
- description: " Rack middleware to force redirects to www.\n"
35
+ description: " Rack middleware to force redirects all traffic to a single domain with or without www.\n"
36
36
  email: stjhimy@gmail.com
37
37
  executables: []
38
38
 
@@ -44,6 +44,7 @@ files:
44
44
  - lib/rack-www.rb
45
45
  - lib/rack/www.rb
46
46
  - LICENSE
47
+ - CHANGELOG.rdoc
47
48
  - README.rdoc
48
49
  has_rdoc: true
49
50
  homepage: https://github.com/stjhimy/rack-www
@@ -78,6 +79,6 @@ rubyforge_project:
78
79
  rubygems_version: 1.5.2
79
80
  signing_key:
80
81
  specification_version: 3
81
- summary: Force redirects to www.
82
+ summary: Force redirects to a single domain with or without www
82
83
  test_files: []
83
84