phc_string_format 0.1.3 → 0.1.4
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/Gemfile.lock +1 -1
- data/lib/phc_string_format.rb +4 -10
- data/lib/phc_string_format/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 387297ecdd03a3f1a103a1c852be5a2ecd07abc3
|
4
|
+
data.tar.gz: 83a0e1784b853a92de75aa9c75829b5999409cab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2d21cde31f35746242a8eaf48bd996b01adc3aa24698133cbf82f1104dbbf6aa304d125d1e767662d7498c2ead5ea8a5e75ae704e93a489a91779fadf958189
|
7
|
+
data.tar.gz: 46daddb661e5f9b879cdfd2e8487fbd570b20a3aa88165cabe4c205ea6557e6819db0bbc98d46a7dc9e3df0a6c41b08c13d7ba3efee4c369f43af837abd3d625
|
data/Gemfile.lock
CHANGED
data/lib/phc_string_format.rb
CHANGED
@@ -9,24 +9,20 @@ require "base64"
|
|
9
9
|
# ```
|
10
10
|
#
|
11
11
|
module PhcStringFormat
|
12
|
-
|
13
12
|
module Formatter
|
14
|
-
|
15
|
-
#
|
16
13
|
def self.format(id:, params: {}, salt: '', hash: '', hint: {salt: {encoding: 'base64'}})
|
17
14
|
raise ArgumentError.new, 'id is required' if id.nil?
|
18
15
|
raise ArgumentError.new, 'hash needs salt' if (salt.nil? || salt.empty?) && !(hash.nil? || hash.empty?)
|
19
16
|
|
20
17
|
elements = [
|
21
18
|
id,
|
22
|
-
(params.map
|
19
|
+
(params.map{|e| e.join '='}.join(',') if params),
|
23
20
|
hint.dig(:salt, :encoding) == '7bit' ? salt : short_strict_encode64(salt),
|
24
21
|
short_strict_encode64(hash)
|
25
22
|
]
|
26
|
-
"$#{elements.select
|
23
|
+
"$#{elements.select{|e| !(e.nil? || e.empty?)}.join('$')}"
|
27
24
|
end
|
28
25
|
|
29
|
-
#
|
30
26
|
def self.parse(string, hint: {salt: {encoding: 'base64'}})
|
31
27
|
elements = string.split(/\$/, 5)
|
32
28
|
elements.shift
|
@@ -34,11 +30,11 @@ module PhcStringFormat
|
|
34
30
|
params = parse_parameter_string(elements.shift) if (elements.first || '').include?('=')
|
35
31
|
salt = hint.dig(:salt, :encoding) == '7bit' ? elements.shift : short_strict_decode64(elements.shift)
|
36
32
|
hash = short_strict_decode64(elements.shift)
|
37
|
-
{id: id, params: params, salt: salt, hash: hash }
|
33
|
+
{id: id, params: params, salt: salt, hash: hash}.select {|_,v| v}
|
38
34
|
end
|
39
35
|
|
40
36
|
def self.parse_parameter_string(string)
|
41
|
-
string.split(/,/).map {|e| e.split '='}.each_with_object({})
|
37
|
+
string.split(/,/).map {|e| e.split '='}.each_with_object({}){|e, h| k, v = e; h[k] = (/\A-?\d+(.\d+)?\Z/.match(v) ? v.to_i : v)}
|
42
38
|
end
|
43
39
|
|
44
40
|
def self.short_strict_encode64(bin)
|
@@ -52,7 +48,5 @@ module PhcStringFormat
|
|
52
48
|
end
|
53
49
|
|
54
50
|
private_class_method :short_strict_encode64, :short_strict_decode64, :parse_parameter_string
|
55
|
-
|
56
51
|
end
|
57
|
-
|
58
52
|
end
|