params_converter 0.1.0 → 0.1.1

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
- SHA1:
3
- metadata.gz: ea344296fffc48efd4fac249271e62acf4fe99b5
4
- data.tar.gz: a1515e29ca6da4e20e37318eb5041303ce063fac
2
+ SHA256:
3
+ metadata.gz: c8096a5ba7d71ad4d02e16a0d388b50daf1a10ea5ca2d5ce54b79c0133daaf74
4
+ data.tar.gz: 88bb33b63edb55ddb4c4982fa78e370feb7cc8fb41891e13e74bc105b7b54df0
5
5
  SHA512:
6
- metadata.gz: de44d9ad188fd5bac727bbeecab198124bcfe43c21b10876829d110a097694e59815bdd0add0d4c776b3915f8205b0a8c94927c807ec9dc25634f716d2bd510c
7
- data.tar.gz: 0c5c5017a1cb92a6cd0377904fca6590bd390bf9b2c742842a622503662d28b7284dd349d35eab3da76314bbd1ce83f508e05037f4ef80b218c2f47fe8e5f747
6
+ metadata.gz: f9391a2d6da9c6841b3a5d9a1df398dc20774f664ff6831c2d8dde83b6a46db740ae19712d654108624a61d666c39ac0c33898f72f809adc54e9e672a53c6b6b
7
+ data.tar.gz: a4786bf71c0f04094b2bec5f309f131e2b99aa86a265648e8dadcc2bdf60d5d85b5ec6329328675585ce5b536955ce2d6a015b47a8abd385ae150ecaabd4881d
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alexey Osipenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -70,4 +70,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
70
70
 
71
71
  ## Contributing
72
72
 
73
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/params_converter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cimon-io/params_converter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+
79
+ ## Code of Conduct
80
+
81
+ Everyone interacting in the EthWalletFormatValidator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cimon-io/params_converter/blob/master/CODE_OF_CONDUCT.md).
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ParamsConverter
2
- VERSION = "0.1.0"
4
+ VERSION = '0.1.1'
3
5
  end
@@ -1,46 +1,112 @@
1
- require "params_converter/version"
1
+ # frozen_string_literal: true
2
2
 
3
- module ParamsConverter
3
+ require 'params_converter/version'
4
4
 
5
- class ParamsConverterError < ::StandardError
5
+ module ParamsConverter # :nodoc:
6
+ class ParamsConverterError < ::StandardError # :nodoc:
6
7
  def initialize(msg = nil)
7
8
  msg = msg.is_a?(Array) ? msg.join(',') : msg.to_s
8
9
  super("#{message_prefix} #{msg}")
9
10
  end
10
11
  end
11
12
 
12
- class NotAllowedError < ParamsConverterError
13
+ class NotAllowedError < ParamsConverterError # :nodoc:
13
14
  def message_prefix
14
- "Not allowed keys"
15
+ 'Not allowed keys'
15
16
  end
16
17
  end
17
18
 
18
- class MissingRequiredError < ParamsConverterError
19
+ class MissingRequiredError < ParamsConverterError # :nodoc:
19
20
  def message_prefix
20
- "Missing keys"
21
+ 'Missing keys'
21
22
  end
22
23
  end
23
24
 
24
- def convert!(params, re=nil, pe=nil)
25
- raise ArgumentError unless re.nil? or re.is_a?(Array)
26
- raise ArgumentError unless pe.nil? or pe.is_a?(Array)
27
- raise ArgumentError unless params.is_a?(Hash)
25
+ class Adjustor # :nodoc:
26
+ attr_reader :params,
27
+ :required_keys,
28
+ :allowed_keys
28
29
 
29
- params = Hash[params.map { |k,v| [k.to_s.to_sym, v] }]
30
+ def initialize(params, required_keys, allowed_keys)
31
+ @params = check_and_symbolize_keys!(params, Hash)
32
+ @required_keys = check_and_symbolize!(required_keys, nil, Array)
33
+ @allowed_keys = check_and_symbolize!(allowed_keys, nil, Array)
34
+ end
35
+
36
+ def perform
37
+ required_keys_filter unless required_keys.nil?
38
+ allowed_keys_filter unless allowed_keys.nil?
39
+ params
40
+ end
41
+
42
+ private
43
+
44
+ def required_keys_filter
45
+ raise_if_any(MissingRequiredError, missing_keys)
46
+ end
47
+
48
+ def allowed_keys_filter
49
+ raise_if_any(NotAllowedError, not_allowed_keys)
50
+ end
30
51
 
31
- unless re.nil?
32
- re = re.map(&:to_s).map(&:to_sym).uniq
33
- missing_keys = re.reject { |r| !!params.fetch(r, false) }
34
- raise MissingRequiredError.new(missing_keys) if missing_keys.count > 0
52
+ def missing_keys
53
+ @missing_keys ||= required_keys - param_keys
35
54
  end
36
55
 
37
- pe = params.keys if pe.nil?
38
- pe = ((re || []) | pe).map(&:to_s).map(&:to_sym).uniq
39
- not_allowed_keys = params.keys - pe
40
- raise NotAllowedError.new(not_allowed_keys) if not_allowed_keys.count > 0
56
+ def not_allowed_keys
57
+ @not_allowed_keys ||= param_keys - [*allowed_keys, *required_keys]
58
+ end
59
+
60
+ def param_keys
61
+ @param_keys ||= params.keys
62
+ end
63
+
64
+ def raise_if_any(error, keys)
65
+ raise(error, keys) if keys.any?
66
+ end
67
+
68
+ def check_and_symbolize_keys!(value, *allowed_values)
69
+ value = prepare(value)
70
+ check_params!(value, allowed_values)
71
+ hash_with_symbolized_keys(value)
72
+ end
41
73
 
42
- params
74
+ def prepare(value)
75
+ value = value.to_unsafe_h if value.respond_to?(:to_unsafe_h)
76
+ value = value.to_h if value.respond_to?(:to_h)
77
+ value
78
+ end
79
+
80
+ def check_params!(value, allowed_values)
81
+ case value
82
+ when *allowed_values
83
+ value
84
+ else
85
+ raise ArgumentError
86
+ end
87
+ end
88
+
89
+ def check_and_symbolize!(value, *allowed_values)
90
+ check_params!(value, allowed_values)
91
+ return if value.nil?
92
+ symbolized_uniq_array(value)
93
+ end
94
+
95
+ def symbolized_uniq_array(value)
96
+ value.map { |v| symbolize(v) }.uniq
97
+ end
98
+
99
+ def symbolize(value)
100
+ value.to_s.to_sym
101
+ end
102
+
103
+ def hash_with_symbolized_keys(value)
104
+ Hash[value.map { |k, v| [symbolize(k), v] }]
105
+ end
43
106
  end
44
- module_function :convert!
45
107
 
108
+ def convert!(params, required_keys = nil, allowed_keys = nil)
109
+ Adjustor.new(params, required_keys, allowed_keys).perform
110
+ end
111
+ module_function :convert!
46
112
  end
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = %q{Rich version of assert_valid_keys method}
13
13
  spec.description = %q{Rich version of assert_valid_keys method}
14
14
  spec.homepage = "https://github.com/cimon-io/params_converter"
15
+ spec.license = 'MIT'
15
16
 
16
17
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
18
  # delete this section to allow pushing this gem to any host.
@@ -26,7 +27,7 @@ Gem::Specification.new do |spec|
26
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
28
  spec.require_paths = ["lib"]
28
29
 
29
- spec.add_development_dependency "bundler", "~> 1.11"
30
- spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "bundler", ">= 2.2.10"
31
+ spec.add_development_dependency "rake", ">= 12.3.3"
31
32
  spec.add_development_dependency "rspec"
32
33
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Osipenko
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-11 00:00:00.000000000 Z
11
+ date: 2021-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.11'
19
+ version: 2.2.10
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.11'
26
+ version: 2.2.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -63,7 +63,7 @@ files:
63
63
  - ".rspec"
64
64
  - CODE_OF_CONDUCT.md
65
65
  - Gemfile
66
- - Gemfile.lock
66
+ - LICENSE.txt
67
67
  - README.md
68
68
  - Rakefile
69
69
  - bin/console
@@ -73,10 +73,11 @@ files:
73
73
  - lib/params_converter/version.rb
74
74
  - params_converter.gemspec
75
75
  homepage: https://github.com/cimon-io/params_converter
76
- licenses: []
76
+ licenses:
77
+ - MIT
77
78
  metadata:
78
79
  allowed_push_host: https://rubygems.org
79
- post_install_message:
80
+ post_install_message:
80
81
  rdoc_options: []
81
82
  require_paths:
82
83
  - lib
@@ -91,10 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
- rubyforge_project:
95
- rubygems_version: 2.5.1
96
- signing_key:
95
+ rubygems_version: 3.1.4
96
+ signing_key:
97
97
  specification_version: 4
98
98
  summary: Rich version of assert_valid_keys method
99
99
  test_files: []
100
- has_rdoc:
data/Gemfile.lock DELETED
@@ -1,35 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- params_converter (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- diff-lcs (1.2.5)
10
- rake (10.5.0)
11
- rspec (3.4.0)
12
- rspec-core (~> 3.4.0)
13
- rspec-expectations (~> 3.4.0)
14
- rspec-mocks (~> 3.4.0)
15
- rspec-core (3.4.4)
16
- rspec-support (~> 3.4.0)
17
- rspec-expectations (3.4.0)
18
- diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.4.0)
20
- rspec-mocks (3.4.1)
21
- diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.4.0)
23
- rspec-support (3.4.1)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- bundler (~> 1.11)
30
- params_converter!
31
- rake (~> 10.0)
32
- rspec
33
-
34
- BUNDLED WITH
35
- 1.11.2