phc_string_format 0.3.1 → 0.3.2

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
  SHA256:
3
- metadata.gz: f2c0d23fc35b585de7e270b87439a5ae61c53213f10442f8b5e576484e985d2d
4
- data.tar.gz: 5ccf72ce6afe6ac8e16a8fb6904341007e7698daf2ddd85b25296d5179c7cca2
3
+ metadata.gz: 95e7a26c44ec35184f4fb1ca0be50e7a6c29d99b738016ed7cd03e82f9b67694
4
+ data.tar.gz: 76c006ce54750252df08eeba8dba21c4ff5ee89ea8236eb5393e5cb4c474ba4a
5
5
  SHA512:
6
- metadata.gz: 59d704eeda40482c8b55c1ee0e398c3da3b09c70fcff60e05106411c8bf2e43246c57d9b6bbaebf2af5f1ae855105ce915847bee79a09bb67f2b3436d5b21bb6
7
- data.tar.gz: 5518af92641a14e47ba3bc552bf9e3da25ece970b479ac2e5708fe868eebbb6571b4725b22e8b0374eeb0f0215d550e11054ccb4e7a1883bc8d286fa9df51677
6
+ metadata.gz: 8932e026c805b7e9cc1a04d9b28ab9cab7c6410ae3ca12c776765c143c33a0db4cc350def716dc4fc4d5af5aff06c1dc2d286098a0058676877385314b074dd5
7
+ data.tar.gz: 39c415ecd8487e4c99182dd8f3bb008d6b952b0b5a39479fe99233895b2e4422f5efc3e0362cf272ebe1f101e9ce7f18c278b53749445414aa79cea6fe0fb976
data/.reek.yml CHANGED
@@ -1,9 +1,6 @@
1
1
  ---
2
2
  detectors:
3
3
 
4
- DuplicateMethodCall:
5
- exclude:
6
- - 'PhcStringFormat::PhcString#self.parse'
7
4
  FeatureEnvy:
8
5
  exclude:
9
6
  - 'PhcStringFormat::PhcString#parse_params'
data/.rubocop.yml CHANGED
@@ -15,6 +15,8 @@ Metrics/AbcSize:
15
15
  Metrics/BlockLength:
16
16
  Exclude:
17
17
  - 'spec/**/*'
18
+ Metrics/ClassLength:
19
+ Max: 120
18
20
 
19
21
  Metrics/CyclomaticComplexity:
20
22
  Max: 10
data/Gemfile CHANGED
@@ -6,5 +6,4 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
6
  gemspec
7
7
 
8
8
  gem "rubocop", "~> 0.58.2", require: false, :groups => [:development, :test]
9
-
10
9
  gem "reek", "~> 5.0", require: false, :groups => [:development, :test]
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phc_string_format (0.3.1)
4
+ phc_string_format (0.3.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
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
- PhcString.new(id, version, params, salt, hash, hint)
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: {}, salt: '', hash: '', hint: {})
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
- def initialize(id, version_string, params_string, encoded_salt, encoded_hash, hint)
30
- raise ArgumentError.new, 'id is required' unless id
31
- if (!encoded_salt || encoded_salt.empty?) && !(!encoded_hash || encoded_hash.empty?)
32
- raise ArgumentError.new, 'hash needs salt'
33
- end
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
@@ -1,3 +1,3 @@
1
1
  module PhcStringFormat
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
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.1
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-07-29 00:00:00.000000000 Z
11
+ date: 2018-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler