phc_string_format 0.3.2 → 0.3.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95e7a26c44ec35184f4fb1ca0be50e7a6c29d99b738016ed7cd03e82f9b67694
4
- data.tar.gz: 76c006ce54750252df08eeba8dba21c4ff5ee89ea8236eb5393e5cb4c474ba4a
3
+ metadata.gz: 33dff8312aa061f66c90fdabb7d4630ffdb322c2d770e1714fd7f8115335a72e
4
+ data.tar.gz: 87d4da1c7bd827cad52cf8f748184d11d661f7d1757627b87be932aaeaf16728
5
5
  SHA512:
6
- metadata.gz: 8932e026c805b7e9cc1a04d9b28ab9cab7c6410ae3ca12c776765c143c33a0db4cc350def716dc4fc4d5af5aff06c1dc2d286098a0058676877385314b074dd5
7
- data.tar.gz: 39c415ecd8487e4c99182dd8f3bb008d6b952b0b5a39479fe99233895b2e4422f5efc3e0362cf272ebe1f101e9ce7f18c278b53749445414aa79cea6fe0fb976
6
+ metadata.gz: bb3038a561c2b36c94d064fa101dec7aa6dd7c49fd4df7b3da269e3d3595ccd8a54b79c1eebd905a42ea1abec0bcfb2fabe4f6cb54cc728d7a172485ced83105
7
+ data.tar.gz: a4c5bc3223bf5a614b485fe3a55afaf04f039d0123a734bef209d7db42ba85e076b7dfaed2685076433a87189762f43d314b5607c304706b3b41283d1ae5b527
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phc_string_format (0.3.2)
4
+ phc_string_format (0.3.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -8,7 +8,7 @@ module PhcStringFormat
8
8
  end
9
9
 
10
10
  def self.parse(string, hint: {}, pick: nil)
11
- PhcString.parse(string, hint: hint).to_h(pick)
11
+ PhcString.parse(string).to_h(pick: pick, hint: hint)
12
12
  end
13
13
  end
14
14
  end
@@ -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, **validator)
7
- @validators ||= {}
8
- @validators[name] = validator
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(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
+ 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, hint: {})
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, hint)
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', unless: ->(id) { id && id =~ /\A[a-z0-9-]{1,32}\z/ }
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
- 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
- }
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
- unless: ->(encoded_salt) { !encoded_salt || encoded_salt =~ %r{\A[a-zA-Z0-9/+.-]+\z} }
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
- unless: ->(encoded_hash) { !encoded_hash || encoded_hash =~ %r{\A[a-zA-Z0-9/+]+\z} }
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
- # :reek:DuplicateMethodCall { allow_calls: ['!encoded_salt', '!encoded_hash'] }
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.validate self
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 = nil)
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
- @hint.dig(:salt, :encoding) == '7bit' ? e : B64.decode(@encoded_salt)
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
@@ -1,3 +1,3 @@
1
1
  module PhcStringFormat
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
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.3.2
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-01 00:00:00.000000000 Z
11
+ date: 2018-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler