creative_rails_utilities 0.4.4 → 0.4.5
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/CHANGELOG.md +5 -0
- data/README.md +17 -1
- data/lib/creative_rails_utilities/string.rb +15 -0
- data/lib/creative_rails_utilities/uri.rb +20 -0
- data/lib/creative_rails_utilities/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b64a37df9c84fef7bc29b96d589b9f475459ee59
|
4
|
+
data.tar.gz: 3ccfd498f0d7dcebfa4c896b859c5991dd93e1be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3815e20f599be7cb52c502a898ecc5fc675cb80c39c736888cc48e5ae8e7aef70cb5e24f37e34028636d65ddebff3de8b7e9fbf68d27128861016c00db652d71
|
7
|
+
data.tar.gz: 372130b0d9e8070ef4005e63b604f6854d89d09315399d6f8ab1f5aab5df84daa68b9e97954989ce0f96bec5361288c8f22a01f110996d185bd90a0ec57b0f5b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -105,7 +105,7 @@ some_hash.fast_sort_keys #=> some_sorted_hash
|
|
105
105
|
```
|
106
106
|
|
107
107
|
```ruby
|
108
|
-
#
|
108
|
+
# basically serves as safe division. Get a portion even when deleting with zero
|
109
109
|
# whole.safe_per(part)
|
110
110
|
100.safe_per(50) #=> 2.0
|
111
111
|
3.12.safe_per(2.6) #=> 1.2
|
@@ -173,6 +173,12 @@ string #=> "1..." instead of " 1..."
|
|
173
173
|
"y".to_bool #=> true
|
174
174
|
```
|
175
175
|
|
176
|
+
```rb
|
177
|
+
# Convert query string into a hash
|
178
|
+
"test=true&apples=yup".to_query_hash
|
179
|
+
#=> {"test" => "true", "apples" => "yup"}
|
180
|
+
```
|
181
|
+
|
176
182
|
##### URI
|
177
183
|
|
178
184
|
```ruby
|
@@ -187,6 +193,16 @@ URI.build(scheme: "https", domain: "example.com", path: "/some/path", query: {te
|
|
187
193
|
#=> "https://example.com/some/path?fake=false&test=true"
|
188
194
|
```
|
189
195
|
|
196
|
+
```rb
|
197
|
+
# URI.merge(uri="", new_query="", replace=false), NB, also accepts hash type new_query argument!
|
198
|
+
# merge new query parameters into an existing uri string
|
199
|
+
|
200
|
+
URI.merge("http://domain.com?test=true", "test=absolutely&apples=yup") # => "http://domain.com?apples=yup&test=absolutely"
|
201
|
+
|
202
|
+
# use the replace trigger to replace the old query parameters with the new ones entirely
|
203
|
+
URI.merge("http://domain.com?test=true", {apples: "yup"}, true) #=> "http://domain.com?apples=yup"
|
204
|
+
```
|
205
|
+
|
190
206
|
#### View Helpers
|
191
207
|
Rails-only (via railtie) view helpers have been added, they are automagically loaded and usable upon gem inclusion.
|
192
208
|
|
@@ -27,4 +27,19 @@ class String
|
|
27
27
|
raise(ArgumentError.new "could not interpret '#{self}' as boolean.")
|
28
28
|
end
|
29
29
|
|
30
|
+
# convert an URI query string into a symbol hash, does not support array query records (yet)
|
31
|
+
def to_query_hash
|
32
|
+
raise ArgumentError.new("Looks like unsupported array query is used, sorry!") if self.to_s[/\[\]\=/].present?
|
33
|
+
|
34
|
+
query_hash = split("&").reduce({}) do |mem, i|
|
35
|
+
key = i.split("=").first
|
36
|
+
value = i.split("=").second
|
37
|
+
|
38
|
+
mem[key.to_s] = value.to_s
|
39
|
+
mem
|
40
|
+
end
|
41
|
+
|
42
|
+
return query_hash
|
43
|
+
end
|
44
|
+
|
30
45
|
end
|
@@ -11,4 +11,24 @@ module URI
|
|
11
11
|
end
|
12
12
|
|
13
13
|
module_function :build
|
14
|
+
|
15
|
+
# use this to merge new query parameters into well-formed URI strings, pass averride as true to replace the query portion with the new one entirely
|
16
|
+
def merge(uri="", new_query="", replace=false)
|
17
|
+
parsed_uri = URI(uri)
|
18
|
+
raise ArgumentError.new(":uri argument is not a well-formed uri string!") if parsed_uri.host.blank?
|
19
|
+
|
20
|
+
query = !new_query.is_a?(String) ? (new_query.try(:to_query).presence || new_query.to_s) : new_query.to_s
|
21
|
+
|
22
|
+
base_uri_options = {scheme: parsed_uri.scheme, domain: parsed_uri.host, path: parsed_uri.path, query: query, fragment: parsed_uri.fragment}
|
23
|
+
|
24
|
+
if !replace
|
25
|
+
query = parsed_uri.query.to_s.to_query_hash.merge(query.to_s.to_query_hash).to_query
|
26
|
+
end
|
27
|
+
|
28
|
+
new_uri = URI.build(**base_uri_options.merge(query: query))
|
29
|
+
|
30
|
+
return new_uri
|
31
|
+
end
|
32
|
+
|
33
|
+
module_function :merge
|
14
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: creative_rails_utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Creative
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|