ensure_subdomain 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Guardfile +9 -0
- data/ensure_subdomain.gemspec +12 -12
- data/lib/ensure_subdomain.rb +37 -30
- data/lib/ensure_subdomain/version.rb +1 -1
- data/test/ensure_subdomain_test.rb +47 -66
- data/test/test_helper.rb +58 -0
- metadata +10 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a5b38bc301b07a5d51159dfc71595fb49e1a919
|
4
|
+
data.tar.gz: cf9297a48daaa005a972a1e80003b553d78ba4b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 097c68036be7e3c157399be5db6e3f5db1f3e938f4233debdcfd6b284af229634fe4e8e8fc6e1a481c888a0090d206123a5953d6d12d84b27c4f8d318f882fc4
|
7
|
+
data.tar.gz: 548b8d69cee9fe45c209e97d4edf8a077f50076a5e1eda1b584d1048ebca969a2e6a1d9189ddd7821ba7da1eb5e147fd5b8667ab4ab18af0af21396d6da2d163
|
data/Guardfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :minitest do
|
5
|
+
# with Minitest::Unit
|
6
|
+
watch(%r{^test/(.*)\/?(.*)\_test.rb$})
|
7
|
+
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
|
8
|
+
watch(%r{^test/test_helper\.rb$}) { 'test' }
|
9
|
+
end
|
data/ensure_subdomain.gemspec
CHANGED
@@ -3,18 +3,18 @@
|
|
3
3
|
require File.expand_path('../lib/ensure_subdomain/version', __FILE__)
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
s.name = 'ensure_subdomain'
|
7
|
+
s.version = EnsureSubdomain::VERSION::STRING
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = "Jacob Evan Shreve"
|
10
|
+
s.email = %w(jacob@shreve.ly)
|
11
|
+
s.summary = 'Ensure requests are going to the right subdomain.'
|
12
|
+
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
|
+
s.homepage = 'https://github.com/shreve/ensure_subdomain'
|
14
|
+
s.license = 'MIT'
|
15
15
|
|
16
|
-
|
16
|
+
s.add_runtime_dependency 'actionpack', '>= 4.0.0'
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.require_path = 'lib'
|
20
20
|
end
|
data/lib/ensure_subdomain.rb
CHANGED
@@ -1,40 +1,47 @@
|
|
1
1
|
class EnsureSubdomain
|
2
|
-
|
2
|
+
attr_accessor :subdomain
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def initialize(subdomain)
|
5
|
+
self.subdomain = subdomain.sub(/\.$/, '')
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
def matches?(request)
|
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?
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def to(params, request)
|
15
|
+
url = request.protocol
|
16
|
+
url << "#{self.subdomain}." if self.subdomain.present?
|
17
|
+
url << "#{request.subdomain}." if vendor_url?(request)
|
18
|
+
url << request.domain
|
19
|
+
url << "/#{params[:path]}" if params[:path].present?
|
20
|
+
url
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def vendor_url?(request)
|
25
|
+
request.domain.match /heroku/
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
module ActionDispatch::Routing::Mapper::HttpHelpers
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
30
|
+
def ensure_no_www
|
31
|
+
ensure_subdomain ''
|
32
|
+
end
|
33
|
+
alias_method :ensure_non_www, :ensure_no_www
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
35
|
+
def ensure_www
|
36
|
+
ensure_subdomain 'www'
|
37
|
+
end
|
32
38
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
39
|
+
def ensure_subdomain(subdomain, options={})
|
40
|
+
redirector = EnsureSubdomain.new( subdomain )
|
41
|
+
verbs = options[:via] || [:get, :post, :put, :patch, :delete]
|
42
|
+
constraints( redirector ) do
|
43
|
+
match '/', to: redirect { |params, request| redirector.to params, request }, via: verbs
|
44
|
+
match '/*path', to: redirect { |params, request| redirector.to params, request }, via: verbs
|
45
|
+
end
|
46
|
+
end
|
40
47
|
end
|
@@ -1,84 +1,65 @@
|
|
1
|
-
require '
|
2
|
-
require 'action_dispatch'
|
3
|
-
require File.join(File.dirname(__FILE__), '../lib/ensure_subdomain')
|
4
|
-
require 'ostruct'
|
1
|
+
require 'test_helper'
|
5
2
|
|
6
3
|
class TestEnsureSubdomain < MiniTest::Unit::TestCase
|
7
4
|
=begin
|
8
|
-
|
9
|
-
|
5
|
+
Test that the EnsureSubdomain constraint matching works.
|
6
|
+
Involves class initialization, and the matches method
|
10
7
|
=end
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
def test_empty_subdomain_will_redirect_to_www
|
9
|
+
redirector = www_redirector
|
10
|
+
assert redirector.matches?(empty_subdomain_request),
|
11
|
+
"request with empty subdomain won't redirect with www_redirector"
|
12
|
+
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
def test_empty_subdomain_will_not_redirect_if_empty
|
15
|
+
redirector = empty_redirector
|
16
|
+
refute redirector.matches?(empty_subdomain_request),
|
17
|
+
"request with empty subdomain will redirect with empty_redirector"
|
18
|
+
end
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
def test_www_will_redirect_to_empty
|
21
|
+
redirector = empty_redirector
|
22
|
+
assert redirector.matches?(subdomain_request),
|
23
|
+
"request with www subdomain won't redirect with empty_redirector"
|
24
|
+
end
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
def test_www_will_not_redirect_if_www
|
27
|
+
redirector = www_redirector
|
28
|
+
refute redirector.matches?(subdomain_request),
|
29
|
+
"request with www subdomain will redirect with www_redirector"
|
30
|
+
end
|
34
31
|
|
35
32
|
=begin
|
36
|
-
|
37
|
-
|
33
|
+
Test that the EnsureSubdomain url generator works.
|
34
|
+
Involves the to method
|
38
35
|
=end
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
def test_www_redirector_generates_www_url
|
37
|
+
redirector = www_redirector
|
38
|
+
assert_equal "http://www.example.com", redirector.to(no_path_params, empty_subdomain_request)
|
39
|
+
end
|
43
40
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
41
|
+
def test_empty_redirector_generates_empty_url
|
42
|
+
redirector = empty_redirector
|
43
|
+
assert_equal "http://example.com", redirector.to(no_path_params, subdomain_request)
|
44
|
+
end
|
48
45
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
46
|
+
def test_path_is_included
|
47
|
+
redirector = empty_redirector
|
48
|
+
assert_equal "http://example.com/user/signup", redirector.to(path_params, empty_subdomain_request)
|
49
|
+
end
|
53
50
|
|
54
51
|
=begin
|
55
|
-
|
52
|
+
Test hosting vendor special url compatability
|
53
|
+
ex: http://myapp.herokuapp.com shouldn't redirect to herokuapp.com on ensure_no_subdomain
|
56
54
|
=end
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
def test_no_subdomain_wont_redirect_to_herokuapp
|
56
|
+
redirector = empty_redirector
|
57
|
+
assert_equal "http://example.herokuapp.com", redirector.to(no_path_params, heroku_request)
|
58
|
+
end
|
60
59
|
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
def test_www_subdomain_will_remain_on_herokuapp
|
61
|
+
redirector = www_redirector
|
62
|
+
assert_equal "http://www.example.herokuapp.com", redirector.to(no_path_params, heroku_request)
|
63
|
+
end
|
64
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
65
|
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'action_dispatch'
|
4
|
+
require 'ensure_subdomain'
|
5
|
+
require 'ostruct'
|
6
|
+
|
7
|
+
def www_redirector
|
8
|
+
EnsureSubdomain.new('www')
|
9
|
+
end
|
10
|
+
|
11
|
+
def empty_redirector
|
12
|
+
EnsureSubdomain.new('')
|
13
|
+
end
|
14
|
+
|
15
|
+
def empty_subdomain_request
|
16
|
+
OpenStruct.new(protocol: 'http://', domain: 'example.com', subdomain: '')
|
17
|
+
end
|
18
|
+
|
19
|
+
def subdomain_request(sub='www')
|
20
|
+
base = empty_subdomain_request
|
21
|
+
base[:subdomain] = sub
|
22
|
+
base
|
23
|
+
end
|
24
|
+
|
25
|
+
def heroku_request
|
26
|
+
base = empty_subdomain_request
|
27
|
+
base[:subdomain] = base[:domain].sub('.com', '')
|
28
|
+
base[:domain] = 'herokuapp.com'
|
29
|
+
base
|
30
|
+
end
|
31
|
+
|
32
|
+
def no_path_params
|
33
|
+
{ _method: :get }
|
34
|
+
end
|
35
|
+
|
36
|
+
def path_params
|
37
|
+
base = no_path_params
|
38
|
+
base[:path] = 'user/signup'
|
39
|
+
base
|
40
|
+
end
|
41
|
+
|
42
|
+
def router
|
43
|
+
ActionDispatch::Routing::RouteSet.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def www_router
|
47
|
+
router.draw do
|
48
|
+
ensure_www
|
49
|
+
end
|
50
|
+
router
|
51
|
+
end
|
52
|
+
|
53
|
+
def empty_router
|
54
|
+
router.draw do
|
55
|
+
ensure_no_www
|
56
|
+
end
|
57
|
+
router
|
58
|
+
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
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.0.1
|
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:
|
11
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 4.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 4.0.0
|
27
27
|
description: Ensure requests are going to the right subdomain to avoid competing with
|
@@ -32,13 +32,15 @@ executables: []
|
|
32
32
|
extensions: []
|
33
33
|
extra_rdoc_files: []
|
34
34
|
files:
|
35
|
-
- .gitignore
|
35
|
+
- ".gitignore"
|
36
|
+
- Guardfile
|
36
37
|
- LICENSE
|
37
38
|
- README.md
|
38
39
|
- ensure_subdomain.gemspec
|
39
40
|
- lib/ensure_subdomain.rb
|
40
41
|
- lib/ensure_subdomain/version.rb
|
41
42
|
- test/ensure_subdomain_test.rb
|
43
|
+
- test/test_helper.rb
|
42
44
|
homepage: https://github.com/shreve/ensure_subdomain
|
43
45
|
licenses:
|
44
46
|
- MIT
|
@@ -49,17 +51,17 @@ require_paths:
|
|
49
51
|
- lib
|
50
52
|
required_ruby_version: !ruby/object:Gem::Requirement
|
51
53
|
requirements:
|
52
|
-
- -
|
54
|
+
- - ">="
|
53
55
|
- !ruby/object:Gem::Version
|
54
56
|
version: '0'
|
55
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
58
|
requirements:
|
57
|
-
- -
|
59
|
+
- - ">="
|
58
60
|
- !ruby/object:Gem::Version
|
59
61
|
version: '0'
|
60
62
|
requirements: []
|
61
63
|
rubyforge_project:
|
62
|
-
rubygems_version: 2.
|
64
|
+
rubygems_version: 2.2.2
|
63
65
|
signing_key:
|
64
66
|
specification_version: 4
|
65
67
|
summary: Ensure requests are going to the right subdomain.
|