phc_string_format 0.3.2 → 0.3.3
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/formatter.rb +1 -1
- data/lib/phc_string_format/phc_string.rb +43 -31
- data/lib/phc_string_format/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33dff8312aa061f66c90fdabb7d4630ffdb322c2d770e1714fd7f8115335a72e
|
4
|
+
data.tar.gz: 87d4da1c7bd827cad52cf8f748184d11d661f7d1757627b87be932aaeaf16728
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb3038a561c2b36c94d064fa101dec7aa6dd7c49fd4df7b3da269e3d3595ccd8a54b79c1eebd905a42ea1abec0bcfb2fabe4f6cb54cc728d7a172485ced83105
|
7
|
+
data.tar.gz: a4c5bc3223bf5a614b485fe3a55afaf04f039d0123a734bef209d7db42ba85e076b7dfaed2685076433a87189762f43d314b5607c304706b3b41283d1ae5b527
|
data/Gemfile.lock
CHANGED
@@ -3,20 +3,30 @@ module PhcStringFormat
|
|
3
3
|
# Parser for parsing PHC-string-format.
|
4
4
|
#
|
5
5
|
class PhcString
|
6
|
-
def self.validates(name, **
|
7
|
-
@validators ||=
|
8
|
-
@validators
|
6
|
+
def self.validates(name, **options)
|
7
|
+
@validators ||= []
|
8
|
+
@validators << lambda { |object|
|
9
|
+
value = object.instance_variable_get(name)
|
10
|
+
return if options[:allow_nil] && !value
|
11
|
+
regex = options.dig(:format, :with)
|
12
|
+
raise ArgumentError, options[:message] unless !regex || value =~ regex
|
13
|
+
}
|
9
14
|
end
|
10
15
|
|
11
|
-
def self.validate(
|
12
|
-
@validators
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
+
def self.validate(name, **options)
|
17
|
+
@validators ||= []
|
18
|
+
@validators << ->(object) { raise ArgumentError, options[:message] unless object.send(name) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.do_validate(that)
|
22
|
+
@validators.each { |validator| validator.call(that) }
|
23
|
+
that
|
16
24
|
end
|
17
25
|
|
26
|
+
private_class_method :validates, :validate
|
27
|
+
|
18
28
|
# :reek:DuplicateMethodCall { allow_calls: ['elements.shift', 'elements.first'] }
|
19
|
-
def self.parse(string
|
29
|
+
def self.parse(string)
|
20
30
|
string ||= ''
|
21
31
|
elements = string.split(/\$/, 6)
|
22
32
|
elements.shift
|
@@ -26,7 +36,7 @@ module PhcStringFormat
|
|
26
36
|
salt = elements.shift
|
27
37
|
hash = elements.shift
|
28
38
|
begin
|
29
|
-
PhcString.new(id, version, params, salt, hash
|
39
|
+
PhcString.new(id, version, params, salt, hash)
|
30
40
|
rescue ArgumentError
|
31
41
|
raise ParseError
|
32
42
|
end
|
@@ -38,44 +48,37 @@ module PhcStringFormat
|
|
38
48
|
("v=#{version}" if version),
|
39
49
|
(params.map { |entry| entry.join '=' }.join(',') if params),
|
40
50
|
hint.dig(:salt, :encoding) == '7bit' ? salt : B64.encode(salt),
|
41
|
-
B64.encode(hash)
|
42
|
-
hint
|
51
|
+
B64.encode(hash)
|
43
52
|
)
|
44
53
|
end
|
45
54
|
|
46
|
-
validates :@id, message: 'id is non-compliant',
|
55
|
+
validates :@id, message: 'id is non-compliant', format: { with: /\A[a-z0-9-]{1,32}\z/ }
|
47
56
|
validates \
|
48
57
|
:@version_string,
|
49
58
|
message: 'version is non-compliant',
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
message: 'parameters is non-compliant',
|
54
|
-
unless: proc { |params_string|
|
55
|
-
!params_string || !params_string.empty? && params_string.split(',').all? \
|
56
|
-
{ |param| param =~ %r{\A[a-z0-9-]{1,32}=[a-zA-Z0-9/+.-]+\z} }
|
57
|
-
}
|
59
|
+
allow_nil: true,
|
60
|
+
format: { with: /\Av=\d+\z/ }
|
61
|
+
validate :validate_params_string, message: 'parameters is non-compliant'
|
58
62
|
validates \
|
59
63
|
:@encoded_salt,
|
60
64
|
message: 'encoded salt is non-compliant',
|
61
|
-
|
65
|
+
allow_nil: true,
|
66
|
+
format: { with: %r{\A[a-zA-Z0-9/+.-]+\z} }
|
62
67
|
validates \
|
63
68
|
:@encoded_hash,
|
64
69
|
message: 'encoded hash is non-compliant',
|
65
|
-
|
70
|
+
allow_nil: true,
|
71
|
+
format: { with: %r{\A[a-zA-Z0-9/+]+\z} }
|
72
|
+
validate :validate_salt_and_hash, message: 'hash needs salt'
|
66
73
|
|
67
|
-
|
68
|
-
def initialize(id, version_string, params_string, encoded_salt, encoded_hash, hint)
|
74
|
+
def initialize(id, version_string, params_string, encoded_salt, encoded_hash)
|
69
75
|
@id = id
|
70
76
|
@version_string = version_string
|
71
77
|
@params_string = params_string
|
72
78
|
@encoded_salt = encoded_salt
|
73
79
|
@encoded_hash = encoded_hash
|
74
|
-
@hint = hint
|
75
80
|
|
76
|
-
self.class.
|
77
|
-
raise ArgumentError, 'hash needs salt' \
|
78
|
-
if (!encoded_salt || encoded_salt.empty?) && !(!encoded_hash || encoded_hash.empty?)
|
81
|
+
self.class.do_validate self
|
79
82
|
end
|
80
83
|
|
81
84
|
def to_s
|
@@ -88,7 +91,7 @@ module PhcStringFormat
|
|
88
91
|
].reject { |element| !element || element.empty? }.join('$')
|
89
92
|
end
|
90
93
|
|
91
|
-
def to_h(pick
|
94
|
+
def to_h(pick: nil, hint: {})
|
92
95
|
pick ||= %i[id version params salt hash]
|
93
96
|
{
|
94
97
|
id: (@id if pick.include?(:id)),
|
@@ -96,7 +99,7 @@ module PhcStringFormat
|
|
96
99
|
params: (parse_params(@params_string) if pick.include?(:params)),
|
97
100
|
salt:
|
98
101
|
if pick.include?(:salt)
|
99
|
-
|
102
|
+
hint.dig(:salt, :encoding) == '7bit' ? @encoded_salt : B64.decode(@encoded_salt)
|
100
103
|
end,
|
101
104
|
hash: (B64.decode(@encoded_hash) if pick.include?(:hash))
|
102
105
|
}.select { |_, value| value }
|
@@ -109,6 +112,15 @@ module PhcStringFormat
|
|
109
112
|
|
110
113
|
private
|
111
114
|
|
115
|
+
def validate_params_string
|
116
|
+
!@params_string || !@params_string.empty? && @params_string.split(',').all? \
|
117
|
+
{ |param| param =~ %r{\A[a-z0-9-]{1,32}=[a-zA-Z0-9/+.-]+\z} }
|
118
|
+
end
|
119
|
+
|
120
|
+
def validate_salt_and_hash
|
121
|
+
!((!@encoded_salt || @encoded_salt.empty?) && !(!@encoded_hash || @encoded_hash.empty?))
|
122
|
+
end
|
123
|
+
|
112
124
|
def parse_version(version_string)
|
113
125
|
parse_params(version_string)['v']
|
114
126
|
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.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- naokikimura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|