class Url

Attributes

host[RW]
port[RW]
proto[RW]

Public Class Methods

current() click to toggle source
# File lib/common/url.rb, line 15
def current
  new Lux.current.request.url
end
escape(str=nil) click to toggle source
# File lib/common/url.rb, line 45
def escape str=nil
  CGI::escape(str.to_s)
end
force_locale(loc) click to toggle source
# File lib/common/url.rb, line 19
def force_locale loc
  # u = current
  # u.locale loc
  # u.relative

  '/' + loc.to_s + Lux.current.nav.full
end
new(url) click to toggle source
# File lib/common/url.rb, line 56
def initialize url
  url, qs_part = url.split('?', 2)

  @qs = qs_part.to_s.split('&').inject({}) do |qs, el|
    parts = el.split('=', 2)
    qs[parts[0]] = Url.unescape parts[1]
    qs
  end

  if url =~ %r{://}
    @proto, _, @domain, @path = url.split '/', 4

    @proto = @proto.sub(':', '')

    @domain, @port = @domain.split(':', 2)
    @port = nil if @port == '80' || @port.blank?

    @domain_parts = @domain.split('.').reverse.map(&:downcase)
  else
    @path = url.to_s.sub(%r{^/}, '')
  end

  @path = '' if @path.blank?
end
prepare_qs(name) click to toggle source

for search Url.prepare_qs(:q) -> /foo?bar=1&q=

# File lib/common/url.rb, line 39
def prepare_qs name
  url = current.delete(name).relative
  url += url.index('?') ? '&' : '?'
  "#{url}#{name}="
end
qs(name, value) click to toggle source
# File lib/common/url.rb, line 33
def qs name, value
  current.qs(name, value).relative
end
subdomain(name, in_path=nil) click to toggle source
# File lib/common/url.rb, line 27
def subdomain name,  in_path=nil
  b = current.subdomain(name)
  b.path in_path if in_path
  b.url
end
unescape(str=nil) click to toggle source
# File lib/common/url.rb, line 49
def unescape str=nil
  CGI::unescape(str.to_s)
end

Public Instance Methods

[](key) click to toggle source
# File lib/common/url.rb, line 169
def [] key
  @qs[key.to_s]
end
[]=(key, value) click to toggle source
# File lib/common/url.rb, line 173
def []= key, value
  @qs[key.to_s] = value
end
delete(*keys) click to toggle source
# File lib/common/url.rb, line 120
def delete *keys
  keys.map{ |key| @qs.delete(key.to_s) }
  self
end
domain(what=nil) click to toggle source
# File lib/common/url.rb, line 81
def domain what=nil
  if what
    @host = what
    return self
  end

  @domain_parts.slice(0,2).reverse.join '.'
end
hash(val) click to toggle source
# File lib/common/url.rb, line 125
def hash val
  @hash = "##{val}"
end
locale(what) click to toggle source
# File lib/common/url.rb, line 143
def locale what
  elms = @path.split('/')

  if elms[0] && Locale.all.index(elms[0].to_s)
    elms[0] = what
  else
    elms.unshift what
  end

  @path = elms.join('/')

  self
end
namespace(data) click to toggle source
# File lib/common/url.rb, line 138
def namespace data
  @namespace = data.to_s
  self
end
path(val=nil) click to toggle source
# File lib/common/url.rb, line 111
def path val=nil
  if val
    @path = val.sub /^\//, ''
    return self
  else
    '/' + @path
  end
end
qs(name, value=:_nil) click to toggle source
# File lib/common/url.rb, line 129
def qs name, value=:_nil
  if value != :_nil
    @qs[name.to_s] = value
    self
  elsif name
    @qs[name.to_s]
  end
end
query() click to toggle source
# File lib/common/url.rb, line 107
def query
  @query
end
relative() click to toggle source
# File lib/common/url.rb, line 161
def relative
  [path, qs_val, @hash].join('').sub('//','/')
end
subdomain(name=nil) click to toggle source
# File lib/common/url.rb, line 90
def subdomain name=nil
  if name
    @domain_parts[2] = name
    return self
  end

  @domain_parts.drop(2).reverse.join('.').or nil
end
subdomain=(name) click to toggle source
# File lib/common/url.rb, line 99
def subdomain= name
  @domain_parts[2] = name
end
to_s() click to toggle source
# File lib/common/url.rb, line 165
def to_s
  @domain ? url : relative
end
url() click to toggle source
# File lib/common/url.rb, line 157
def url
  [host_with_port, path, qs_val, @hash].join('')
end

Private Instance Methods

host_with_port() click to toggle source
# File lib/common/url.rb, line 187
def host_with_port
  %[#{proto}://#{host}#{@port.present? ? ":#{@port}" : ''}]
end
qs_val() click to toggle source
# File lib/common/url.rb, line 179
def qs_val
  ret = []
  if @qs.keys.length > 0
    ret.push '?' + @qs.keys.sort.map{ |key| "#{key}=#{Url.escape(@qs[key].to_s)}" }.join('&')
  end
  ret.join('')
end