lib/sinatra/subdomain.rb in sinatra-subdomain-0.1.2 vs lib/sinatra/subdomain.rb in sinatra-subdomain-0.2.0
- old
+ new
@@ -1,20 +1,23 @@
require "sinatra/base"
require "uri"
+require "resolv"
module Sinatra
module Subdomain
class << self
attr_accessor :app, :subdomain
end
module Helpers
def subdomain
uri = URI.parse("http://#{request.env["HTTP_HOST"]}")
+ return if Sinatra::Subdomain.ip_address?(uri.host)
parts = uri.host.split(".")
parts.pop(settings.tld_size + 1)
- parts.first
+
+ parts.empty? ? nil : parts.join(".")
end
end
def subdomain(expected_subdomain = true, &block)
::Sinatra::Subdomain.tap do |mod|
@@ -26,9 +29,13 @@
::Sinatra::Subdomain.tap do |mod|
mod.app = nil
mod.subdomain = nil
end
+ end
+
+ def self.ip_address?(host)
+ host =~ Resolv::IPv4::Regex || host =~ Resolv::IPv6::Regex
end
def self.route_added(verb, path, block)
return unless subdomain && app