ensure_subdomain 1.0.3 → 1.1.0

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: 8bfbed2cb42ec1dec6b7db959362ceea0cb69744
4
- data.tar.gz: 108f8ddd0931f49a7b23a4b4a9a6065133f0a0ac
3
+ metadata.gz: d92f9be5e9736803dcf8624689996509100ccc21
4
+ data.tar.gz: ddf4448c07e20ba6e4a812c94e4a9f95d1e658ab
5
5
  SHA512:
6
- metadata.gz: f7433c9c8ac9f077ac8939b4e2c5ae155f829a03ede929b01789ddd03eff470566ac2dd9b1b6a3f0e4580bd10a0611396b4576854455f6bfaeba44987d4d8fbe
7
- data.tar.gz: 408da1bae5482b9d43066c1c22f4f8c11629d1ba17f88808003796ea751b6ca3c4c3a5cebf9a724279ae92b92c21cc70a37aa84a60f3268bfe5e4595e928b01f
6
+ metadata.gz: 3ffcbea839fd66d15f6410f732d6d398253a6147fb58e35a2e2f982863e86049faf25a10c5094c8388f29fa4e96f62dbb0a1abec603e91e7a49f35ef75e867f1
7
+ data.tar.gz: 02ca130fd3eeb8c3ca996562ac1b8af5b5070f411726b98a0395ee448c14b3031250c1ee09df528bb2b729257d4efaadd0a8d07f743590696d3d184fa64ac5cc
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/LICENSE CHANGED
@@ -1,20 +1,13 @@
1
- The MIT License (MIT)
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
2
3
 
3
- Copyright (c) 2013 Jacob Evan Shreve
4
+ Copyright (C) 2014 Jacob Evan Shreve
4
5
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of
6
- this software and associated documentation files (the "Software"), to deal in
7
- the Software without restriction, including without limitation the rights to
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
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
14
12
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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
+ [![Gem Version](https://badge.fury.io/rb/ensure_subdomain.png)](http://badge.fury.io/rb/ensure_subdomain)
6
+ [![Code Climate](https://codeclimate.com/github/shreve/ensure_subdomain.png)](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
- `bundle install`
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
- GET www.example.com → 301 example.com
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 are wrong and think you should use www, there's a method for that.
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
- GET example.com → 301 www.example.com
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
- If you've got some other domain, there's a method for that too.
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
- GET example.com → 301 blog.example.com
61
+ ```
62
+ $ curl -I http://example.com
63
+ HTTP/1.1 301 Moved Permanently
64
+ Location: http://blog.example.com
50
65
 
51
- GET www.example.com → 301 blog.example.com
66
+ $ curl -I http://www.example.com
67
+ HTTP/1.1 301 Moved Permanently
68
+ Location: http://blog.example.com
69
+ ```
52
70
 
53
- What if you want to control the direction for different environments? I've got ya.
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
@@ -0,0 +1,5 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default do
4
+ Dir.glob('./test/*_test.rb').each { |f| require f }
5
+ end
@@ -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 = 'MIT'
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
+
@@ -1,58 +1,55 @@
1
+ require_relative './action_dispatch/routing/mapper/http_helpers'
2
+
1
3
  class EnsureSubdomain
2
- attr_accessor :subdomain
4
+ attr_accessor :subdomains
3
5
 
4
- def initialize(subdomain)
5
- self.subdomain = subdomain.sub(/\.$/, '')
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
- # don't deal with addresses like http://0.0.0.0:3000
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 << "#{self.subdomain}." if self.subdomain.present?
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 vendor_url?(request)
27
- request.domain.match /heroku/
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
- module ActionDispatch::Routing::Mapper::HttpHelpers
32
- def ensure_no_www
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 ensure_www
39
- ensure_subdomain 'www'
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 ensure_on(environments)
43
- environments.each_pair do |env, domain|
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 ensure_subdomain(subdomain, options={})
51
- redirector = EnsureSubdomain.new( subdomain )
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,8 +1,8 @@
1
1
  class EnsureSubdomain
2
2
  module VERSION
3
3
  MAJOR = 1
4
- MINOR = 0
5
- TINY = 3
4
+ MINOR = 1
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -1,6 +1,6 @@
1
- require 'test_helper'
1
+ require_relative './test_helper'
2
2
 
3
- class TestEnsureSubdomain < MiniTest::Unit::TestCase
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.3
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-02-28 00:00:00.000000000 Z
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
- - MIT
48
+ - WTFPL
47
49
  metadata: {}
48
50
  post_install_message:
49
51
  rdoc_options: []