rack-www-enforcer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-www-enforcer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Guillermo Iguaran
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ Rack::WwwRedirect
2
+ =================
3
+
4
+ Rack Middleware for redirect requests to an specific subdomain of your app. (Ex. http://mysite.com to https://app.mysite.com)
5
+
6
+ Usage
7
+ -------
8
+
9
+ use Rack::WwwRedirect, :subdomain => "app"
10
+
11
+ If subdomain isn't present "www" is used by default
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.warning = true
8
+ end
9
+
@@ -0,0 +1 @@
1
+ require "rack/www-enforcer"
@@ -0,0 +1,36 @@
1
+ require "rack"
2
+ require "rack/request"
3
+
4
+ module Rack
5
+ class WwwEnforcer
6
+
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ @subdomain = options[:subdomain] || "www"
10
+ end
11
+
12
+ def call(env)
13
+ req = Request.new(env)
14
+ if subdomain_present?(req)
15
+ @app.call(env)
16
+ else
17
+ redirect_to_subdomain(req)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def subdomain_present?(req)
24
+ host = URI(req.url).host
25
+ host.match(/^([^.]+)\..*$/)[1] == @subdomain
26
+ end
27
+
28
+ def redirect_to_subdomain(req)
29
+ url = URI(req.url)
30
+ url.host = "#{@subdomain}.#{url.host}"
31
+ headers = {'Content-Type' => 'text/html', 'Location' => url.to_s}
32
+
33
+ [301, headers, []]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "rack-www-enforcer"
5
+ s.version = "0.0.1"
6
+ s.authors = ["Guillermo Iguaran"]
7
+ s.email = ["guilleiguaran@gmail.com"]
8
+ s.homepage = "https://github.com/guilleiguaran/rack-www-enforcer"
9
+ s.summary = %q{Redirect requests to a subdomain in your app}
10
+ s.description = <<-EOS
11
+ Rack middleware to redirect requests to an specific subdomain in your app.
12
+ EOS
13
+
14
+ s.add_dependency 'rack'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,34 @@
1
+ $:.push File.expand_path("../../lib", __FILE__)
2
+ require 'rack-www-enforcer'
3
+
4
+ require 'test/unit'
5
+ require 'rack/test'
6
+
7
+ class TestWwwEnforcer < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def mock_app
11
+ lambda { |env|
12
+ headers = {'Content-Type' => "text/html"}
13
+ [200, headers, ["OK"]]
14
+ }
15
+ end
16
+
17
+ def app
18
+ @app ||= Rack::WwwEnforcer.new(mock_app)
19
+ end
20
+ attr_writer :app
21
+
22
+ def test_redirect_to_default_subdomain
23
+ get "http://example.org/"
24
+ assert_equal "http://www.example.org/",
25
+ last_response.headers['Location']
26
+ end
27
+
28
+ def test_redirect_to_custom_subdomain
29
+ self.app = Rack::WwwEnforcer.new(mock_app, :subdomain => "app")
30
+ get "http://example.org/"
31
+ assert_equal "http://app.example.org/",
32
+ last_response.headers['Location']
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-www-enforcer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guillermo Iguaran
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &2157511420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2157511420
25
+ description: ! ' Rack middleware to redirect requests to an specific subdomain
26
+ in your app.
27
+
28
+ '
29
+ email:
30
+ - guilleiguaran@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - .gitignore
36
+ - Gemfile
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/rack-www-enforcer.rb
41
+ - lib/rack/www-enforcer.rb
42
+ - rack-www-enforcer.gemspec
43
+ - test/test_www-enforcer.rb
44
+ homepage: https://github.com/guilleiguaran/rack-www-enforcer
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Redirect requests to a subdomain in your app
68
+ test_files:
69
+ - test/test_www-enforcer.rb