ensure_subdomain 0.9.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/README.md +1 -3
- data/lib/ensure_subdomain.rb +7 -5
- data/lib/ensure_subdomain/version.rb +4 -4
- data/test/ensure_subdomain_test.rb +84 -0
- metadata +9 -11
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c706bfd647210b33566e2fbb57308663f51ee4cd
|
4
|
+
data.tar.gz: 07180f9ce0482aa1ce23ed97686635d617ad54d6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ce86ba5c71b3c9882a3d2c7a1ab071cd5798abdb3b6d3bfdc7fa19112939b77f13bf0e7c83a81ffe907b99f65a32b78347c07bb7f2c131f6fce2885761745c7e
|
7
|
+
data.tar.gz: 788952e1e1bfc52aaaf6318a1c8952fc691594450a83dbcbedbf7270a3bb02c3beb61af7c330b8d7e5954bf3b960b0387d489259d906b0e825937f10e95d0471
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ensure_subdomain-*.gem
|
data/README.md
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
an ActionDispatch extension to handle subdomain redirects
|
4
4
|
|
5
|
-
#### Warning: There aren't any tests yet, so this is probably not suitable for production. If you'd like to contribute, I wouldn't be opposed.
|
6
|
-
|
7
5
|
Let's say you believe that [using the www subdomain is dumb](http://no-www.org).
|
8
6
|
Ensuring your rails app doesn't use this subdomain is pretty easy, but now it's easier.
|
9
7
|
|
@@ -40,4 +38,4 @@ Finally, if you've got some other domain, there's a method for that too.
|
|
40
38
|
ensure_subdomain 'blog'
|
41
39
|
end
|
42
40
|
|
43
|
-
GETting http://my-app.dev/ will redirect to http://blog.my-app.dev
|
41
|
+
GETting http://my-app.dev/ will redirect to http://blog.my-app.dev
|
data/lib/ensure_subdomain.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
class EnsureSubdomain
|
2
2
|
attr_accessor :subdomain
|
3
|
-
|
3
|
+
|
4
4
|
def initialize(subdomain)
|
5
5
|
self.subdomain = subdomain.sub(/\.$/, '')
|
6
6
|
end
|
7
7
|
|
8
8
|
def matches?(request)
|
9
|
-
|
9
|
+
# ''.match('www') #=> nil, which is is the opposite of what we want
|
10
|
+
(self.subdomain.empty? && request.subdomain.present?) ||
|
11
|
+
request.subdomain.match(self.subdomain).nil?
|
10
12
|
end
|
11
|
-
|
13
|
+
|
12
14
|
def to(params, request)
|
13
15
|
url = request.protocol
|
14
16
|
url << "#{self.subdomain}." if self.subdomain.present?
|
@@ -23,14 +25,14 @@ module ActionDispatch::Routing::Mapper::HttpHelpers
|
|
23
25
|
ensure_subdomain ''
|
24
26
|
end
|
25
27
|
alias_method :ensure_non_www, :ensure_no_www
|
26
|
-
|
28
|
+
|
27
29
|
def ensure_www
|
28
30
|
ensure_subdomain 'www'
|
29
31
|
end
|
30
32
|
|
31
33
|
def ensure_subdomain(subdomain, options={})
|
32
34
|
redirector = EnsureSubdomain.new( subdomain )
|
33
|
-
verbs = options[:via] || [:get, :post, :put, :patch, :delete]
|
35
|
+
verbs = options[:via] || [:get, :post, :put, :patch, :delete]
|
34
36
|
constraints( redirector ) do
|
35
37
|
match '/*path', to: redirect { |params, request| redirector.to params, request }, via: verbs
|
36
38
|
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'action_dispatch'
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/ensure_subdomain')
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
class TestEnsureSubdomain < MiniTest::Unit::TestCase
|
7
|
+
=begin
|
8
|
+
Test that the EnsureSubdomain constraint matching works.
|
9
|
+
Involves class initialization, and the matches method
|
10
|
+
=end
|
11
|
+
def test_empty_subdomain_will_redirect_to_www
|
12
|
+
redirector = www_redirector
|
13
|
+
assert redirector.matches?(empty_subdomain_request),
|
14
|
+
"request with empty subdomain won't redirect with www_redirector"
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_empty_subdomain_will_not_redirect_if_empty
|
18
|
+
redirector = empty_redirector
|
19
|
+
refute redirector.matches?(empty_subdomain_request),
|
20
|
+
"request with empty subdomain will redirect with empty_redirector"
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_www_will_redirect_to_empty
|
24
|
+
redirector = empty_redirector
|
25
|
+
assert redirector.matches?(subdomain_request),
|
26
|
+
"request with www subdomain won't redirect with empty_redirector"
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_www_will_not_redirect_if_www
|
30
|
+
redirector = www_redirector
|
31
|
+
refute redirector.matches?(subdomain_request),
|
32
|
+
"request with www subdomain will redirect with www_redirector"
|
33
|
+
end
|
34
|
+
|
35
|
+
=begin
|
36
|
+
Test that the EnsureSubdomain url generator works.
|
37
|
+
Involves the to method
|
38
|
+
=end
|
39
|
+
def test_www_redirector_generates_www_url
|
40
|
+
redirector = www_redirector
|
41
|
+
assert_equal "http://www.example.com", redirector.to(no_path_params, empty_subdomain_request)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_empty_redirector_generates_empty_url
|
45
|
+
redirector = empty_redirector
|
46
|
+
assert_equal "http://example.com", redirector.to(no_path_params, subdomain_request)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_path_is_included
|
50
|
+
redirector = empty_redirector
|
51
|
+
assert_equal "http://example.com/user/signup", redirector.to(path_params, empty_subdomain_request)
|
52
|
+
end
|
53
|
+
|
54
|
+
=begin
|
55
|
+
Test helpers because I'm too lazy for a separate file
|
56
|
+
=end
|
57
|
+
def www_redirector
|
58
|
+
EnsureSubdomain.new('www')
|
59
|
+
end
|
60
|
+
|
61
|
+
def empty_redirector
|
62
|
+
EnsureSubdomain.new('')
|
63
|
+
end
|
64
|
+
|
65
|
+
def empty_subdomain_request
|
66
|
+
OpenStruct.new(protocol: 'http://', domain: 'example.com', subdomain: '')
|
67
|
+
end
|
68
|
+
|
69
|
+
def subdomain_request(sub='www')
|
70
|
+
base = empty_subdomain_request
|
71
|
+
base[:subdomain] = sub
|
72
|
+
base
|
73
|
+
end
|
74
|
+
|
75
|
+
def no_path_params
|
76
|
+
{ _method: :get }
|
77
|
+
end
|
78
|
+
|
79
|
+
def path_params
|
80
|
+
base = no_path_params
|
81
|
+
base[:path] = 'user/signup'
|
82
|
+
base
|
83
|
+
end
|
84
|
+
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ensure_subdomain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Jacob Evan Shreve
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-24 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: actionpack
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -35,34 +32,35 @@ executables: []
|
|
35
32
|
extensions: []
|
36
33
|
extra_rdoc_files: []
|
37
34
|
files:
|
35
|
+
- .gitignore
|
38
36
|
- LICENSE
|
39
37
|
- README.md
|
40
38
|
- ensure_subdomain.gemspec
|
41
39
|
- lib/ensure_subdomain.rb
|
42
40
|
- lib/ensure_subdomain/version.rb
|
41
|
+
- test/ensure_subdomain_test.rb
|
43
42
|
homepage: https://github.com/shreve/ensure_subdomain
|
44
43
|
licenses:
|
45
44
|
- MIT
|
45
|
+
metadata: {}
|
46
46
|
post_install_message:
|
47
47
|
rdoc_options: []
|
48
48
|
require_paths:
|
49
49
|
- lib
|
50
50
|
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
51
|
requirements:
|
53
|
-
- -
|
52
|
+
- - '>='
|
54
53
|
- !ruby/object:Gem::Version
|
55
54
|
version: '0'
|
56
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
56
|
requirements:
|
59
|
-
- -
|
57
|
+
- - '>='
|
60
58
|
- !ruby/object:Gem::Version
|
61
59
|
version: '0'
|
62
60
|
requirements: []
|
63
61
|
rubyforge_project:
|
64
|
-
rubygems_version:
|
62
|
+
rubygems_version: 2.0.3
|
65
63
|
signing_key:
|
66
|
-
specification_version:
|
64
|
+
specification_version: 4
|
67
65
|
summary: Ensure requests are going to the right subdomain.
|
68
66
|
test_files: []
|