./lib/common/url.rb in lux-fw-0.2.3 vs ./lib/common/url.rb in lux-fw-0.5.32

- old
+ new

@@ -28,18 +28,18 @@ b = current.subdomain(name) b.path in_path if in_path b.url end - def qs name, value=nil + def qs name, value current.qs(name, value).relative end # for search # Url.prepare_qs(:q) -> /foo?bar=1&q= def prepare_qs name - url = current.qs(name).relative + url = current.delete(name).relative url += url.index('?') ? '&' : '?' "#{url}#{name}=" end def escape str=nil @@ -52,30 +52,32 @@ end ### def initialize url - if url =~ /:/ - @elms = url.split '/', 4 - else - @elms = [url] - @elms.unshift '','' + 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 - domain_and_port = @elms[2].split(':') - @domain_parts = domain_and_port[0].to_s.split('.').reverse.map(&:downcase) + if url =~ %r{://} + @proto, _, @domain, @path = url.split '/', 4 - @qs = {} - path_with_qs = @elms[3].to_s.split(/\?|#/) - path_with_qs[1].split('&').map do |el| - parts = el.split('=') - @qs[parts[0]] = Url.unescape parts[1] - end if path_with_qs[1] + @proto = @proto.sub(':', '') - @path = path_with_qs[0] || '/' - @proto = @elms[0].split(':').first.downcase - @port = domain_and_port[1] ? domain_and_port[1].to_i : 80 + @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 def domain what=nil if what @host = what @@ -83,15 +85,16 @@ end @domain_parts.slice(0,2).reverse.join '.' end - def subdomain name + def subdomain name=nil if name @domain_parts[2] = name return self end + @domain_parts.drop(2).reverse.join('.').or nil end def subdomain= name @domain_parts[2] = name @@ -99,23 +102,21 @@ def host @domain_parts.reverse.join '.' end - def host_with_port - %[#{proto}://#{host}#{port == 80 ? '' : ":#{port}"}] - end - def query @query end def path val=nil - return '/'+@path+(@namespace ? ":#{@namespace}" : '') unless val - - @path = val.sub(/^\//,'') - self + if val + @path = val.sub /^\//, '' + return self + else + '/' + @path + end end def delete *keys keys.map{ |key| @qs.delete(key.to_s) } self @@ -123,51 +124,68 @@ def hash val @hash = "##{val}" end - def qs name, value=nil - if value + def qs name, value=:_nil + if value != :_nil @qs[name.to_s] = value + self elsif name - @qs.delete(name.to_s) + @qs[name.to_s] end - self end def namespace data @namespace = data.to_s self end 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 - 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 - def url [host_with_port, path, qs_val, @hash].join('') end def relative [path, qs_val, @hash].join('').sub('//','/') end def to_s - domain.length > 0 ? url : local_url + @domain ? url : relative + end + + def [] key + @qs[key.to_s] + end + + def []= key, value + @qs[key.to_s] = value + end + + private + + 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 + + def host_with_port + %[#{proto}://#{host}#{@port.present? ? ":#{@port}" : ''}] end end \ No newline at end of file