simple-url 0.0.1 → 0.0.3
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.
- checksums.yaml +4 -4
- data/LICENSE +24 -0
- data/lib/url.rb +2 -0
- data/readme.md +102 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0c8a6dbc59d68c1a775dad5f3bf9a3596eb41aa8ad1d2da3774a11822bb1102
|
4
|
+
data.tar.gz: 026665ece6eddedcfb9638b203dad6400f0ea5d87fd186c9335051b3e9e73810
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12fd9d790cefba9ad4fcaea5123338f337340e71202d40f1346609cccc959f48cabac3ed9686b8c303221979948c1a84739b18223fd7aaec259b81f3ae8fe9bc
|
7
|
+
data.tar.gz: b4bb33efa8c65a2a52f1634f9a787681dd7f978af53193a15fb88b3c5537d9f78f177b0787595217a4a069d87a575d69555e30616be3950f701b9d48d81d4ed1
|
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org>
|
data/lib/url.rb
CHANGED
data/readme.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Simple URL
|
2
|
+
|
3
|
+
ruby's `URI` is a very simple way to deal with...well, uri.
|
4
|
+
|
5
|
+
the problem is the `search` (query string).
|
6
|
+
|
7
|
+
dealing with search string by hand is really annoying
|
8
|
+
and error prune. as the search has no form specified on the RFC,
|
9
|
+
we can play with it, it opens a lot of interesting ideas
|
10
|
+
to providing a better way to manage it.
|
11
|
+
|
12
|
+
an example is a search that works with key/values pairs
|
13
|
+
that can contains duplicated keys:
|
14
|
+
|
15
|
+
```txt
|
16
|
+
a=ok&a=meh
|
17
|
+
```
|
18
|
+
|
19
|
+
if this is not a good idea for you application,
|
20
|
+
we could implement a query string that uses a hashmap
|
21
|
+
as storage.
|
22
|
+
|
23
|
+
a required interface is:
|
24
|
+
|
25
|
+
```java
|
26
|
+
interface QueryString {
|
27
|
+
static QueryString from_string(String query_string);
|
28
|
+
String to_s();
|
29
|
+
}
|
30
|
+
```
|
31
|
+
|
32
|
+
```rb
|
33
|
+
class HashmapSearch
|
34
|
+
def self.from_string(query_string)
|
35
|
+
ary = URI.decode_www_form(query_string || '')
|
36
|
+
new(ary)
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(query)
|
40
|
+
# if a keys is present many times,
|
41
|
+
# the last one will get on the hash.
|
42
|
+
@query = query.empty? ? {} : Hash[query]
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
URI.encode_www_form(@query)
|
47
|
+
end
|
48
|
+
|
49
|
+
# operations is up to you!
|
50
|
+
|
51
|
+
def add(key, value)
|
52
|
+
@query[key] = value
|
53
|
+
end
|
54
|
+
|
55
|
+
def [](key)
|
56
|
+
@query[key]
|
57
|
+
end
|
58
|
+
|
59
|
+
def remove(key)
|
60
|
+
@query = @query.reject do |pair|
|
61
|
+
k, = pair
|
62
|
+
k == key
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
getting everything together...
|
69
|
+
|
70
|
+
```rb
|
71
|
+
irb> a = SimpleUrl::Url.new('https://okokok.com/path?a=1&a=2', HashmapSearch)
|
72
|
+
=> #<SimpleUrl::Url:0x00000000017447e8 @url=#<URI::HTTPS https://okokok.com/path?a=1&a=2>, @query_string_class=HashmapSearch, @query=#<HashmapSea...
|
73
|
+
irb> a.query
|
74
|
+
=> #<HashmapSearch:0x0000000001744338 @query={"a"=>"2"}>
|
75
|
+
irb> a.url.path = "/asdf"
|
76
|
+
=> "/asdf"
|
77
|
+
irb> a.to_s
|
78
|
+
=> "https://okokok.com/asdf?a=2"
|
79
|
+
irb> a.query.add('b', 2)
|
80
|
+
=> 2
|
81
|
+
irb> a.to_s
|
82
|
+
=> "https://okokok.com/path?a=2&b=2"
|
83
|
+
irb(main):039:0> a.query.add('a', 43)
|
84
|
+
=> 43
|
85
|
+
irb(main):040:0> a.to_s
|
86
|
+
=> "https://okokok.com/path?a=43&b=2"
|
87
|
+
|
88
|
+
irb> a = SimpleUrl::Url.new('https://okokok.com/path?a=1&a=2', SimpleUrl::KeyValueQueryString)
|
89
|
+
=> #<SimpleUrl::Url:0x0000000003c32f78 @url=#<URI::HTTPS https://okokok.com/path?a=1&a=2>, @query_string_class=SimpleUrl::KeyValueQueryString, @q...
|
90
|
+
irb> a.query.to_s
|
91
|
+
=> "a=1&a=2"
|
92
|
+
irb(main):043:0> a.query.add('a', 3)
|
93
|
+
=> [["a", "1"], ["a", "2"], ["a", 3]]
|
94
|
+
irb(main):044:0> a.query.to_s
|
95
|
+
=> "a=1&a=2&a=3"
|
96
|
+
```
|
97
|
+
|
98
|
+
## license
|
99
|
+
|
100
|
+
This code is released under `Unlicense`.
|
101
|
+
|
102
|
+
See `License`
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-url
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Dias
|
@@ -16,8 +16,10 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- LICENSE
|
19
20
|
- lib/key_value_query_string.rb
|
20
21
|
- lib/url.rb
|
22
|
+
- readme.md
|
21
23
|
homepage: https://rubygems.org/gems/simple-url
|
22
24
|
licenses:
|
23
25
|
- Unlicense
|
@@ -30,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
32
|
requirements:
|
31
33
|
- - ">="
|
32
34
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
35
|
+
version: '2.6'
|
34
36
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
37
|
requirements:
|
36
38
|
- - ">="
|