./lib/rack/subdomain.rb in rack-subdomain-0.2.1 vs ./lib/rack/subdomain.rb in rack-subdomain-0.3.0
- old
+ new
@@ -1,8 +1,8 @@
module Rack
class Subdomain
- VERSION = '0.2.1'
+ VERSION = '0.3.0'
def initialize(app, domain, options = {}, &block)
# Maintain compatibility with previous rack-subdomain gem
options = {to: options} if options.is_a? String
@@ -18,11 +18,11 @@
instance_eval &block
else
raise ArgumentError, "missing `:to` option or block to define mapping"
end
end
-
+
def call(env)
@env = env
@subdomain = subdomain
if @subdomain && !@subdomain.empty? && !@options[:except].include?(@subdomain)
@@ -47,16 +47,27 @@
@mappings[regexp] = route
end
private
+ def domain
+ case @domain
+ when Proc
+ @domain.call(@env['HTTP_HOST'])
+ when Regexp
+ @domain.match(@env['HTTP_HOST'])
+ else
+ @domain
+ end
+ end
+
def subdomain
- @env['HTTP_HOST'].sub(/\.?#{@domain}.*$/,'') unless @env['HTTP_HOST'].match(/^localhost/)
+ @env['HTTP_HOST'].sub(/\.?#{domain}.*$/,'') unless @env['HTTP_HOST'].match(/^localhost/)
end
def remap_with_substituted_path!(path)
scheme = @env["rack.url_scheme"]
- host = "#{@subdomain}.#{@domain}"
+ host = "#{@subdomain}.#{domain}"
port = ":#{@env['SERVER_PORT']}" unless @env['SERVER_PORT'] == '80'
path = @env["PATH_INFO"] = ::File.join(path, @env["PATH_INFO"])
query_string = "?" + @env["QUERY_STRING"] unless @env["QUERY_STRING"].empty?
@env["REQUEST_URI"] = "#{scheme}://#{host}#{port}#{path}#{query_string}"