Module: SubdomainFu

Defined in:
lib/subdomain_fu/engine.rb,
lib/subdomain_fu/subdomain_fu.rb

Defined Under Namespace

Modules: Controller Classes: Configuration, Engine

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Object) config

Returns the value of attribute config



3
4
5
# File 'lib/subdomain_fu/subdomain_fu.rb', line 3

def config
  @config
end

Class Method Details

+ (Object) change_subdomain_of_host(subdomain, host)

Changes the subdomain of the host to whatever is passed in.



111
112
113
114
115
# File 'lib/subdomain_fu/subdomain_fu.rb', line 111

def self.change_subdomain_of_host(subdomain, host)
  host = SubdomainFu.host_without_subdomain(host)
  host = "#{subdomain}.#{host}" if subdomain
  host
end

+ (Object) configure {|self.config| ... }

The configurable options of Subdomain Fu. Use like so:

SubdomainFu.configure do |config|
  config.tld_size = 2
  config.preferred_mirror = 'www'
end

Available configurations are:

tld_size:

The size of the top-level domain. For example, 'localhost' is 0, 'example.com' is 1, and 'example.co.uk' is 2.

mirrors:

An array of subdomains that should be equivalent to no subdomain. Defaults to ['www'].

preferred_mirror: The preferred mirror subdomain to which to rewrite URLs. No subdomain is used by default.

override_only_path:

If true, changing the subdomain will emit a full URL in url_for options, even if it wouldn't have otherwise.

Yields:



24
25
26
27
# File 'lib/subdomain_fu/subdomain_fu.rb', line 24

def self.configure
  self.config ||= Configuration.new
  yield(self.config)
end

+ (Object) current_domain(request)

returns nil or the domain or ip Enables subdomain-fu to more completely replace DHH's account_location plugin



169
170
171
172
173
174
# File 'lib/subdomain_fu/subdomain_fu.rb', line 169

def self.current_domain(request)
  return request.domain unless has_domain?(request.domain)
  domain = ""
  domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.length > 1
  domain << request.domain.to_s + request.port_string
end

+ (Object) current_subdomain(request)

returns nil or the subdomain(s)



158
159
160
161
162
163
164
165
# File 'lib/subdomain_fu/subdomain_fu.rb', line 158

def self.current_subdomain(request)
  subdomain = (request.subdomains(SubdomainFu.config.tld_size) - config.mirrors).join(".")
  if has_subdomain?(subdomain)
    subdomain
  else
    nil
  end
end

+ (Boolean) has_domain?(host)

Returns:

  • (Boolean)


59
60
61
# File 'lib/subdomain_fu/subdomain_fu.rb', line 59

def self.has_domain?(host)
  !host.blank? && !(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end

+ (Boolean) has_subdomain?(subdomain)

Is the current subdomain either nil or not a mirror?

Returns:

  • (Boolean)


64
65
66
# File 'lib/subdomain_fu/subdomain_fu.rb', line 64

def self.has_subdomain?(subdomain)
  subdomain != false && !subdomain.blank? && !SubdomainFu.config.mirrors.include?(subdomain)
end

+ (Object) host_without_subdomain(host)



91
92
93
94
# File 'lib/subdomain_fu/subdomain_fu.rb', line 91

def self.host_without_subdomain(host)
  parts = host.split('.')
  Array(parts[-(config.tld_size+1)..-1]).join(".")
end

+ (Boolean) is_mirror?(subdomain)

Returns:

  • (Boolean)


68
69
70
# File 'lib/subdomain_fu/subdomain_fu.rb', line 68

def self.is_mirror?(subdomain)
  subdomain != false && !subdomain.blank? && SubdomainFu.config.mirrors.include?(subdomain)
end

+ (Boolean) needs_rewrite?(subdomain, host)

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/subdomain_fu/subdomain_fu.rb', line 133

def self.needs_rewrite?(subdomain, host)
  return false unless host && host.count('.') >= config.tld_size
  case subdomain
    when nil
      #rewrite when there is a preferred mirror set and there is no subdomain on the host
      return config.preferred_mirror && subdomain_from(host).nil?
    when false
      h = subdomain_from(host)
      #if the host has a subdomain
      if !h.nil?
        #rewrite when there is a subdomain in the host, and it is not a preferred mirror
        return true if !preferred_mirror?(h)
        #rewrite when there is a preferred mirror set and the subdomain of the host is not a mirror
        return true if config.preferred_mirror && !is_mirror?(h)
        #no rewrite if host already has mirror subdomain
        #it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
        return false if is_mirror?(h)
      end
  end

  (!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
    !same_subdomain?(subdomain, host)
end

+ (Object) non_mirror_subdomain_from(host)

Gets only non-mirror subdomains from the host based on the TLD size



86
87
88
89
# File 'lib/subdomain_fu/subdomain_fu.rb', line 86

def self.non_mirror_subdomain_from(host)
  sub = subdomain_from(host)
  has_subdomain?(sub) ? sub : nil
end

+ (Boolean) override_only_path?

Returns:

  • (Boolean)


129
130
131
# File 'lib/subdomain_fu/subdomain_fu.rb', line 129

def self.override_only_path?
  config.override_only_path
end

+ (Boolean) preferred_mirror?(subdomain)

Is the subdomain a preferred mirror

Returns:

  • (Boolean)


73
74
75
# File 'lib/subdomain_fu/subdomain_fu.rb', line 73

def self.preferred_mirror?(subdomain)
  subdomain == SubdomainFu.config.preferred_mirror || SubdomainFu.config.preferred_mirror.nil?
end

+ (Object) rewrite_host_for_subdomains(subdomain, host)

Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/subdomain_fu/subdomain_fu.rb', line 97

def self.rewrite_host_for_subdomains(subdomain, host)
  if needs_rewrite?(subdomain, host)
    change_subdomain_of_host(subdomain || SubdomainFu.config.preferred_mirror, host)
  else
    if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host)) ||
        (subdomain.nil? && has_subdomain?(subdomain_from(host)))
      host
    else
      change_subdomain_of_host(SubdomainFu.config.preferred_mirror, host)
    end
  end
end

+ (Boolean) same_host?(subdomain_host, host)

Is the host without subdomain equivalent to the subdomain_host in this subdomain_host string?

Returns:

  • (Boolean)


125
126
127
# File 'lib/subdomain_fu/subdomain_fu.rb', line 125

def self.same_host?(subdomain_host, host)
  SubdomainFu.host_without_subdomain(host) == subdomain_host
end

+ (Boolean) same_subdomain?(subdomain, host)

Is this subdomain equivalent to the subdomain found in this host string?

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/subdomain_fu/subdomain_fu.rb', line 118

def self.same_subdomain?(subdomain, host)
  subdomain = nil unless subdomain
  (subdomain == subdomain_from(host)) ||
    (!has_subdomain?(subdomain) && !has_subdomain?(subdomain_from(host)))
end

+ (Object) subdomain_from(host)

Gets the subdomain from the host based on the TLD size



78
79
80
81
82
83
# File 'lib/subdomain_fu/subdomain_fu.rb', line 78

def self.subdomain_from(host)
  return nil unless has_domain?(host)
  parts = host.split('.')
  sub = parts[0..-(SubdomainFu.config.tld_size+2)].join(".")
  sub.blank? ? nil : sub
end