Class: Url

Inherits:
Object
  • Object
show all
Defined in:
lib/lux-url.rb

Constant Summary collapse

VERSION =
Pathname.new(__FILE__).join('../../.version').read

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Url

Returns a new instance of Url.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lux-url.rb', line 78

def initialize url
  @opt = OPTS.new

  url, qs_part = url.split('?', 2)

  # querysting hash
  qs_part, @opt.qs_hash = qs_part.to_s.split('#')
  @opt.qs_hash = '#%s' % @opt.qs_hash if @opt.qs_hash

  # querystring
  @opt.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 subdomain
  if url =~ %r{^\w+://}
    @opt.proto, _, host, @opt.path = url.split '/', 4

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

    host, @opt.port = host.split(':', 2)
    @opt.port = nil if @opt.port == '80' || @opt.port == '443' || @opt.port.to_s.blank?

    # domain and subdomain
    parts = host.split('.').map(&:downcase)
    @opt.domain = parts.pop(2)
    @opt.domain.unshift parts.pop if @opt.domain.join('').length == 4 # co.uk
    @opt.domain = @opt.domain.join('.')
    @opt.subdomain = parts.first ? parts.join('.') : nil
  else
    @opt.path = url.to_s.sub(%r{^/}, '')
  end

  # check for locale
  @opt.qs_path ||= {}
  parts = @opt.path.to_s.split('/')
  if parts[0] =~ /^\w{2}$/ || parts[0] =~ /^\w{2}\-\w{2}$/
    @opt.locale = parts.shift
  end

  while parts.last&.include?(':')
    key, value = parts.pop.split(':')
    @opt.qs_path[key] = value
  end

  @opt.path   = parts.join('/')

  @opt.path = '' if @opt.path.blank?
end

Class Method Details

.currentObject

get current Url, overload for usage outside Lux



17
18
19
# File 'lib/lux-url.rb', line 17

def current
  new Lux.current.request.url
end

.escape(str = nil) ⇒ Object



63
64
65
# File 'lib/lux-url.rb', line 63

def escape str=nil
  CGI::escape(str.to_s)
end

.hostObject



21
22
23
# File 'lib/lux-url.rb', line 21

def host
  current.host
end

.locale(loc) ⇒ Object



25
26
27
28
29
# File 'lib/lux-url.rb', line 25

def locale loc
  u = current
  u.locale loc
  u.relative
end

.pqs(name, value) ⇒ Object

path qs /foo/bar:baz



43
44
45
46
47
# File 'lib/lux-url.rb', line 43

def pqs name, value
  url = current.pqs(name, value)
  url.qs name, nil
  url.relative
end

.prepare_qs(name) ⇒ Object

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



57
58
59
60
61
# File 'lib/lux-url.rb', line 57

def prepare_qs name
  url = current.delete(name).relative
  url += url.index('?') ? '&' : '?'
  "#{url}#{name}="
end

.qs(name, value) ⇒ Object



38
39
40
# File 'lib/lux-url.rb', line 38

def qs name, value
  current.qs(name, value).relative
end

.rootObject



71
72
73
# File 'lib/lux-url.rb', line 71

def root
  new(Lux.current.request.url).host_with_port
end

.subdomain(name, in_path = nil) ⇒ Object

change current subdomain



32
33
34
35
36
# File 'lib/lux-url.rb', line 32

def subdomain name, in_path=nil
  b = current.subdomain(name)
  b.path in_path if in_path
  b.url
end

.toggle(name, value) ⇒ Object

same as force qs but remove value if selected



50
51
52
53
# File 'lib/lux-url.rb', line 50

def toggle name, value
  value = nil if Lux.current.params[name].to_s == value.to_s
  qs name, value
end

.unescape(str = nil) ⇒ Object



67
68
69
# File 'lib/lux-url.rb', line 67

def unescape str=nil
  CGI::unescape(str.to_s)
end

Instance Method Details

#[](key) ⇒ Object



264
265
266
# File 'lib/lux-url.rb', line 264

def [] key
  qs key
end

#[]=(key, value) ⇒ Object



268
269
270
# File 'lib/lux-url.rb', line 268

def []= key, value
  @opt.qs[key.to_s] = value
end

#delete(*keys) ⇒ Object



179
180
181
182
# File 'lib/lux-url.rb', line 179

def delete *keys
  keys.map{ |key| @opt.qs.delete(key.to_s) }
  self
end

#domain(what = nil) ⇒ Object Also known as: domain=



136
137
138
139
140
141
142
143
# File 'lib/lux-url.rb', line 136

def domain what=nil
  if what
    @opt.domain = what
    self
  else
    @opt.domain
  end
end

#hash(val) ⇒ Object



184
185
186
# File 'lib/lux-url.rb', line 184

def hash val
  @opt.qs_hash = "##{val}"
end

#hostObject



156
157
158
# File 'lib/lux-url.rb', line 156

def host
  @opt.subdomain ? [@opt.subdomain, @opt.domain].join('.') : @opt.domain
end

#host_with_portObject



160
161
162
# File 'lib/lux-url.rb', line 160

def host_with_port
  %[#{@opt.proto}://#{host}#{!@opt.port.to_s.blank? ? ":#{@opt.port}" : ''}]
end

#locale(name = nil) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/lux-url.rb', line 243

def locale name=nil
  if name
    @opt.locale = name
    self
  else
    @opt.locale
  end
end

#path(val = nil) ⇒ Object Also known as: path=



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/lux-url.rb', line 164

def path val=nil
  if val
    @opt.path = val.sub /^\//, ''
    return self
  else
    qs_path = @opt.qs_path.to_a
      .select{ !_1[1].blank? }
      .map{ "#{_1[0]}:#{_1[1]}"}
      .join('/')
    qs_path = "/#{qs_path}" unless qs_path.blank?
    @opt.locale ? "/#{@opt.locale}/#{@opt.path}#{qs_path}" : "/#{@opt.path}#{qs_path}"
  end
end

#path_prefixObject



235
236
237
238
239
240
241
# File 'lib/lux-url.rb', line 235

def path_prefix
  if @opt.path[0, 1] == ':'
    @opt.path.split(':', 2)[1].split('/').first.split(':')
  else
    []
  end
end

#portObject



188
189
190
# File 'lib/lux-url.rb', line 188

def port
  @opt.port
end

#pqs(name = nil, value = :_nil) ⇒ Object Also known as: path_qs

path query string -> /foo/bar:baz



223
224
225
226
227
228
229
230
231
232
# File 'lib/lux-url.rb', line 223

def pqs name = nil, value = :_nil
  if value != :_nil
    @opt.qs_path[name.to_s] = CGI::escape value.to_s
    self
  elsif name
    @opt.qs_path[name.to_s]
  else
    @opt.qs_path
  end
end

#prepare_qs(name) ⇒ Object



130
131
132
133
134
# File 'lib/lux-url.rb', line 130

def prepare_qs name
  url = delete(name).relative
  url += url.index('?') ? '&' : '?'
  "#{url}#{name}="
end

#qs(name = nil, value = :_nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/lux-url.rb', line 192

def qs name=nil, value=:_nil
  return @opt.qs unless name

  name = name.to_s

  if value != :_nil
    if value.nil?
      @opt.qs.delete(name)
    else
      @opt.qs[name] = value
    end

    self
  elsif name.is_a?(Hash)
    @opt.qs = name.inject(@opt.qs) do |t, el|
      if el[1]
        t[el[0].to_s] = el[1]
      else
        t.delete el[0].to_s
      end

      t
    end

    self
  elsif name
    @opt.qs[name] || @opt.qs_path[name]
  end
end

#relativeObject



256
257
258
# File 'lib/lux-url.rb', line 256

def relative
  [path, qs_val, @opt.qs_hash].join('').sub('//','/')
end

#subdomain(name = nil) ⇒ Object Also known as: subdomain=



146
147
148
149
150
151
152
153
# File 'lib/lux-url.rb', line 146

def subdomain name=nil
  if name
    @opt.subdomain = name
    self
  else
    @opt.subdomain
  end
end

#to_hObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/lux-url.rb', line 272

def to_h
  {
    proto:  @opt.proto,
    port:   @opt.port,
    domain: {
      full:      host,
      domain:    @opt.domain,
      subdomain: subdomain
    },
    locale: @opt.locale,
    path:   @opt.path,
    qs:     @opt.qs,
    hash:   @opt.qs_hash
  }
end

#to_jsonObject



288
289
290
# File 'lib/lux-url.rb', line 288

def to_json
  JSON.pretty_generate(to_h)
end

#to_sObject



260
261
262
# File 'lib/lux-url.rb', line 260

def to_s
  @opt.domain ? url : relative
end

#urlObject



252
253
254
# File 'lib/lux-url.rb', line 252

def url
  [host_with_port, path, qs_val, @opt.qs_hash].join('')
end