rack-subdomain 0.0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-subdomain (0.2.1)
5
+ rack (>= 1.2.0, <= 2.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ minitest (2.11.4)
11
+ rack (1.4.1)
12
+ rack-test (0.6.1)
13
+ rack (>= 1.0)
14
+ rake (0.9.2.2)
15
+ rspec (0.6.4)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ minitest (~> 2.11.4)
22
+ rack-subdomain!
23
+ rack-test (~> 0.6.1)
24
+ rake (~> 0.9.2)
25
+ rspec (~> 0.6.1)
data/LICENSE CHANGED
@@ -1,20 +1,19 @@
1
- Copyright (c) 2009 Piotr Sarnacki
1
+ Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
2
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:
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
10
9
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
13
12
 
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.
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # rack-subdomain
2
+
3
+ Rack middleware to transparently route requests with a subdomain to a specified path with substitutions.
4
+
5
+ ## Usage
6
+
7
+ ### Gemfile
8
+
9
+ ``` ruby
10
+ gem 'rack-subdomain'
11
+ ```
12
+
13
+ ### config.ru
14
+
15
+ ``` ruby
16
+
17
+ # Simple Example
18
+
19
+ use Rack::Subdomain, "example.com", to: "/users/:subdomain"
20
+
21
+ # Complex Example
22
+ use Rack::Subdomain, "example.com", except: ['', 'www', 'secure'] do
23
+ map 'downloads', to: "/downloads/:subdomain"
24
+ map '*', to: "/users/:subdomain"
25
+ end
26
+ ```
27
+
28
+ ## Contact
29
+
30
+ Mattt Thompson
31
+
32
+ - http://github.com/mattt
33
+ - http://twitter.com/mattt
34
+ - m@mattt.me
35
+
36
+ ## License
37
+
38
+ rack-subdomain is available under the MIT license. See the LICENSE file for more info.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ gemspec = eval(File.read("rack-subdomain.gemspec"))
5
+
6
+ task :build => "#{gemspec.full_name}.gem"
7
+
8
+ file "#{gemspec.full_name}.gem" => gemspec.files + ["rack-subdomain.gemspec"] do
9
+ system "gem build rack-subdomain.gemspec"
10
+ system "gem install rack-subdomain-#{Rack::Subdomain::VERSION}.gem"
11
+ end
@@ -0,0 +1 @@
1
+ require 'rack/subdomain'
@@ -1,44 +1,66 @@
1
1
  module Rack
2
2
  class Subdomain
3
- def initialize(app, domain, map_to = nil)
4
- @domain = domain
3
+ VERSION = '0.2.1'
4
+
5
+ def initialize(app, domain, options = {}, &block)
6
+ # Maintain compatibility with previous rack-subdomain gem
7
+ options = {to: options} if options.is_a? String
8
+
9
+ @options = {except: ['', 'www']}.merge(options)
10
+
5
11
  @app = app
6
- @map_to = map_to
7
- end
12
+ @domain = domain
13
+ @mappings = {}
8
14
 
15
+ if @options[:to]
16
+ map('*', @options[:to])
17
+ elsif block_given?
18
+ instance_eval &block
19
+ else
20
+ raise ArgumentError, "missing `:to` option or block to define mapping"
21
+ end
22
+ end
23
+
9
24
  def call(env)
10
25
  @env = env
11
- subdomain = fetch_subdomain
12
- set_params(subdomain)
26
+ @subdomain = subdomain
27
+
28
+ if @subdomain && !@subdomain.empty? && !@options[:except].include?(@subdomain)
29
+ pattern, route = @mappings.detect do |pattern, route|
30
+ pattern === subdomain
31
+ end
32
+
33
+ if route
34
+ remap_with_substituted_path!(route.gsub(/\:subdomain/, @subdomain))
35
+ end
36
+ end
13
37
 
14
38
  @app.call(env)
15
39
  end
16
40
 
17
- def fetch_subdomain
18
- subdomain = @env['SERVER_NAME'].sub(/\.?#{@domain}$/,'')
41
+ def map(pattern, route)
42
+ map(pattern, route[:to]) and return if route.kind_of?(Hash)
43
+ raise ArgumentError, "missing route" unless route
19
44
 
20
- ['', 'www'].include?(subdomain) ? nil : subdomain
45
+ pattern = /.*/ if pattern == '*'
46
+ regexp = Regexp.new(pattern)
47
+ @mappings[regexp] = route
21
48
  end
22
49
 
23
- def set_params(subdomain)
24
- request = Rack::Request.new(@env)
25
- if subdomain
26
- request[:subdomain] = subdomain
27
- map_to(subdomain)
28
- end
50
+ private
51
+
52
+ def subdomain
53
+ @env['HTTP_HOST'].sub(/\.?#{@domain}.*$/,'') unless @env['HTTP_HOST'].match(/^localhost/)
29
54
  end
30
55
 
31
- def map_to(subdomain)
32
- if @map_to
33
- @map_to.gsub!(":subdomain", subdomain)
34
- new_path_info = "#{@map_to}#{@env["PATH_INFO"]}"
35
- @env["PATH_INFO"] = new_path_info
36
- new_request_uri = new_path_info.dup
37
- if @env["QUERY_STRING"]
38
- new_request_uri << "?" << @env["QUERY_STRING"]
39
- end
40
- @env["REQUEST_URI"] = new_request_uri
41
- end
56
+ def remap_with_substituted_path!(path)
57
+ scheme = @env["rack.url_scheme"]
58
+ host = "#{@subdomain}.#{@domain}"
59
+ port = ":#{@env['SERVER_PORT']}" unless @env['SERVER_PORT'] == '80'
60
+ path = @env["PATH_INFO"] = ::File.join(path, @env["PATH_INFO"])
61
+ query_string = "?" + @env["QUERY_STRING"] unless @env["QUERY_STRING"].empty?
62
+
63
+ @env["REQUEST_URI"] = "#{scheme}://#{host}#{port}#{path}#{query_string}"
42
64
  end
43
65
  end
44
66
  end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack-subdomain"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-subdomain"
7
+ s.authors = ["Mattt Thompson", "Piotr Sarnacki"]
8
+ s.email = "m@mattt.me"
9
+ s.homepage = "http://github.com/mattt/rack-subdomain"
10
+ s.version = Rack::Subdomain::VERSION
11
+ s.platform = Gem::Platform::RUBY
12
+ s.summary = "rack-subdomain"
13
+ s.description = "Rack middleware to route requests with subdomains to specified routes with substitutions"
14
+
15
+ s.add_development_dependency "rspec", "~> 0.6.1"
16
+ s.add_development_dependency "rake", "~> 0.9.2"
17
+
18
+ s.add_runtime_dependency "rack", ">= 1.2.0", "<= 2.0.0"
19
+
20
+ s.add_development_dependency "minitest", "~> 2.11.4"
21
+ s.add_development_dependency "rack-test", "~> 0.6.1"
22
+ s.add_development_dependency "rake", "~> 0.9.2"
23
+
24
+ s.files = Dir["./**/*"].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+ end
metadata CHANGED
@@ -1,90 +1,162 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rack-subdomain
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 2
9
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
8
+ - Mattt Thompson
12
9
  - Piotr Sarnacki
13
10
  autorequire:
14
11
  bindir: bin
15
12
  cert_chain: []
16
-
17
- date: 2010-06-18 00:00:00 +02:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
13
+ date: 2012-12-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
21
16
  name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.6.1
23
+ type: :development
22
24
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 0.6.1
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 0.9.2
30
39
  type: :development
31
- version_requirements: *id001
32
- - !ruby/object:Gem::Dependency
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.9.2
47
+ - !ruby/object:Gem::Dependency
33
48
  name: rack
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.0
55
+ - - <=
56
+ - !ruby/object:Gem::Version
57
+ version: 2.0.0
58
+ type: :runtime
59
+ prerelease: false
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.2.0
66
+ - - <=
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 2.11.4
77
+ type: :development
34
78
  prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- segments:
40
- - 0
41
- version: "0"
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: 2.11.4
85
+ - !ruby/object:Gem::Dependency
86
+ name: rack-test
87
+ requirement: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ version: 0.6.1
42
93
  type: :development
43
- version_requirements: *id002
44
- description: Rack::Subdomain can fetch subdomain from request and manipulate PATH_INFO to rewrite url with fetched subdomain
45
- email:
46
- - drogus@gmail.com
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ~>
99
+ - !ruby/object:Gem::Version
100
+ version: 0.6.1
101
+ - !ruby/object:Gem::Dependency
102
+ name: rake
103
+ requirement: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ~>
107
+ - !ruby/object:Gem::Version
108
+ version: 0.9.2
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ~>
115
+ - !ruby/object:Gem::Version
116
+ version: 0.9.2
117
+ description: Rack middleware to route requests with subdomains to specified routes
118
+ with substitutions
119
+ email: m@mattt.me
47
120
  executables: []
48
-
49
121
  extensions: []
50
-
51
122
  extra_rdoc_files: []
52
-
53
- files:
54
- - lib/rack/subdomain.rb
55
- - LICENSE
56
- - README.rdoc
57
- has_rdoc: true
58
- homepage: http://github.com/drogus/rack-subdomain
123
+ files:
124
+ - ./Gemfile
125
+ - ./Gemfile.lock
126
+ - ./lib/rack/subdomain.rb
127
+ - ./lib/rack-subdomain.rb
128
+ - ./LICENSE
129
+ - ./rack-subdomain.gemspec
130
+ - ./Rakefile
131
+ - ./README.md
132
+ homepage: http://github.com/mattt/rack-subdomain
59
133
  licenses: []
60
-
61
134
  post_install_message:
62
135
  rdoc_options: []
63
-
64
- require_paths:
136
+ require_paths:
65
137
  - lib
66
- required_ruby_version: !ruby/object:Gem::Requirement
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- segments:
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ segments:
145
+ - 0
146
+ hash: -539744315784908145
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ segments:
71
154
  - 0
72
- version: "0"
73
- required_rubygems_version: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- segments:
78
- - 1
79
- - 3
80
- - 6
81
- version: 1.3.6
155
+ hash: -539744315784908145
82
156
  requirements: []
83
-
84
157
  rubyforge_project:
85
- rubygems_version: 1.3.6
158
+ rubygems_version: 1.8.24
86
159
  signing_key:
87
160
  specification_version: 3
88
- summary: Rack middleware for fetching subdomains
161
+ summary: rack-subdomain
89
162
  test_files: []
90
-
data/README.rdoc DELETED
@@ -1,25 +0,0 @@
1
- = rack-subdomain
2
-
3
- Simple rack middleware for fetching subdomain from request and modyfing path with fetched subdomain.
4
-
5
- == Usage
6
-
7
- Example for rails app:
8
-
9
- config.middleware.use(Rack::Subdomain, "example.org", "/users/:subdomain")
10
-
11
- This will get subdomains for domain example.org (ie. something.example.org) and will map path to /users/:subdomain/original/path.
12
-
13
- == Note on Patches/Pull Requests
14
-
15
- * Fork the project.
16
- * Make your feature addition or bug fix.
17
- * Add tests for it. This is important so I don't break it in a
18
- future version unintentionally.
19
- * Commit, do not mess with rakefile, version, or history.
20
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
21
- * Send me a pull request. Bonus points for topic branches.
22
-
23
- == Copyright
24
-
25
- Copyright (c) 2010 Piotr Sarnacki. See LICENSE for details.