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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd51561f2a2662deee047aeec5ac9f7eaffdf0b5
4
- data.tar.gz: c71044ae647c76af59cd9472431eb702ecb3f74d
3
+ metadata.gz: 9796f936f745ffb9ab58df7b1369eed449de3511
4
+ data.tar.gz: 285a076299d2ee829d9db68fd7a1286344999fa0
5
5
  SHA512:
6
- metadata.gz: 56778712f628f749027aa1143cc77e6e4d0c313335b628fa9ff0f0c0e27d696cb700ad5ae56c9ed2189580eac883ebd2737cde9dc4bcf985753637646f4c8284
7
- data.tar.gz: 02f01c1c68610a692bc86c5ac1f0d645bb71cf27bd462285ce96b6915a9a23288ad097b297c9b7c829562352e81440a85ffb0e7d0380749608e1fb29d46c3c69
6
+ metadata.gz: 32ed90c87744f57f92b96bffc1a408985099e06acfe12f0aebba5763dc9ba9e1dea9a46c594e2067c77b84a103850abb8f6d3ec91563a4b9797f3f078d62735d
7
+ data.tar.gz: b0bca224d19fce7a6df03f1850f6418aa3d420680c6e16b8d6595c9cf5045e84420bc67b14161d44ecb9bd821c01aebb280da5eb4f4c80aac1bdbe32ca5d166f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phc_string_format (0.2.0)
4
+ phc_string_format (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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.shift
31
- version = parse_parameter_string(elements.shift)['v'] if (elements.first || '').start_with?('v=')
32
- params = parse_parameter_string(elements.shift) if (elements.first || '').include?('=')
33
- salt = hint.dig(:salt, :encoding) == '7bit' ? elements.shift : short_strict_decode64(elements.shift)
34
- hash = short_strict_decode64(elements.shift)
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.parse_parameter_string(string)
39
- 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)}
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, :parse_parameter_string
64
+ private_class_method :short_strict_encode64, :short_strict_decode64
53
65
  end
54
66
  end
@@ -1,3 +1,3 @@
1
1
  module PhcStringFormat
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  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.2.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-04 00:00:00.000000000 Z
11
+ date: 2018-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler