phc_string_format 0.3.1 → 0.3.2
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/.reek.yml +0 -3
- data/.rubocop.yml +2 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +1 -1
- data/bin/console +2 -6
- data/lib/phc_string_format/phc_string.rb +55 -7
- 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: 95e7a26c44ec35184f4fb1ca0be50e7a6c29d99b738016ed7cd03e82f9b67694
|
4
|
+
data.tar.gz: 76c006ce54750252df08eeba8dba21c4ff5ee89ea8236eb5393e5cb4c474ba4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8932e026c805b7e9cc1a04d9b28ab9cab7c6410ae3ca12c776765c143c33a0db4cc350def716dc4fc4d5af5aff06c1dc2d286098a0058676877385314b074dd5
|
7
|
+
data.tar.gz: 39c415ecd8487e4c99182dd8f3bb008d6b952b0b5a39479fe99233895b2e4422f5efc3e0362cf272ebe1f101e9ce7f18c278b53749445414aa79cea6fe0fb976
|
data/.reek.yml
CHANGED
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/bin/console
CHANGED
@@ -6,9 +6,5 @@ require "phc_string_format"
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
9
|
+
require "pry"
|
10
|
+
Pry.start
|
@@ -3,6 +3,19 @@ module PhcStringFormat
|
|
3
3
|
# Parser for parsing PHC-string-format.
|
4
4
|
#
|
5
5
|
class PhcString
|
6
|
+
def self.validates(name, **validator)
|
7
|
+
@validators ||= {}
|
8
|
+
@validators[name] = validator
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.validate(object)
|
12
|
+
@validators.each do |name, validator|
|
13
|
+
raise ArgumentError, validator[:message] \
|
14
|
+
unless validator[:unless].call(object.instance_variable_get(name))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# :reek:DuplicateMethodCall { allow_calls: ['elements.shift', 'elements.first'] }
|
6
19
|
def self.parse(string, hint: {})
|
7
20
|
string ||= ''
|
8
21
|
elements = string.split(/\$/, 6)
|
@@ -12,10 +25,14 @@ module PhcStringFormat
|
|
12
25
|
params = elements.shift if (elements.first || '').include?('=')
|
13
26
|
salt = elements.shift
|
14
27
|
hash = elements.shift
|
15
|
-
|
28
|
+
begin
|
29
|
+
PhcString.new(id, version, params, salt, hash, hint)
|
30
|
+
rescue ArgumentError
|
31
|
+
raise ParseError
|
32
|
+
end
|
16
33
|
end
|
17
34
|
|
18
|
-
def self.create(id:, version: nil, params:
|
35
|
+
def self.create(id:, version: nil, params: nil, salt: nil, hash: nil, hint: {})
|
19
36
|
PhcString.new(
|
20
37
|
id,
|
21
38
|
("v=#{version}" if version),
|
@@ -26,18 +43,39 @@ module PhcStringFormat
|
|
26
43
|
)
|
27
44
|
end
|
28
45
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
46
|
+
validates :@id, message: 'id is non-compliant', unless: ->(id) { id && id =~ /\A[a-z0-9-]{1,32}\z/ }
|
47
|
+
validates \
|
48
|
+
:@version_string,
|
49
|
+
message: 'version is non-compliant',
|
50
|
+
unless: ->(version_string) { !version_string || version_string =~ /\Av=\d+\z/ }
|
51
|
+
validates \
|
52
|
+
:@params_string,
|
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
|
+
}
|
58
|
+
validates \
|
59
|
+
:@encoded_salt,
|
60
|
+
message: 'encoded salt is non-compliant',
|
61
|
+
unless: ->(encoded_salt) { !encoded_salt || encoded_salt =~ %r{\A[a-zA-Z0-9/+.-]+\z} }
|
62
|
+
validates \
|
63
|
+
:@encoded_hash,
|
64
|
+
message: 'encoded hash is non-compliant',
|
65
|
+
unless: ->(encoded_hash) { !encoded_hash || encoded_hash =~ %r{\A[a-zA-Z0-9/+]+\z} }
|
34
66
|
|
67
|
+
# :reek:DuplicateMethodCall { allow_calls: ['!encoded_salt', '!encoded_hash'] }
|
68
|
+
def initialize(id, version_string, params_string, encoded_salt, encoded_hash, hint)
|
35
69
|
@id = id
|
36
70
|
@version_string = version_string
|
37
71
|
@params_string = params_string
|
38
72
|
@encoded_salt = encoded_salt
|
39
73
|
@encoded_hash = encoded_hash
|
40
74
|
@hint = hint
|
75
|
+
|
76
|
+
self.class.validate self
|
77
|
+
raise ArgumentError, 'hash needs salt' \
|
78
|
+
if (!encoded_salt || encoded_salt.empty?) && !(!encoded_hash || encoded_hash.empty?)
|
41
79
|
end
|
42
80
|
|
43
81
|
def to_s
|
@@ -64,6 +102,11 @@ module PhcStringFormat
|
|
64
102
|
}.select { |_, value| value }
|
65
103
|
end
|
66
104
|
|
105
|
+
def ==(other)
|
106
|
+
instance_variable_values = other.instance_variables.map { |name| other.instance_variable_get(name) }
|
107
|
+
instance_variable_values == instance_variables.map { |name| instance_variable_get(name) }
|
108
|
+
end
|
109
|
+
|
67
110
|
private
|
68
111
|
|
69
112
|
def parse_version(version_string)
|
@@ -80,4 +123,9 @@ module PhcStringFormat
|
|
80
123
|
params_string.split(/,/).map(&mapper).each_with_object({}, &reducer)
|
81
124
|
end
|
82
125
|
end
|
126
|
+
|
127
|
+
#
|
128
|
+
# This exception is raised if a parser error occurs.
|
129
|
+
#
|
130
|
+
class ParseError < StandardError; end
|
83
131
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- naokikimura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|