phc_string_format 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/phc_string_format.rb +21 -9
- data/lib/phc_string_format/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: 9796f936f745ffb9ab58df7b1369eed449de3511
|
4
|
+
data.tar.gz: 285a076299d2ee829d9db68fd7a1286344999fa0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32ed90c87744f57f92b96bffc1a408985099e06acfe12f0aebba5763dc9ba9e1dea9a46c594e2067c77b84a103850abb8f6d3ec91563a4b9797f3f078d62735d
|
7
|
+
data.tar.gz: b0bca224d19fce7a6df03f1850f6418aa3d420680c6e16b8d6595c9cf5045e84420bc67b14161d44ecb9bd821c01aebb280da5eb4f4c80aac1bdbe32ca5d166f
|
data/Gemfile.lock
CHANGED
data/lib/phc_string_format.rb
CHANGED
@@ -24,21 +24,33 @@ module PhcStringFormat
|
|
24
24
|
"$#{elements.select{|e| !(e.nil? || e.empty?)}.join('$')}"
|
25
25
|
end
|
26
26
|
|
27
|
-
def self.parse(string, hint: {salt: {encoding: 'base64'}})
|
27
|
+
def self.parse(string, pick: nil ,hint: {salt: {encoding: 'base64'}})
|
28
|
+
pick = [:id, :version, :params, :salt, :hash] unless pick
|
28
29
|
elements = string.split(/\$/, 6)
|
29
30
|
elements.shift
|
30
|
-
id = elements.
|
31
|
-
version =
|
32
|
-
params =
|
33
|
-
salt =
|
34
|
-
|
31
|
+
id = shift_and_parse elements, pick.include?(:id), &-> (e) { e }
|
32
|
+
version = shift_and_parse(elements, pick.include?(:version), &@@parse_version) if (elements.first || '').start_with?('v=')
|
33
|
+
params = shift_and_parse(elements, pick.include?(:params), &@@parse_parameter_string) if (elements.first || '').include?('=')
|
34
|
+
salt = shift_and_parse elements, pick.include?(:salt), &-> (e) {
|
35
|
+
hint.dig(:salt, :encoding) == '7bit' ? e : short_strict_decode64(e)
|
36
|
+
}
|
37
|
+
hash = shift_and_parse elements, pick.include?(:hash), &-> (e) { short_strict_decode64(e) }
|
35
38
|
{id: id, version: version, params: params, salt: salt, hash: hash}.select {|_,v| v}
|
36
39
|
end
|
37
40
|
|
38
|
-
def self.
|
39
|
-
|
41
|
+
def self.shift_and_parse(elements, picks, &proc)
|
42
|
+
element = elements.shift
|
43
|
+
proc.call(element) if picks
|
40
44
|
end
|
41
45
|
|
46
|
+
@@parse_version = -> (string) {
|
47
|
+
@@parse_parameter_string.call(string)['v']
|
48
|
+
}
|
49
|
+
|
50
|
+
@@parse_parameter_string = -> (string) {
|
51
|
+
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)}
|
52
|
+
}
|
53
|
+
|
42
54
|
def self.short_strict_encode64(bin)
|
43
55
|
return nil unless bin
|
44
56
|
Base64.strict_encode64(bin).delete('=')
|
@@ -49,6 +61,6 @@ module PhcStringFormat
|
|
49
61
|
Base64.strict_decode64(bin + '=' * (-bin.size % 4))
|
50
62
|
end
|
51
63
|
|
52
|
-
private_class_method :short_strict_encode64, :short_strict_decode64
|
64
|
+
private_class_method :short_strict_encode64, :short_strict_decode64
|
53
65
|
end
|
54
66
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phc_string_format
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- naokikimura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|