Class: Url
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#proto ⇒ Object
Returns the value of attribute proto.
Class Method Summary collapse
- .current ⇒ Object
- .escape(str = nil) ⇒ Object
- .force_locale(loc) ⇒ Object
-
.prepare_qs(name) ⇒ Object
for search Url.prepare_qs(:q) -> /foo?bar=1&q=.
- .qs(name, value = nil) ⇒ Object
- .subdomain(name, in_path = nil) ⇒ Object
- .unescape(str = nil) ⇒ Object
Instance Method Summary collapse
- #delete(*keys) ⇒ Object
- #domain(what = nil) ⇒ Object
- #hash(val) ⇒ Object
- #host_with_port ⇒ Object
-
#initialize(url) ⇒ Url
constructor
A new instance of Url.
- #locale(what) ⇒ Object
- #namespace(data) ⇒ Object
- #path(val = nil) ⇒ Object
- #qs(name, value = nil) ⇒ Object
- #qs_val ⇒ Object
- #query ⇒ Object
- #relative ⇒ Object
- #subdomain(name) ⇒ Object
- #subdomain=(name) ⇒ Object
- #to_s ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(url) ⇒ Url
Returns a new instance of Url.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/common/url.rb', line 56 def initialize url if url =~ /:/ @elms = url.split '/', 4 else @elms = [url] @elms.unshift '','' end domain_and_port = @elms[2].split(':') @domain_parts = domain_and_port[0].to_s.split('.').reverse.map(&:downcase) @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] @path = path_with_qs[0] || '/' @proto = @elms[0].split(':').first.downcase @port = domain_and_port[1] ? domain_and_port[1].to_i : 80 end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/common/url.rb', line 12 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
12 13 14 |
# File 'lib/common/url.rb', line 12 def port @port end |
#proto ⇒ Object
Returns the value of attribute proto.
12 13 14 |
# File 'lib/common/url.rb', line 12 def proto @proto end |
Class Method Details
.current ⇒ Object
15 16 17 |
# File 'lib/common/url.rb', line 15 def current new Lux.current.request.url end |
.escape(str = nil) ⇒ Object
45 46 47 |
# File 'lib/common/url.rb', line 45 def escape str=nil CGI::escape(str.to_s) end |
.force_locale(loc) ⇒ Object
19 20 21 22 23 24 25 |
# 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 |
.prepare_qs(name) ⇒ Object
for search Url.prepare_qs(:q) -> /foo?bar=1&q=
39 40 41 42 43 |
# File 'lib/common/url.rb', line 39 def prepare_qs name url = current.qs(name).relative url += url.index('?') ? '&' : '?' "#{url}#{name}=" end |
.qs(name, value = nil) ⇒ Object
33 34 35 |
# File 'lib/common/url.rb', line 33 def qs name, value=nil current.qs(name, value).relative end |
.subdomain(name, in_path = nil) ⇒ Object
27 28 29 30 31 |
# 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) ⇒ Object
49 50 51 |
# File 'lib/common/url.rb', line 49 def unescape str=nil CGI::unescape(str.to_s) end |
Instance Method Details
#delete(*keys) ⇒ Object
119 120 121 122 |
# File 'lib/common/url.rb', line 119 def delete *keys keys.map{ |key| @qs.delete(key.to_s) } self end |
#domain(what = nil) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/common/url.rb', line 79 def domain what=nil if what @host = what return self end @domain_parts.slice(0,2).reverse.join '.' end |
#hash(val) ⇒ Object
124 125 126 |
# File 'lib/common/url.rb', line 124 def hash val @hash = "##{val}" end |
#host_with_port ⇒ Object
104 105 106 |
# File 'lib/common/url.rb', line 104 def host_with_port %[#{proto}://#{host}#{port == 80 ? '' : ":#{port}"}] end |
#locale(what) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/common/url.rb', line 142 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) ⇒ Object
137 138 139 140 |
# File 'lib/common/url.rb', line 137 def namespace data @namespace = data.to_s self end |
#path(val = nil) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/common/url.rb', line 112 def path val=nil return '/'+@path+(@namespace ? ":#{@namespace}" : '') unless val @path = val.sub(/^\//,'') self end |
#qs(name, value = nil) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/common/url.rb', line 128 def qs name, value=nil if value @qs[name.to_s] = value elsif name @qs.delete(name.to_s) end self end |
#qs_val ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/common/url.rb', line 153 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 |
#query ⇒ Object
108 109 110 |
# File 'lib/common/url.rb', line 108 def query @query end |
#relative ⇒ Object
165 166 167 |
# File 'lib/common/url.rb', line 165 def relative [path, qs_val, @hash].join('').sub('//','/') end |
#subdomain(name) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/common/url.rb', line 88 def subdomain name if name @domain_parts[2] = name return self end @domain_parts.drop(2).reverse.join('.').or nil end |
#subdomain=(name) ⇒ Object
96 97 98 |
# File 'lib/common/url.rb', line 96 def subdomain= name @domain_parts[2] = name end |
#to_s ⇒ Object
169 170 171 |
# File 'lib/common/url.rb', line 169 def to_s domain.length > 0 ? url : local_url end |
#url ⇒ Object
161 162 163 |
# File 'lib/common/url.rb', line 161 def url [host_with_port, path, qs_val, @hash].join('') end |