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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c706bfd647210b33566e2fbb57308663f51ee4cd
4
- data.tar.gz: 07180f9ce0482aa1ce23ed97686635d617ad54d6
3
+ metadata.gz: 3a5b38bc301b07a5d51159dfc71595fb49e1a919
4
+ data.tar.gz: cf9297a48daaa005a972a1e80003b553d78ba4b0
5
5
  SHA512:
6
- metadata.gz: ce86ba5c71b3c9882a3d2c7a1ab071cd5798abdb3b6d3bfdc7fa19112939b77f13bf0e7c83a81ffe907b99f65a32b78347c07bb7f2c131f6fce2885761745c7e
7
- data.tar.gz: 788952e1e1bfc52aaaf6318a1c8952fc691594450a83dbcbedbf7270a3bb02c3beb61af7c330b8d7e5954bf3b960b0387d489259d906b0e825937f10e95d0471
6
+ metadata.gz: 097c68036be7e3c157399be5db6e3f5db1f3e938f4233debdcfd6b284af229634fe4e8e8fc6e1a481c888a0090d206123a5953d6d12d84b27c4f8d318f882fc4
7
+ data.tar.gz: 548b8d69cee9fe45c209e97d4edf8a077f50076a5e1eda1b584d1048ebca969a2e6a1d9189ddd7821ba7da1eb5e147fd5b8667ab4ab18af0af21396d6da2d163
@@ -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
@@ -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
- 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'
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
- s.add_dependency 'actionpack', '~>4.0.0'
16
+ s.add_runtime_dependency 'actionpack', '>= 4.0.0'
17
17
 
18
- s.files = `git ls-files`.split("\n")
19
- s.require_path = 'lib'
18
+ s.files = `git ls-files`.split("\n")
19
+ s.require_path = 'lib'
20
20
  end
@@ -1,40 +1,47 @@
1
1
  class EnsureSubdomain
2
- attr_accessor :subdomain
2
+ attr_accessor :subdomain
3
3
 
4
- def initialize(subdomain)
5
- self.subdomain = subdomain.sub(/\.$/, '')
6
- end
4
+ def initialize(subdomain)
5
+ self.subdomain = subdomain.sub(/\.$/, '')
6
+ end
7
7
 
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
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
- def to(params, request)
15
- url = request.protocol
16
- url << "#{self.subdomain}." if self.subdomain.present?
17
- url << request.domain
18
- url << "/#{params[:path]}" if params[:path].present?
19
- url
20
- end
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
- def ensure_no_www
25
- ensure_subdomain ''
26
- end
27
- alias_method :ensure_non_www, :ensure_no_www
30
+ def ensure_no_www
31
+ ensure_subdomain ''
32
+ end
33
+ alias_method :ensure_non_www, :ensure_no_www
28
34
 
29
- def ensure_www
30
- ensure_subdomain 'www'
31
- end
35
+ def ensure_www
36
+ ensure_subdomain 'www'
37
+ end
32
38
 
33
- def ensure_subdomain(subdomain, options={})
34
- redirector = EnsureSubdomain.new( subdomain )
35
- verbs = options[:via] || [:get, :post, :put, :patch, :delete]
36
- constraints( redirector ) do
37
- match '/*path', to: redirect { |params, request| redirector.to params, request }, via: verbs
38
- end
39
- end
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
@@ -2,7 +2,7 @@ class EnsureSubdomain
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -1,84 +1,65 @@
1
- require 'minitest/autorun'
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
- Test that the EnsureSubdomain constraint matching works.
9
- Involves class initialization, and the matches method
5
+ Test that the EnsureSubdomain constraint matching works.
6
+ Involves class initialization, and the matches method
10
7
  =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
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
- 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
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
- 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
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
- 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
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
- Test that the EnsureSubdomain url generator works.
37
- Involves the to method
33
+ Test that the EnsureSubdomain url generator works.
34
+ Involves the to method
38
35
  =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
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
- 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
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
- 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
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
- Test helpers because I'm too lazy for a separate file
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
- def www_redirector
58
- EnsureSubdomain.new('www')
59
- end
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
- def empty_redirector
62
- EnsureSubdomain.new('')
63
- end
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
@@ -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.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: 2013-11-24 00:00:00.000000000 Z
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.0.3
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.