rack-canonical-host 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7351e7d1c54f3951648e9a1b9f0468d5c631edc0
4
+ data.tar.gz: dedd6608456b3b02233428832026f30ab72560d5
5
+ SHA512:
6
+ metadata.gz: 79f6fb8fab7040d6a002e5c27e521e5c2f3f35760865f3dc80c29da399a22df2bf945b768dd701b7d4709146cf0a7e6b2a9954d7e8e0a3c6063c38e5be84b052
7
+ data.tar.gz: 3ea4f3d955331bf2a96471564c7ba1cf82b57805ed127aac803a646b50f23d7b5ca8b9b5883a1cde7b1358962e074ca457bb152e80d048692c1cadbd80a57adf
data/CHANGELOG.md CHANGED
@@ -1,40 +1,50 @@
1
1
  # Changelog
2
2
 
3
- ## Rack::CanonicalHost 0.0.8
3
+ ## 0.0.9 (2014-02-14)
4
4
 
5
- * Switch to `Addressable::URI` for URI parsing
5
+ * Add `:if` option ([Nick Ostrovsky][firedev])
6
+ * Improve documentation ([Joost Schuur][jschuur])
6
7
 
7
- ## Rack::CanonicalHost 0.0.7
8
+ ## 0.0.8 (2012-06-22)
8
9
 
9
- * Fix an issue handling URLs containing `|` characters
10
+ * Switch to `Addressable::URI` for URI parsing ([Tyler Hunt][tylerhunt])
10
11
 
11
- ## Rack::CanonicalHost 0.0.6
12
+ ## 0.0.7 (2012-06-21)
12
13
 
13
- * Prevent redirect if the canonical host name is `nil`
14
+ * Fix handling of URLs containing `|` characters ([Tyler Hunt][tylerhunt])
14
15
 
15
- ## Rack::CanonicalHost 0.0.5
16
+ ## 0.0.6 (2012-06-21)
16
17
 
17
- * Rename `ignored_hosts` option to `ignore`
18
+ * Prevent redirect if the canonical host is `nil` ([Tyler Hunt][tylerhunt])
18
19
 
19
- ## Rack::CanonicalHost 0.0.4
20
+ ## 0.0.5 (2012-06-21)
21
+
22
+ * Rename `ignored_hosts` option to `ignore` ([Tyler Hunt][tylerhunt])
23
+
24
+ ## 0.0.4 (2012-06-20)
20
25
 
21
26
  * Add option to ignored certain hosts ([Eric Allam][rubymaverick])
22
27
  * Add tests ([Nathaniel Bibler][nbibler])
23
28
  * Add HTML response body on redirect
24
29
  * Set `Content-Type` header on redirect ([Jon Wood][jellybob])
30
+ * Improve documentation ([Peter Baker][finack])
25
31
 
26
- ## Rack::CanonicalHost 0.0.3
32
+ ## 0.0.3 (2011-02-09)
27
33
 
28
- * Allow `env` to be passed to the optional block
34
+ * Allow `env` to be passed to the optional block ([Tyler Hunt][tylerhunt])
29
35
 
30
- ## Rack::CanonicalHost 0.0.2
36
+ ## 0.0.2 (2010-11-18)
31
37
 
32
- * Move `CanonicalHost` into `Rack` namespace
38
+ * Move `CanonicalHost` into `Rack` namespace ([Tyler Hunt][tylerhunt])
33
39
 
34
- ## Rack::CanonicalHost 0.0.1
40
+ ## 0.0.1 (2009-11-04)
35
41
 
36
- * Initial release
42
+ * Initial release ([Tyler Hunt][tylerhunt])
37
43
 
44
+ [finack]: http://github.com/finack
45
+ [firedev]: http://github.com/firedev
38
46
  [jellybob]: http://github.com/jellybob
47
+ [jschuur]: http://github.com/jschuur
39
48
  [nbibler]: http://github.com/nbibler
40
- [rubymaverick]: http://github.com/rubymaverick
49
+ [rubymaverick]: http://github.com/ericallam
50
+ [tylerhunt]: http://github.com/tylerhunt
data/README.md CHANGED
@@ -80,14 +80,22 @@ Alternatively, you can pass a block whose return value will be used as the
80
80
  canonical host name.
81
81
 
82
82
  ``` ruby
83
- use Rack::CanonicalHost do
84
- case ENV['RACK_ENV'].to_sym
85
- when :staging then 'example.com'
86
- when :production then 'staging.example.com'
83
+ use Rack::CanonicalHost do |env|
84
+ case env['RACK_ENV'].to_sym
85
+ when :staging then 'staging.example.com'
86
+ when :production then 'example.com'
87
87
  end
88
88
  end
89
89
  ```
90
90
 
91
+ If you want it to react only on specific hosts for multi-domain environment use `:if`,
92
+ it accepts string, regexp or an array.
93
+
94
+ ``` ruby
95
+ use Rack::CanonicalHost, 'example.com', if: /.*\.example\.com/
96
+ use Rack::CanonicalHost, 'example.ru', if: /.*\.example\.ru/
97
+ ```
98
+
91
99
 
92
100
  ## Contributing
93
101
 
@@ -103,10 +111,11 @@ end
103
111
  Thanks to the following people who have contributed patches or helpful
104
112
  suggestions:
105
113
 
106
- * [Peter Baker](https://github.com/finack)
114
+ * [Joost Schuur](https://github.com/jellybob)
107
115
  * [Jon Wood](https://github.com/jellybob)
116
+ * [Peter Baker](https://github.com/finack)
108
117
  * [Nathaniel Bibler](https://github.com/nbibler)
109
- * [Eric Allam](https://github.com/rubymaverick)
118
+ * [Eric Allam](https://github.com/ericallam)
110
119
  * [Fabrizio Regini](https://github.com/freegenie)
111
120
 
112
121
 
data/Rakefile CHANGED
@@ -1,3 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
 
3
3
  require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -1,5 +1,6 @@
1
1
  require 'rack'
2
2
  require 'rack/canonical_host/redirect'
3
+ require 'rack/canonical_host/version'
3
4
 
4
5
  module Rack
5
6
  class CanonicalHost
@@ -21,9 +22,10 @@ module Rack
21
22
  end
22
23
  end
23
24
 
25
+ private
26
+
24
27
  def host(env)
25
28
  @block ? @block.call(env) || @host : @host
26
29
  end
27
- private :host
28
30
  end
29
31
  end
@@ -3,7 +3,7 @@ require 'addressable/uri'
3
3
  module Rack
4
4
  class CanonicalHost
5
5
  class Redirect
6
- HTML_TEMPLATE = <<-HTML
6
+ HTML_TEMPLATE = <<-HTML.gsub(/^\s+/, '')
7
7
  <!DOCTYPE html>
8
8
  <html lang="en-US">
9
9
  <head><title>301 Moved Permanently</title></head>
@@ -17,11 +17,12 @@ module Rack
17
17
  def initialize(env, host, options={})
18
18
  @env = env
19
19
  @host = host
20
- @ignore = options[:ignore]
20
+ @ignore = Array(options[:ignore])
21
+ @if = Array(options[:if])
21
22
  end
22
23
 
23
24
  def canonical?
24
- known? || ignored?
25
+ known? || ignored? || !conditions_match?
25
26
  end
26
27
 
27
28
  def response
@@ -29,25 +30,34 @@ module Rack
29
30
  [301, headers, [HTML_TEMPLATE % new_url]]
30
31
  end
31
32
 
33
+ private
34
+
32
35
  def known?
33
36
  @host.nil? || request_uri.host == @host
34
37
  end
35
- private :known?
36
38
 
37
39
  def ignored?
38
40
  @ignore && @ignore.include?(request_uri.host)
39
41
  end
40
- private :ignored?
42
+
43
+ def conditions_match?
44
+ return true unless @if.size > 0
45
+ @if.include?( request_uri.host ) || any_regexp_match?( @if, request_uri.host )
46
+ end
47
+ private :conditions_match?
48
+
49
+ def any_regexp_match?( regexp_array, string )
50
+ regexp_array.any?{ |r| string[r] }
51
+ end
52
+ private :any_regexp_match?
41
53
 
42
54
  def new_url
43
55
  request_uri.tap { |uri| uri.host = @host }.to_s
44
56
  end
45
- private :new_url
46
57
 
47
58
  def request_uri
48
59
  Addressable::URI.parse(Rack::Request.new(@env).url)
49
60
  end
50
- private :request_uri
51
61
  end
52
62
  end
53
63
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class CanonicalHost
3
- VERSION = '0.0.8'
3
+ VERSION = '0.0.9'
4
4
  end
5
5
  end
@@ -69,7 +69,7 @@ describe Rack::CanonicalHost do
69
69
  end
70
70
 
71
71
  context 'with :ignore option' do
72
- let(:app) { build_app('example.com', :ignore => ['example.net']) }
72
+ let(:app) { build_app('example.com', :ignore => 'example.net') }
73
73
 
74
74
  include_context 'matching and non-matching requests'
75
75
 
@@ -85,6 +85,38 @@ describe Rack::CanonicalHost do
85
85
  end
86
86
  end
87
87
 
88
+ context 'with :if option' do
89
+
90
+ let(:app) { build_app('example.com', :if => 'www.example.net') }
91
+
92
+ context 'with a request to a :if matching host' do
93
+ let(:url) { 'http://www.example.net/full/path' }
94
+ it { should be_redirect.to('http://example.com/full/path') }
95
+ end
96
+
97
+ context 'with a request to a :if non-matching host' do
98
+ let(:url) { 'http://www.sexample.net/full/path' }
99
+ it { should_not be_redirect }
100
+ end
101
+
102
+ end
103
+
104
+ context 'with :if and regexp as an option' do
105
+
106
+ let(:app) { build_app('example.com', :if => /.*\.example\.net/) }
107
+
108
+ context 'with a request to a :if matching host' do
109
+ let(:url) { 'http://subdomain.example.net/full/path' }
110
+ it { should be_redirect.to('http://example.com/full/path') }
111
+ end
112
+
113
+ context 'with a request to a :if non-matching host' do
114
+ let(:url) { 'http://example.net/full/path' }
115
+ it { should_not be_redirect }
116
+ end
117
+
118
+ end
119
+
88
120
  context 'with a block' do
89
121
  let(:app) { build_app { 'example.com' } }
90
122
 
metadata CHANGED
@@ -1,36 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-canonical-host
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
5
- prerelease:
4
+ version: 0.0.9
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tyler Hunt
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-06-22 00:00:00.000000000 Z
11
+ date: 2014-03-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: addressable
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
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
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rack
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -83,27 +76,26 @@ files:
83
76
  - spec/support/matchers/be_redirect.rb
84
77
  homepage: http://github.com/tylerhunt/rack-canonical-host
85
78
  licenses: []
79
+ metadata: {}
86
80
  post_install_message:
87
81
  rdoc_options: []
88
82
  require_paths:
89
83
  - lib
90
84
  required_ruby_version: !ruby/object:Gem::Requirement
91
- none: false
92
85
  requirements:
93
- - - ! '>='
86
+ - - '>='
94
87
  - !ruby/object:Gem::Version
95
88
  version: '0'
96
89
  required_rubygems_version: !ruby/object:Gem::Requirement
97
- none: false
98
90
  requirements:
99
- - - ! '>='
91
+ - - '>='
100
92
  - !ruby/object:Gem::Version
101
93
  version: '0'
102
94
  requirements: []
103
95
  rubyforge_project:
104
- rubygems_version: 1.8.24
96
+ rubygems_version: 2.0.14
105
97
  signing_key:
106
- specification_version: 3
98
+ specification_version: 4
107
99
  summary: Rack middleware for defining a canonical host name.
108
100
  test_files:
109
101
  - spec/rack/canonical_host_spec.rb