lux-url 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/.version +1 -0
  3. data/lib/lux-url.rb +301 -0
  4. metadata +41 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b919212898330d26fef868e930b931576173e66fe8dbd09b24a0f3346e9d7ddd
4
+ data.tar.gz: a508a205cc9a4d16bb1504d612430b32010d0246fdca960bfbba4f4631e2e0d3
5
+ SHA512:
6
+ metadata.gz: 81e80276498aa8b77cb8b59ccb92ace8e8afb5380ae2dd0dbd2e477641c651cff06c8b245d4736de2389586f655f2c4db80dc077d6e32f0ca7c3ce4f4e9e3656
7
+ data.tar.gz: 3727a9ffd54aeeafe0389037ffce2bc24538c832dae4f7fa7a65c3765bbcd9340aebdacd55d255fac55eb72d1db61730aada3aeaa0375d4e3b9f694e24166262
data/.version ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
data/lib/lux-url.rb ADDED
@@ -0,0 +1,301 @@
1
+ # frozen_string_literal: true
2
+
3
+ # u = Url.new('https://www.YouTube.com/watch?t=1260&v=cOFSX6nezEY')
4
+ # u.delete :t
5
+ # u.hash '160s'
6
+ # u.to_s -> 'https://com.youtube.www/watch?v=cOFSX6nezEY#160s'
7
+
8
+ require 'cgi'
9
+
10
+ class Url
11
+ OPTS ||= Struct.new(:proto, :port, :subdomain, :domain, :locale, :path, :qs, :qs_hash, :qs_path)
12
+
13
+ class << self
14
+ VERSION = Pathname.new(__FILE__).join('../../.version').read
15
+
16
+ # get current Url, overload for usage outside Lux
17
+ def current
18
+ new Lux.current.request.url
19
+ end
20
+
21
+ def host
22
+ current.host
23
+ end
24
+
25
+ def locale loc
26
+ u = current
27
+ u.locale loc
28
+ u.relative
29
+ end
30
+
31
+ # change current subdomain
32
+ def subdomain name, in_path=nil
33
+ b = current.subdomain(name)
34
+ b.path in_path if in_path
35
+ b.url
36
+ end
37
+
38
+ def qs name, value
39
+ current.qs(name, value).relative
40
+ end
41
+
42
+ # path qs /foo/bar:baz
43
+ def pqs name, value
44
+ url = current.pqs(name, value)
45
+ url.qs name, nil
46
+ url.relative
47
+ end
48
+
49
+ # same as force qs but remove value if selected
50
+ def toggle name, value
51
+ value = nil if Lux.current.params[name].to_s == value.to_s
52
+ qs name, value
53
+ end
54
+
55
+ # for search
56
+ # Url.prepare_qs(:q) -> /foo?bar=1&q=
57
+ def prepare_qs name
58
+ url = current.delete(name).relative
59
+ url += url.index('?') ? '&' : '?'
60
+ "#{url}#{name}="
61
+ end
62
+
63
+ def escape str=nil
64
+ CGI::escape(str.to_s)
65
+ end
66
+
67
+ def unescape str=nil
68
+ CGI::unescape(str.to_s)
69
+ end
70
+
71
+ def root
72
+ new(Lux.current.request.url).host_with_port
73
+ end
74
+ end
75
+
76
+ ###
77
+
78
+ def initialize url
79
+ @opt = OPTS.new
80
+
81
+ url, qs_part = url.split('?', 2)
82
+
83
+ # querysting hash
84
+ qs_part, @opt.qs_hash = qs_part.to_s.split('#')
85
+ @opt.qs_hash = '#%s' % @opt.qs_hash if @opt.qs_hash
86
+
87
+ # querystring
88
+ @opt.qs = qs_part.to_s.split('&').inject({}) do |qs, el|
89
+ parts = el.split('=', 2)
90
+ qs[parts[0]] = Url.unescape parts[1]
91
+ qs
92
+ end
93
+
94
+ # domain and subdomain
95
+ if url =~ %r{^\w+://}
96
+ @opt.proto, _, host, @opt.path = url.split '/', 4
97
+
98
+ @opt.proto = @opt.proto.sub(':', '')
99
+
100
+ host, @opt.port = host.split(':', 2)
101
+ @opt.port = nil if @opt.port == '80' || @opt.port == '443' || @opt.port.to_s.blank?
102
+
103
+ # domain and subdomain
104
+ parts = host.split('.').map(&:downcase)
105
+ @opt.domain = parts.pop(2)
106
+ @opt.domain.unshift parts.pop if @opt.domain.join('').length == 4 # co.uk
107
+ @opt.domain = @opt.domain.join('.')
108
+ @opt.subdomain = parts.first ? parts.join('.') : nil
109
+ else
110
+ @opt.path = url.to_s.sub(%r{^/}, '')
111
+ end
112
+
113
+ # check for locale
114
+ @opt.qs_path ||= {}
115
+ parts = @opt.path.to_s.split('/')
116
+ if parts[0] =~ /^\w{2}$/ || parts[0] =~ /^\w{2}\-\w{2}$/
117
+ @opt.locale = parts.shift
118
+ end
119
+
120
+ while parts.last&.include?(':')
121
+ key, value = parts.pop.split(':')
122
+ @opt.qs_path[key] = value
123
+ end
124
+
125
+ @opt.path = parts.join('/')
126
+
127
+ @opt.path = '' if @opt.path.blank?
128
+ end
129
+
130
+ def prepare_qs name
131
+ url = delete(name).relative
132
+ url += url.index('?') ? '&' : '?'
133
+ "#{url}#{name}="
134
+ end
135
+
136
+ def domain what=nil
137
+ if what
138
+ @opt.domain = what
139
+ self
140
+ else
141
+ @opt.domain
142
+ end
143
+ end
144
+ alias :domain= :domain
145
+
146
+ def subdomain name=nil
147
+ if name
148
+ @opt.subdomain = name
149
+ self
150
+ else
151
+ @opt.subdomain
152
+ end
153
+ end
154
+ alias :subdomain= :subdomain
155
+
156
+ def host
157
+ @opt.subdomain ? [@opt.subdomain, @opt.domain].join('.') : @opt.domain
158
+ end
159
+
160
+ def host_with_port
161
+ %[#{@opt.proto}://#{host}#{!@opt.port.to_s.blank? ? ":#{@opt.port}" : ''}]
162
+ end
163
+
164
+ def path val=nil
165
+ if val
166
+ @opt.path = val.sub /^\//, ''
167
+ return self
168
+ else
169
+ qs_path = @opt.qs_path.to_a
170
+ .select{ !_1[1].blank? }
171
+ .map{ "#{_1[0]}:#{_1[1]}"}
172
+ .join('/')
173
+ qs_path = "/#{qs_path}" unless qs_path.blank?
174
+ @opt.locale ? "/#{@opt.locale}/#{@opt.path}#{qs_path}" : "/#{@opt.path}#{qs_path}"
175
+ end
176
+ end
177
+ alias :path= :path
178
+
179
+ def delete *keys
180
+ keys.map{ |key| @opt.qs.delete(key.to_s) }
181
+ self
182
+ end
183
+
184
+ def hash val
185
+ @opt.qs_hash = "##{val}"
186
+ end
187
+
188
+ def port
189
+ @opt.port
190
+ end
191
+
192
+ def qs name=nil, value=:_nil
193
+ return @opt.qs unless name
194
+
195
+ name = name.to_s
196
+
197
+ if value != :_nil
198
+ if value.nil?
199
+ @opt.qs.delete(name)
200
+ else
201
+ @opt.qs[name] = value
202
+ end
203
+
204
+ self
205
+ elsif name.is_a?(Hash)
206
+ @opt.qs = name.inject(@opt.qs) do |t, el|
207
+ if el[1]
208
+ t[el[0].to_s] = el[1]
209
+ else
210
+ t.delete el[0].to_s
211
+ end
212
+
213
+ t
214
+ end
215
+
216
+ self
217
+ elsif name
218
+ @opt.qs[name] || @opt.qs_path[name]
219
+ end
220
+ end
221
+
222
+ # path query string -> /foo/bar:baz
223
+ def pqs name = nil, value = :_nil
224
+ if value != :_nil
225
+ @opt.qs_path[name.to_s] = CGI::escape value.to_s
226
+ self
227
+ elsif name
228
+ @opt.qs_path[name.to_s]
229
+ else
230
+ @opt.qs_path
231
+ end
232
+ end
233
+ alias :path_qs :pqs
234
+
235
+ def path_prefix
236
+ if @opt.path[0, 1] == ':'
237
+ @opt.path.split(':', 2)[1].split('/').first.split(':')
238
+ else
239
+ []
240
+ end
241
+ end
242
+
243
+ def locale name=nil
244
+ if name
245
+ @opt.locale = name
246
+ self
247
+ else
248
+ @opt.locale
249
+ end
250
+ end
251
+
252
+ def url
253
+ [host_with_port, path, qs_val, @opt.qs_hash].join('')
254
+ end
255
+
256
+ def relative
257
+ [path, qs_val, @opt.qs_hash].join('').sub('//','/')
258
+ end
259
+
260
+ def to_s
261
+ @opt.domain ? url : relative
262
+ end
263
+
264
+ def [] key
265
+ qs key
266
+ end
267
+
268
+ def []= key, value
269
+ @opt.qs[key.to_s] = value
270
+ end
271
+
272
+ def to_h
273
+ {
274
+ proto: @opt.proto,
275
+ port: @opt.port,
276
+ domain: {
277
+ full: host,
278
+ domain: @opt.domain,
279
+ subdomain: subdomain
280
+ },
281
+ locale: @opt.locale,
282
+ path: @opt.path,
283
+ qs: @opt.qs,
284
+ hash: @opt.qs_hash
285
+ }
286
+ end
287
+
288
+ def to_json
289
+ JSON.pretty_generate(to_h)
290
+ end
291
+
292
+ private
293
+
294
+ def qs_val
295
+ ret = []
296
+ if @opt.qs.keys.length > 0
297
+ ret.push '?' + @opt.qs.keys.sort.map{ |key| "#{key}=#{Url.escape(@opt.qs[key].to_s)}" }.join('&')
298
+ end
299
+ ret.join('')
300
+ end
301
+ end
metadata ADDED
@@ -0,0 +1,41 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lux-url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Dino Reic
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: URL object used in Lux framework, allows path attributes and path params
13
+ email: rejotl@gmail.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - "./.version"
19
+ - "./lib/lux-url.rb"
20
+ homepage: http://github.com/dux/lux-url
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubygems_version: 4.0.11
39
+ specification_version: 4
40
+ summary: URL object with extra features
41
+ test_files: []