ensure_subdomain 1.0.3 → 1.1.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/LICENSE +9 -16
- data/README.md +49 -9
- data/Rakefile +5 -0
- data/ensure_subdomain.gemspec +1 -3
- data/lib/action_dispatch/routing/mapper/http_helpers.rb +30 -0
- data/lib/ensure_subdomain.rb +31 -34
- data/lib/ensure_subdomain/version.rb +2 -2
- data/test/ensure_subdomain_test.rb +2 -2
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d92f9be5e9736803dcf8624689996509100ccc21
|
4
|
+
data.tar.gz: ddf4448c07e20ba6e4a812c94e4a9f95d1e658ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ffcbea839fd66d15f6410f732d6d398253a6147fb58e35a2e2f982863e86049faf25a10c5094c8388f29fa4e96f62dbb0a1abec603e91e7a49f35ef75e867f1
|
7
|
+
data.tar.gz: 02ca130fd3eeb8c3ca996562ac1b8af5b5070f411726b98a0395ee448c14b3031250c1ee09df528bb2b729257d4efaadd0a8d07f743590696d3d184fa64ac5cc
|
data/.travis.yml
ADDED
data/LICENSE
CHANGED
@@ -1,20 +1,13 @@
|
|
1
|
-
|
1
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
2
|
+
Version 2, December 2004
|
2
3
|
|
3
|
-
Copyright (
|
4
|
+
Copyright (C) 2014 Jacob Evan Shreve
|
4
5
|
|
5
|
-
|
6
|
-
this
|
7
|
-
the
|
8
|
-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
-
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
-
subject to the following conditions:
|
6
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
7
|
+
copies of this license document, and changing it is allowed as long
|
8
|
+
as the name is changed.
|
11
9
|
|
12
|
-
|
13
|
-
|
10
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
11
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
14
12
|
|
15
|
-
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
13
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
_an ActionDispatch extension to handle subdomain redirects_
|
4
4
|
|
5
|
+
[](http://badge.fury.io/rb/ensure_subdomain)
|
6
|
+
[](https://codeclimate.com/github/shreve/ensure_subdomain)
|
7
|
+
|
5
8
|
Let's say you believe that [using the www subdomain is dumb](http://no-www.org).
|
6
9
|
|
7
10
|
Ensuring your rails app doesn't use this subdomain is pretty easy, but now it's easier.
|
@@ -11,7 +14,7 @@ Ensuring your rails app doesn't use this subdomain is pretty easy, but now it's
|
|
11
14
|
gem 'ensure_subdomain'
|
12
15
|
|
13
16
|
# Preferred terminal
|
14
|
-
|
17
|
+
bundle install
|
15
18
|
|
16
19
|
# config/routes.rb
|
17
20
|
Rails.application.routes.draw do
|
@@ -21,11 +24,15 @@ Rails.application.routes.draw do
|
|
21
24
|
end
|
22
25
|
```
|
23
26
|
|
24
|
-
|
27
|
+
```
|
28
|
+
$ curl -I http://www.example.com
|
29
|
+
HTTP/1.1 301 Moved Permanently
|
30
|
+
Location: http://example.com
|
31
|
+
```
|
25
32
|
|
26
33
|
Simple as that.
|
27
34
|
|
28
|
-
Conversely, if you
|
35
|
+
Conversely, if you _do_ want to use the www subdomain, there's also a method for that.
|
29
36
|
|
30
37
|
```ruby
|
31
38
|
# config/routes.rb
|
@@ -34,23 +41,53 @@ Rails.application.routes.draw do
|
|
34
41
|
end
|
35
42
|
```
|
36
43
|
|
37
|
-
|
38
|
-
|
44
|
+
```
|
45
|
+
$ curl -I http://example.com
|
46
|
+
HTTP/1.1 301 Moved Permanently
|
47
|
+
Location: http://www.example.com
|
48
|
+
```
|
39
49
|
|
40
|
-
|
50
|
+
There's also a method for whatever custom domain or domains you'd like.
|
41
51
|
|
42
52
|
```ruby
|
43
53
|
# config/routes.rb
|
44
54
|
Rails.application.routes.draw do
|
55
|
+
# Single domain
|
56
|
+
# The only allowed subdomain is blog. All others will redirect.
|
45
57
|
ensure_subdomain 'blog'
|
46
58
|
end
|
47
59
|
```
|
48
60
|
|
49
|
-
|
61
|
+
```
|
62
|
+
$ curl -I http://example.com
|
63
|
+
HTTP/1.1 301 Moved Permanently
|
64
|
+
Location: http://blog.example.com
|
50
65
|
|
51
|
-
|
66
|
+
$ curl -I http://www.example.com
|
67
|
+
HTTP/1.1 301 Moved Permanently
|
68
|
+
Location: http://blog.example.com
|
69
|
+
```
|
52
70
|
|
53
|
-
|
71
|
+
```ruby
|
72
|
+
# config/routes.rb
|
73
|
+
Rails.application.routes.draw do
|
74
|
+
# Multiple domains
|
75
|
+
# All of these subdomains are allowed. If another subdomain is requested, the
|
76
|
+
# response will redirect to en because it's first.
|
77
|
+
ensure_subdomains %w(en es fr jp)
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
```
|
82
|
+
$ curl -I http://jp.example.com
|
83
|
+
HTTP/1.1 200 OK
|
84
|
+
|
85
|
+
$ curl -I http://example.com
|
86
|
+
HTTP/1.1 301 Moved Permanently
|
87
|
+
Location: http://en.example.com
|
88
|
+
```
|
89
|
+
|
90
|
+
We've even got you covered if you want different rules for different environments.
|
54
91
|
|
55
92
|
```ruby
|
56
93
|
# config/routes.rb
|
@@ -61,6 +98,9 @@ Rails.application.routes.draw do
|
|
61
98
|
end
|
62
99
|
```
|
63
100
|
|
101
|
+
Please note that multiple rules don't work together well. It's recommended to
|
102
|
+
just add a single ensure rule at the top of your routes.
|
103
|
+
|
64
104
|
Also recently added, and somewhat experimental, _not fucking up on Heroku!_
|
65
105
|
|
66
106
|
*Before:* GET application.herokuapp.com → 301 herokuapp.com → 301 heroku.com
|
data/Rakefile
ADDED
data/ensure_subdomain.gemspec
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
2
|
require File.expand_path('../lib/ensure_subdomain/version', __FILE__)
|
4
3
|
|
5
4
|
Gem::Specification.new do |s|
|
@@ -7,11 +6,10 @@ Gem::Specification.new do |s|
|
|
7
6
|
s.version = EnsureSubdomain::VERSION::STRING
|
8
7
|
s.platform = Gem::Platform::RUBY
|
9
8
|
s.authors = "Jacob Evan Shreve"
|
10
|
-
s.email = %w(jacob@shreve.ly)
|
11
9
|
s.summary = 'Ensure requests are going to the right subdomain.'
|
12
10
|
s.description = 'Ensure requests are going to the right subdomain to avoid competing with yourself for dem SERPs. Adds a couple methods to ActionDispatch for routes.'
|
13
11
|
s.homepage = 'https://github.com/shreve/ensure_subdomain'
|
14
|
-
s.license = '
|
12
|
+
s.license = 'WTFPL'
|
15
13
|
|
16
14
|
s.add_runtime_dependency 'actionpack', '>= 3.2.0'
|
17
15
|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActionDispatch::Routing::Mapper::HttpHelpers
|
2
|
+
def ensure_no_www
|
3
|
+
ensure_subdomain ''
|
4
|
+
end
|
5
|
+
alias_method :ensure_non_www, :ensure_no_www
|
6
|
+
alias_method :ensure_apex, :ensure_no_www
|
7
|
+
|
8
|
+
def ensure_www
|
9
|
+
ensure_subdomain 'www'
|
10
|
+
end
|
11
|
+
|
12
|
+
def ensure_on(environments)
|
13
|
+
environments.each_pair do |env, domain|
|
14
|
+
if Rails.env.to_sym == env
|
15
|
+
ensure_subdomain domain
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def ensure_subdomain(subdomain, options={})
|
21
|
+
redirector = ::EnsureSubdomain.new( subdomain )
|
22
|
+
verbs = options[:via] || [:get, :post, :put, :patch, :delete]
|
23
|
+
constraints( redirector ) do
|
24
|
+
match '/', to: redirect { |params, request| redirector.to params, request }, via: verbs
|
25
|
+
match '/*path', to: redirect { |params, request| redirector.to params, request }, via: verbs
|
26
|
+
end
|
27
|
+
end
|
28
|
+
alias_method :ensure_subdomains, :ensure_subdomain
|
29
|
+
end
|
30
|
+
|
data/lib/ensure_subdomain.rb
CHANGED
@@ -1,58 +1,55 @@
|
|
1
|
+
require_relative './action_dispatch/routing/mapper/http_helpers'
|
2
|
+
|
1
3
|
class EnsureSubdomain
|
2
|
-
attr_accessor :
|
4
|
+
attr_accessor :subdomains
|
3
5
|
|
4
|
-
def initialize(
|
5
|
-
|
6
|
+
def initialize(subdomains)
|
7
|
+
subdomains = [subdomains] unless subdomains.respond_to?(:map)
|
8
|
+
self.subdomains = subdomains.map { |s| s.sub(/\.$/, '') }
|
6
9
|
end
|
7
10
|
|
8
11
|
def matches?(request)
|
9
|
-
#
|
10
|
-
request.domain.present? and
|
11
|
-
# ''.match('www') #=> nil, which is is the opposite of what we want
|
12
|
-
((self.subdomain.empty? and request.subdomain.present?) or
|
13
|
-
request.subdomain.match(self.subdomain).nil?)
|
12
|
+
# Don't deal with addresses like http://0.0.0.0:3000
|
13
|
+
request.domain.present? and requires_redirect?(request)
|
14
14
|
end
|
15
15
|
|
16
|
+
# Only called when not on an appropriate domain
|
16
17
|
def to(params, request)
|
17
18
|
url = request.protocol
|
18
|
-
url <<
|
19
|
-
url << "#{request.subdomain}." if vendor_url?(request)
|
20
|
-
url << request.domain
|
19
|
+
url << redirect_to_full_domain(request)
|
21
20
|
url << "/#{params[:path]}" if params[:path].present?
|
22
21
|
url
|
23
22
|
end
|
24
23
|
|
25
24
|
private
|
26
|
-
def
|
27
|
-
|
25
|
+
def redirect_to_full_domain(request)
|
26
|
+
parts = []
|
27
|
+
parts << self.subdomains.first if self.subdomains.any?
|
28
|
+
parts << request.subdomain if vendor_url?(request)
|
29
|
+
parts << request.domain
|
30
|
+
parts.reject(&:empty?).join('.')
|
28
31
|
end
|
29
|
-
end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
ensure_subdomain ''
|
33
|
+
def requires_redirect?(request)
|
34
|
+
shouldnt_have_a_subdomain?(request) or !on_allowed_subdomain?(request)
|
34
35
|
end
|
35
|
-
alias_method :ensure_non_www, :ensure_no_www
|
36
|
-
alias_method :ensure_apex, :ensure_no_www
|
37
36
|
|
38
|
-
def
|
39
|
-
|
37
|
+
def shouldnt_have_a_subdomain?(request)
|
38
|
+
# ''.match('www') #=> nil, which is is the opposite of what we want
|
39
|
+
apex_required? and request.subdomain.present?
|
40
|
+
end
|
41
|
+
|
42
|
+
def on_allowed_subdomain?(request)
|
43
|
+
self.subdomains.select do |subdomain|
|
44
|
+
request.subdomain == subdomain
|
45
|
+
end.size > 0
|
40
46
|
end
|
41
47
|
|
42
|
-
def
|
43
|
-
|
44
|
-
if Rails.env.to_sym == env
|
45
|
-
ensure_subdomain domain
|
46
|
-
end
|
47
|
-
end
|
48
|
+
def apex_required?
|
49
|
+
self.subdomains.size == 1 && self.subdomains.first == ''
|
48
50
|
end
|
49
51
|
|
50
|
-
def
|
51
|
-
|
52
|
-
verbs = options[:via] || [:get, :post, :put, :patch, :delete]
|
53
|
-
constraints( redirector ) do
|
54
|
-
match '/', to: redirect { |params, request| redirector.to params, request }, via: verbs
|
55
|
-
match '/*path', to: redirect { |params, request| redirector.to params, request }, via: verbs
|
56
|
-
end
|
52
|
+
def vendor_url?(request)
|
53
|
+
request.domain.match /heroku/
|
57
54
|
end
|
58
55
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
require_relative './test_helper'
|
2
2
|
|
3
|
-
class TestEnsureSubdomain < MiniTest::
|
3
|
+
class TestEnsureSubdomain < MiniTest::Test
|
4
4
|
=begin
|
5
5
|
Test that the EnsureSubdomain constraint matching works.
|
6
6
|
Involves class initialization, and the matches method
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ensure_subdomain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Evan Shreve
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -26,24 +26,26 @@ dependencies:
|
|
26
26
|
version: 3.2.0
|
27
27
|
description: Ensure requests are going to the right subdomain to avoid competing with
|
28
28
|
yourself for dem SERPs. Adds a couple methods to ActionDispatch for routes.
|
29
|
-
email:
|
30
|
-
- jacob@shreve.ly
|
29
|
+
email:
|
31
30
|
executables: []
|
32
31
|
extensions: []
|
33
32
|
extra_rdoc_files: []
|
34
33
|
files:
|
35
34
|
- ".gitignore"
|
35
|
+
- ".travis.yml"
|
36
36
|
- Guardfile
|
37
37
|
- LICENSE
|
38
38
|
- README.md
|
39
|
+
- Rakefile
|
39
40
|
- ensure_subdomain.gemspec
|
41
|
+
- lib/action_dispatch/routing/mapper/http_helpers.rb
|
40
42
|
- lib/ensure_subdomain.rb
|
41
43
|
- lib/ensure_subdomain/version.rb
|
42
44
|
- test/ensure_subdomain_test.rb
|
43
45
|
- test/test_helper.rb
|
44
46
|
homepage: https://github.com/shreve/ensure_subdomain
|
45
47
|
licenses:
|
46
|
-
-
|
48
|
+
- WTFPL
|
47
49
|
metadata: {}
|
48
50
|
post_install_message:
|
49
51
|
rdoc_options: []
|