params_converter 0.1.0
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 +7 -0
- data/.cimon.yml +39 -0
- data/.rspec +2 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/README.md +73 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/rspec +16 -0
- data/bin/setup +8 -0
- data/lib/params_converter.rb +46 -0
- data/lib/params_converter/version.rb +3 -0
- data/params_converter.gemspec +32 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea344296fffc48efd4fac249271e62acf4fe99b5
|
4
|
+
data.tar.gz: a1515e29ca6da4e20e37318eb5041303ce063fac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de44d9ad188fd5bac727bbeecab198124bcfe43c21b10876829d110a097694e59815bdd0add0d4c776b3915f8205b0a8c94927c807ec9dc25634f716d2bd510c
|
7
|
+
data.tar.gz: 0c5c5017a1cb92a6cd0377904fca6590bd390bf9b2c742842a622503662d28b7284dd349d35eab3da76314bbd1ce83f508e05037f4ef80b218c2f47fe8e5f747
|
data/.cimon.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
services:
|
2
|
+
ruby21:
|
3
|
+
from: ruby:2.1.8
|
4
|
+
cache:
|
5
|
+
- /bundle
|
6
|
+
build:
|
7
|
+
- gem install bundler
|
8
|
+
ruby22:
|
9
|
+
from: ruby:2.2.4
|
10
|
+
cache:
|
11
|
+
- /bundle
|
12
|
+
build:
|
13
|
+
- gem install bundler
|
14
|
+
ruby23:
|
15
|
+
from: ruby:2.3.0
|
16
|
+
cache:
|
17
|
+
- /bundle
|
18
|
+
build:
|
19
|
+
- gem install bundler
|
20
|
+
|
21
|
+
default: &default
|
22
|
+
prepare:
|
23
|
+
- bundle install --path /bundle
|
24
|
+
folder: /app
|
25
|
+
cmd: bin/rake
|
26
|
+
env:
|
27
|
+
- COVERAGE=true
|
28
|
+
|
29
|
+
test21:
|
30
|
+
<<: *default
|
31
|
+
service: ruby21
|
32
|
+
|
33
|
+
test22:
|
34
|
+
<<: *default
|
35
|
+
service: ruby22
|
36
|
+
|
37
|
+
test23:
|
38
|
+
<<: *default
|
39
|
+
service: ruby23
|
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at alexey@osipenko.in.ua. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
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
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# ParamsConverter
|
2
|
+
|
3
|
+
Advanced version of `assert_valid_keys` method. This gem allow to check keys which must be present and can be present. Also it symbolize keys. There are no dependencies.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'params_converter'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install params_converter
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
This is function without side effects and it's not modify existing hash, but returns new one. The signature of `convert!` method the following:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
ParamsConverter.convert!(hash, required_keys, allowed_keys)
|
27
|
+
```
|
28
|
+
|
29
|
+
There are a few examples:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
ParamsConverter.convert!({ a: 1, b: 2, 'c' => 3, d: 4 }, [:a, 'b'], ['c', :d])
|
33
|
+
# { a: 1, b: 2, c: 3, d: 4 }
|
34
|
+
|
35
|
+
ParamsConverter.convert!({ a: 1, b: 2 }, [:c], [:b])
|
36
|
+
# raises ParamsConverter::MissingRequiredError
|
37
|
+
|
38
|
+
ParamsConverter.convert!({ a: 1, b: 2 }, [:a], [:d])
|
39
|
+
# raises ParamsConverter::MissingRequiredError
|
40
|
+
```
|
41
|
+
|
42
|
+
`nil` instead of second or third parameter is valid value. It means that it allows any required/allowed keys:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
ParamsConverter.convert!({ a: 1, 'b' => 2 }, nil, nil)
|
46
|
+
# { a: 1, b: 2 }
|
47
|
+
```
|
48
|
+
|
49
|
+
Empty array instead of required keys means that any key is invalid. Empty array instead of allowed keys means that there are no keys should be present except required keys:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
ParamsConverter.convert!({ a: 1, 'b' => 2, c: 3 }, [:a, :b, :c], [])
|
53
|
+
# { a: 1, b: 2, c: 3 }
|
54
|
+
|
55
|
+
ParamsConverter.convert!({ a: 1, 'b' => 2, c: 3, d: 4 }, [:a, :b, :c], [])
|
56
|
+
# raises ParamsConverter::NotAllowedError
|
57
|
+
|
58
|
+
ParamsConverter.convert!({ a: 1, 'b' => 2, c: 3, d: 4 }, [], nil)
|
59
|
+
# raises ParamsConverter::NotAllowedError
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
It should support all versions of ruby, but only ruby 2.1+ is guaranteed.
|
64
|
+
|
65
|
+
## Development
|
66
|
+
|
67
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
68
|
+
|
69
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
70
|
+
|
71
|
+
## Contributing
|
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.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "params_converter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
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
|
data/bin/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require "pathname"
|
10
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require "rubygems"
|
14
|
+
require "bundler/setup"
|
15
|
+
|
16
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/setup
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "params_converter/version"
|
2
|
+
|
3
|
+
module ParamsConverter
|
4
|
+
|
5
|
+
class ParamsConverterError < ::StandardError
|
6
|
+
def initialize(msg = nil)
|
7
|
+
msg = msg.is_a?(Array) ? msg.join(',') : msg.to_s
|
8
|
+
super("#{message_prefix} #{msg}")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class NotAllowedError < ParamsConverterError
|
13
|
+
def message_prefix
|
14
|
+
"Not allowed keys"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MissingRequiredError < ParamsConverterError
|
19
|
+
def message_prefix
|
20
|
+
"Missing keys"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
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)
|
28
|
+
|
29
|
+
params = Hash[params.map { |k,v| [k.to_s.to_sym, v] }]
|
30
|
+
|
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
|
35
|
+
end
|
36
|
+
|
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
|
41
|
+
|
42
|
+
params
|
43
|
+
end
|
44
|
+
module_function :convert!
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'params_converter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "params_converter"
|
8
|
+
spec.version = ParamsConverter::VERSION
|
9
|
+
spec.authors = ["Alexey Osipenko"]
|
10
|
+
spec.email = ["alexey@osipenko.in.ua"]
|
11
|
+
|
12
|
+
spec.summary = %q{Rich version of assert_valid_keys method}
|
13
|
+
spec.description = %q{Rich version of assert_valid_keys method}
|
14
|
+
spec.homepage = "https://github.com/cimon-io/params_converter"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: params_converter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Osipenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Rich version of assert_valid_keys method
|
56
|
+
email:
|
57
|
+
- alexey@osipenko.in.ua
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".cimon.yml"
|
63
|
+
- ".rspec"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/rspec
|
71
|
+
- bin/setup
|
72
|
+
- lib/params_converter.rb
|
73
|
+
- lib/params_converter/version.rb
|
74
|
+
- params_converter.gemspec
|
75
|
+
homepage: https://github.com/cimon-io/params_converter
|
76
|
+
licenses: []
|
77
|
+
metadata:
|
78
|
+
allowed_push_host: https://rubygems.org
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.5.1
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Rich version of assert_valid_keys method
|
99
|
+
test_files: []
|
100
|
+
has_rdoc:
|