diff options
author | José Valim <jose.valim@gmail.com> | 2010-12-03 13:27:43 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-12-03 13:27:43 +0100 |
commit | 226ea0e9e817a2e449d989d925c36cf6a4cfd67b (patch) | |
tree | 0f48dd20921a000907962e860a1478c8d1a45070 | |
parent | 78afe68afbfec229db50148c00e48417cd2a352f (diff) | |
download | rails-226ea0e9e817a2e449d989d925c36cf6a4cfd67b.tar.gz rails-226ea0e9e817a2e449d989d925c36cf6a4cfd67b.tar.bz2 rails-226ea0e9e817a2e449d989d925c36cf6a4cfd67b.zip |
Wrap everything in class << self.
-rw-r--r-- | actionpack/lib/action_dispatch/http/url.rb | 79 |
1 files changed, 37 insertions, 42 deletions
diff --git a/actionpack/lib/action_dispatch/http/url.rb b/actionpack/lib/action_dispatch/http/url.rb index 51d4766282..796cd8c09b 100644 --- a/actionpack/lib/action_dispatch/http/url.rb +++ b/actionpack/lib/action_dispatch/http/url.rb @@ -4,55 +4,54 @@ module ActionDispatch mattr_accessor :tld_length self.tld_length = 1 - def self.extract_domain(host, tld_length = @@tld_length) - return nil unless named_host?(host) - - host.split('.').last(1 + tld_length).join('.') - end - - def self.extract_subdomains(host, tld_length = @@tld_length) - return [] unless named_host?(host) - parts = host.split('.') - parts[0..-(tld_length+2)] - end - - def self.extract_subdomain(host, tld_length = @@tld_length) - extract_subdomains(host, tld_length).join('.') - end + class << self + def extract_domain(host, tld_length = @@tld_length) + return nil unless named_host?(host) + host.split('.').last(1 + tld_length).join('.') + end - def self.named_host?(host) - !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) - end + def extract_subdomains(host, tld_length = @@tld_length) + return [] unless named_host?(host) + parts = host.split('.') + parts[0..-(tld_length+2)] + end - def self.url_for(options = {}) - unless options[:host].present? || options[:only_path].present? - raise ArgumentError, 'Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true' + def extract_subdomain(host, tld_length = @@tld_length) + extract_subdomains(host, tld_length).join('.') end - rewritten_url = "" + def url_for(options = {}) + unless options[:host].present? || options[:only_path].present? + raise ArgumentError, 'Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true' + end - unless options[:only_path] - rewritten_url << (options[:protocol] || "http") - rewritten_url << "://" unless rewritten_url.match("://") - rewritten_url << rewrite_authentication(options) - rewritten_url << host_or_subdomain_and_domain(options) - rewritten_url << ":#{options.delete(:port)}" if options[:port] - end + rewritten_url = "" - path = options.delete(:path) || '' + unless options[:only_path] + rewritten_url << (options[:protocol] || "http") + rewritten_url << "://" unless rewritten_url.match("://") + rewritten_url << rewrite_authentication(options) + rewritten_url << host_or_subdomain_and_domain(options) + rewritten_url << ":#{options.delete(:port)}" if options[:port] + end - params = options[:params] || {} - params.reject! {|k,v| !v } + path = options.delete(:path) || '' - rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path) - rewritten_url << "?#{params.to_query}" unless params.empty? - rewritten_url << "##{Rack::Mount::Utils.escape_uri(options[:anchor].to_param.to_s)}" if options[:anchor] - rewritten_url - end + params = options[:params] || {} + params.reject! {|k,v| !v } + + rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path) + rewritten_url << "?#{params.to_query}" unless params.empty? + rewritten_url << "##{Rack::Mount::Utils.escape_uri(options[:anchor].to_param.to_s)}" if options[:anchor] + rewritten_url + end - class << self private + def named_host?(host) + !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) + end + def rewrite_authentication(options) if options[:user] && options[:password] "#{Rack::Utils.escape(options[:user])}:#{Rack::Utils.escape(options[:password])}@" @@ -72,11 +71,8 @@ module ActionDispatch host << (options[:domain] || extract_domain(options[:host], tld_length)) host end - end - - # Returns the complete URL used for this request. def url protocol + host_with_port + fullpath @@ -168,7 +164,6 @@ module ActionDispatch def subdomain(tld_length = @@tld_length) subdomains(tld_length) end - end end end |