rack-seo_redirect 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f424acca9d2b87c96a82186e8c75b4180cf65b2b
4
+ data.tar.gz: 302527f61c5db299a32a87b01f99b87e15b954f7
5
+ SHA512:
6
+ metadata.gz: c3924330a12af0f119cf508c67eccdcee7b6282154461614cf9edf2cfd9b4b10d7232156425a1610cfbab9b0d3cfa9b041af059d6cbb0eb478c7e3233e3f96ff
7
+ data.tar.gz: 784b21db6692fc02530c201fb7243a35543ffd84428d29fd2e4b7812a021c3cb3bf7ccd775679f753da1b295b0cff592270761b3e7da4e3ad9ec48e7c1d9561b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-seo_redirect.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Michael Khabarov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # Rack::SeoRedirect
2
+
3
+ Rack middleware for making non-www to www (and conversely) redirects and removing trailing slash in urls. Use it if you can not edit Nginx or Apache rewrite rules.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-seo_redirect'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-seo_redirect
18
+
19
+ ## Usage
20
+
21
+ ### With any Rack application
22
+
23
+ # config.ru
24
+ require 'rack/seo_redirect'
25
+ use Rack::SeoRedirect::Www
26
+ use Rack::SeoRedirect::TrailingSlash
27
+ run MyApp
28
+
29
+ ### With Rails
30
+
31
+ Insert to the top of the Rails middleware stack:
32
+
33
+ # application.rb
34
+ config.middleware.insert 0, Rack::SeoRedirect::Www
35
+ config.middleware.insert 0, Rack::SeoRedirect::TrailingSlash
36
+
37
+ ## Customization
38
+
39
+ ### Rack::SeoRedirect::Www
40
+
41
+ Your can pass *true* or *false* as a parameter to *Www* middleware. This indicates either you need *www* in your url or not.
42
+
43
+ For non-www to www redirect use:
44
+
45
+ use Rack::SeoRedirect::Www, true
46
+
47
+ For www to non-www redirect use:
48
+
49
+ use Rack::SeoRedirect::Www, false
50
+
51
+ Default is *false* (www to non-www).
52
+
53
+ ### Rack::SeoRedirect::TrailingSlash
54
+
55
+ Your can pass *true* or *false* as a parameter to *TrailingSlash* middleware. This indicates either you need */* in the end of your url or not.
56
+
57
+ For adding */* to the end of urls use:
58
+
59
+ use Rack::SeoRedirect::TrailingSlash, true
60
+
61
+ For removing */* from the end of urls use:
62
+
63
+ use Rack::SeoRedirect::TrailingSlash, false
64
+
65
+ Default is *false* (removing trailing slash).
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,9 @@
1
+ require "rack/seo_redirect/version"
2
+ require "rack/seo_redirect/base"
3
+ require "rack/seo_redirect/www"
4
+ require "rack/seo_redirect/trailing_slash"
5
+
6
+ module Rack
7
+ module SeoRedirect
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ require 'rack/mime'
2
+
3
+ module Rack
4
+ module SeoRedirect
5
+ class Base
6
+ def initialize app
7
+ @app = app
8
+ end
9
+
10
+ private
11
+
12
+ def request
13
+ Rack::Request.new(@env)
14
+ end
15
+
16
+ def headers url
17
+ {
18
+ 'Location' => url,
19
+ 'Content-Type' => ::Rack::Mime.mime_type(::File.extname(request.path), 'text/html')
20
+ }
21
+ end
22
+
23
+ def redirect_message url
24
+ "Redirecting to <a href='#{url}'>#{url}</a>"
25
+ end
26
+
27
+ def build_url options = {}
28
+ options[:host] ||= request.host
29
+ options[:path] ||= request.path
30
+
31
+ url = "#{request.scheme}://#{options[:host]}"
32
+
33
+ if request.scheme == "https" && request.port != 443 ||
34
+ request.scheme == "http" && request.port != 80
35
+ url << ":#{request.port}"
36
+ end
37
+
38
+ url << "#{options[:path]}"
39
+ url << "?#{request.query_string}" unless request.query_string.empty?
40
+
41
+ url
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ module Rack
2
+ module SeoRedirect
3
+ class TrailingSlash < Base
4
+ def initialize app, slash = false
5
+ super(app)
6
+ @should_ends_with_slash = slash
7
+ end
8
+
9
+ def call env
10
+ @env = env
11
+ req = Rack::Request.new(env)
12
+ ends_with_slash = !!(req.path =~ /\A(.*)\/\z/)
13
+
14
+ if req.get? && req.path != '/' && @should_ends_with_slash != ends_with_slash
15
+ path = @should_ends_with_slash ? "#{req.path}/" : "#{$1}"
16
+ url = build_url(:path => path)
17
+
18
+ [ 301, headers(url), [ redirect_message(url) ] ]
19
+ else
20
+ @app.call(env)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module SeoRedirect
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ module Rack
2
+ module SeoRedirect
3
+ class Www < Base
4
+ def initialize app, www = false
5
+ super(app)
6
+ @should_starts_from_www = www
7
+ end
8
+
9
+ def call env
10
+ @env = env
11
+
12
+ starts_from_www = !!(request.host =~ /\Awww.(.*)\z/)
13
+
14
+ if request.get? && @should_starts_from_www != starts_from_www
15
+ host = @should_starts_from_www ? "www.#{request.host}" : $1
16
+ url = build_url(:host => host)
17
+
18
+ [ 301, headers(url), [ redirect_message(url) ] ]
19
+ else
20
+ @app.call(env)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/seo_redirect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-seo_redirect"
8
+ spec.version = Rack::SeoRedirect::VERSION
9
+ spec.authors = ["Michael Khabarov"]
10
+ spec.email = ["michaelkhabarov@alphastate.ru"]
11
+ spec.description = %q{Remove or add www and trailing slash from urls for Rack applications. Use it if you can not edit Nginx or Apache configs.}
12
+ spec.summary = %q{Make www and trailing slash redirects from Rack}
13
+ spec.homepage = "https://github.com/michaelkhabarov/rack-seo_redirect"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rack-test"
24
+ spec.add_development_dependency "rspec", "~> 2.0"
25
+
26
+ spec.add_dependency "rack"
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'rack/seo_redirect'
2
+ require 'rack/test'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ config.order = 'random'
10
+ config.include Rack::Test::Methods
11
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::SeoRedirect::TrailingSlash do
4
+
5
+ let(:base) { Proc.new { |env| [ 200, env, 'App' ] } }
6
+
7
+ context "default behaviour" do
8
+ let(:app) { Rack::SeoRedirect::TrailingSlash.new(base) }
9
+
10
+ it 'set @should_ends_with_slash to false' do
11
+ app.instance_variable_get('@should_ends_with_slash').should be_false
12
+ end
13
+
14
+ it 'removes trailing slash' do
15
+ get 'http://www.example.com/users/'
16
+ last_response.status.should == 301
17
+ last_response.location.should == 'http://www.example.com/users'
18
+ end
19
+ end
20
+
21
+ context "with trailing slash" do
22
+ let(:app) { Rack::SeoRedirect::TrailingSlash.new(base, true) }
23
+
24
+ it 'adds slash' do
25
+ get 'http://example.com/users'
26
+ last_response.status.should == 301
27
+ last_response.location.should == 'http://example.com/users/'
28
+ end
29
+
30
+ it 'adds slash preserving port and path' do
31
+ get 'http://example.com:3000/users?foo=bar'
32
+ last_response.status.should == 301
33
+ last_response.location.should == 'http://example.com:3000/users/?foo=bar'
34
+ end
35
+
36
+ it 'does not do anything if slash is already in url' do
37
+ get 'http://example.com/users/?foo=bar'
38
+ last_response.status.should == 200
39
+ end
40
+
41
+ it 'does not do anything for root url' do
42
+ get 'http://example.com'
43
+ last_response.status.should == 200
44
+
45
+ get 'http://example.com/'
46
+ last_response.status.should == 200
47
+ end
48
+ end
49
+
50
+ context "without trailing slash" do
51
+ let(:app) { Rack::SeoRedirect::TrailingSlash.new(base, false) }
52
+
53
+ it 'removes slash' do
54
+ get 'http://example.com/users/'
55
+ last_response.status.should == 301
56
+ last_response.location.should == 'http://example.com/users'
57
+ end
58
+
59
+ it 'adds slash preserving port and path' do
60
+ get 'http://example.com:3000/users/?foo=bar'
61
+ last_response.status.should == 301
62
+ last_response.location.should == 'http://example.com:3000/users?foo=bar'
63
+ end
64
+
65
+ it 'does not do anything if no slash in url' do
66
+ get 'http://example.com/users?foo=bar'
67
+ last_response.status.should == 200
68
+ end
69
+
70
+ it 'does not do anything for root url' do
71
+ get 'http://example.com'
72
+ last_response.status.should == 200
73
+
74
+ get 'http://example.com/'
75
+ last_response.status.should == 200
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::SeoRedirect::Www do
4
+
5
+ let(:base) { Proc.new { |env| [ 200, env, 'App' ] } }
6
+
7
+ context "default behaviour" do
8
+ let(:app) { Rack::SeoRedirect::Www.new(base) }
9
+
10
+ it 'set @should_starts_from_www to false' do
11
+ app.instance_variable_get('@should_starts_from_www').should be_false
12
+ end
13
+
14
+ it 'removes www' do
15
+ get 'http://www.example.com/'
16
+ last_response.status.should == 301
17
+ last_response.location.should == 'http://example.com/'
18
+ end
19
+ end
20
+
21
+ context "non-www to www" do
22
+ let(:app) { Rack::SeoRedirect::Www.new(base, true) }
23
+
24
+ it 'adds www' do
25
+ get 'http://example.com/'
26
+ last_response.status.should == 301
27
+ last_response.location.should == 'http://www.example.com/'
28
+ end
29
+
30
+ it 'adds www preserving port and path' do
31
+ get 'http://example.com:3000/users?foo=bar'
32
+ last_response.status.should == 301
33
+ last_response.location.should == 'http://www.example.com:3000/users?foo=bar'
34
+ end
35
+
36
+ it 'does not do anything if www presence in url' do
37
+ get 'http://www.example.com/users?foo=bar'
38
+ last_response.status.should == 200
39
+ end
40
+ end
41
+
42
+ context "www to non-www" do
43
+ let(:app) { Rack::SeoRedirect::Www.new(base, false) }
44
+
45
+ it 'removes www' do
46
+ get 'http://www.example.com/'
47
+ last_response.status.should == 301
48
+ last_response.location.should == 'http://example.com/'
49
+ end
50
+
51
+ it 'removes www preserving port and path' do
52
+ get 'http://www.example.com:3000/users?foo=bar'
53
+ last_response.status.should == 301
54
+ last_response.location.should == 'http://example.com:3000/users?foo=bar'
55
+ end
56
+
57
+ it 'does not redirect if www not presence in url' do
58
+ get 'http://example.com/users?foo=bar'
59
+ last_response.status.should == 200
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-seo_redirect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Khabarov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Remove or add www and trailing slash from urls for Rack applications.
84
+ Use it if you can not edit Nginx or Apache configs.
85
+ email:
86
+ - michaelkhabarov@alphastate.ru
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/rack/seo_redirect.rb
97
+ - lib/rack/seo_redirect/base.rb
98
+ - lib/rack/seo_redirect/trailing_slash.rb
99
+ - lib/rack/seo_redirect/version.rb
100
+ - lib/rack/seo_redirect/www.rb
101
+ - rack-seo_redirect.gemspec
102
+ - spec/spec_helper.rb
103
+ - spec/trailing_slash_spec.rb
104
+ - spec/www_spec.rb
105
+ homepage: https://github.com/michaelkhabarov/rack-seo_redirect
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.1.9
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Make www and trailing slash redirects from Rack
129
+ test_files:
130
+ - spec/spec_helper.rb
131
+ - spec/trailing_slash_spec.rb
132
+ - spec/www_spec.rb