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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da8068ff68669af009b9d001ca3309828b2738f6
4
- data.tar.gz: 05ce3d57ff7b16fd5d725cf93910c60b72a1079d
3
+ metadata.gz: b64a37df9c84fef7bc29b96d589b9f475459ee59
4
+ data.tar.gz: 3ccfd498f0d7dcebfa4c896b859c5991dd93e1be
5
5
  SHA512:
6
- metadata.gz: a215145db575639703d62321060a8996d5d9e82ce1a8875bc3a230192fb1de281fcd36fc259bf29383846226107c4c274148e5595835fbc34f335e4f59e0630a
7
- data.tar.gz: cc0f9c257f44d171405902ddaa4d90b9a2db40e87c2ea15cdfb73609e8fdd1af3209a8f80c45849ea01d31e77ee67fa5828a6aa826389393dd4076a31af9bc44
6
+ metadata.gz: 3815e20f599be7cb52c502a898ecc5fc675cb80c39c736888cc48e5ae8e7aef70cb5e24f37e34028636d65ddebff3de8b7e9fbf68d27128861016c00db652d71
7
+ data.tar.gz: 372130b0d9e8070ef4005e63b604f6854d89d09315399d6f8ab1f5aab5df84daa68b9e97954989ce0f96bec5361288c8f22a01f110996d185bd90a0ec57b0f5b
data/CHANGELOG.md CHANGED
@@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
8
8
  -
9
9
  ```
10
10
 
11
+ ## [0.4.5] - 2016-12-05
12
+ ### Added
13
+ - URI#merge
14
+ - String#to_query_hash
15
+
11
16
  ## [0.4.4] - 2016-07-18
12
17
  ### Added
13
18
  - URI#build
data/README.md CHANGED
@@ -105,7 +105,7 @@ some_hash.fast_sort_keys #=> some_sorted_hash
105
105
  ```
106
106
 
107
107
  ```ruby
108
- # get a portion even when deleting with zero
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
@@ -1,3 +1,3 @@
1
1
  module CreativeRailsUtilities
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.5"
3
3
  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
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-07-18 00:00:00.000000000 Z
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport