Sha256: 1967ea96a910a613ff8595b11b39565dc8e848e37baf820f894ee827c87038dc

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module SubdomainRoutes
  class TooManySubdomains < StandardError
    # TODO: should this just be an ActionController::RoutingError instead? Any benefit to having a separate error type?
    # OK, keep the special codes, but catch and re-case them in UrlWriter.
    # (The errors are also raised in extract_request_environment...)
  end

  module SplitHost
    private
    
    def split_host(host)
      raise HostNotSupplied, "No host supplied!" if host.blank?
      raise HostNotSupplied, "Can't set subdomain for an IP address!" if host =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
      parts = host.split('.')
      if Config.domain_length
        domain_parts = [ ]
        Config.domain_length.times { domain_parts.unshift parts.pop }
        if parts.size > 1
          raise TooManySubdomains, "Multiple subdomains found: #{parts.join('.')}. (Have you set SubdomainRoutes::Config.domain_length correctly?)"
        end
        [ parts.pop.to_s, domain_parts.join('.') ]
      else
        [ parts.shift.to_s, parts.join('.') ]
      end
    end
    
    def domain_for_host(host)
      split_host(host).last
    end
    
    def subdomain_for_host(host)
      split_host(host).first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mholling-subdomain_routes-0.1.0 lib/subdomain_routes/split_host.rb