iknow_params 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 72ed5066cf3acd50bc02597d9731adb68cc5f13c1c672a74ed0e73d38e30721a
4
+ data.tar.gz: d818d2b61fa52eaafe28522c50cf4d2ff39c1c97048a58e8f3d9ae1c4e4d5212
5
+ SHA512:
6
+ metadata.gz: 0540c5cdb77e348a212628f197003885ece7296ca4a2bcab9d98c29c8d7719ed893178fa6ff7126dba2150881419c7c5282579856f9b7cd19ab64b0baff46d80
7
+ data.tar.gz: 4ff81fb705909a6a793b5ac1f436ff4a4cfc97acb00aab9767725e73c7cec1bbf0f91fe1be31d462fe4cd9124617cee9b10f135f007310cfc062bb65ef94aed3
@@ -0,0 +1,103 @@
1
+ version: 2.1
2
+
3
+ executors:
4
+ ruby:
5
+ parameters:
6
+ ruby-version:
7
+ type: string
8
+ default: "2.6"
9
+ gemfile:
10
+ type: string
11
+ default: "Gemfile"
12
+ docker:
13
+ - image: circleci/ruby:<< parameters.ruby-version >>
14
+ environment:
15
+ BUNDLE_JOBS: 3
16
+ BUNDLE_RETRY: 3
17
+ BUNDLE_PATH: vendor/bundle
18
+ RAILS_ENV: test
19
+ BUNDLE_GEMFILE: << parameters.gemfile >>
20
+
21
+ jobs:
22
+ test:
23
+ parameters:
24
+ ruby-version:
25
+ type: string
26
+ gemfile:
27
+ type: string
28
+ executor:
29
+ name: ruby
30
+ ruby-version: << parameters.ruby-version >>
31
+ gemfile: << parameters.gemfile >>
32
+ parallelism: 1
33
+ steps:
34
+ - checkout
35
+
36
+ - run:
37
+ # Remove the non-appraisal gemfile for safety: we never want to use it.
38
+ name: Prepare bundler
39
+ command: bundle -v
40
+
41
+ - run:
42
+ name: Compute a gemfile lock
43
+ command: bundle lock && cp "${BUNDLE_GEMFILE}.lock" /tmp/gem-lock
44
+
45
+ - restore_cache:
46
+ keys:
47
+ - iknow_params-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
48
+ - iknow_params-
49
+
50
+ - run:
51
+ name: Bundle Install
52
+ command: bundle check || bundle install
53
+
54
+ - save_cache:
55
+ key: iknow_params-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
56
+ paths:
57
+ - vendor/bundle
58
+
59
+ - run:
60
+ name: Run rspec
61
+ command: bundle exec rspec --profile 10 --format RspecJunitFormatter --out test_results/rspec.xml --format progress
62
+
63
+ - store_test_results:
64
+ path: test_results
65
+
66
+ publish:
67
+ executor: ruby
68
+ steps:
69
+ - checkout
70
+ - run:
71
+ name: Setup Rubygems
72
+ command: |
73
+ mkdir ~/.gem &&
74
+ echo -e "---\r\n:rubygems_api_key: $RUBYGEMS_API_KEY" > ~/.gem/credentials &&
75
+ chmod 0600 ~/.gem/credentials
76
+ - run:
77
+ name: Publish to Rubygems
78
+ command: |
79
+ gem build iknow_params.gemspec
80
+ gem push iknow_params-*.gem
81
+
82
+ workflows:
83
+ version: 2.1
84
+ build:
85
+ jobs:
86
+ - test:
87
+ name: 'ruby 2.5 ActiveSupport 5.2'
88
+ ruby-version: "2.5"
89
+ gemfile: gemfiles/activesupport_5_2.gemfile
90
+ - test:
91
+ name: 'ruby 2.6 ActiveSupport 5.2'
92
+ ruby-version: "2.6"
93
+ gemfile: gemfiles/activesupport_5_2.gemfile
94
+ - test:
95
+ name: 'ruby 2.6 ActiveSupport 6.0-beta'
96
+ ruby-version: "2.6"
97
+ gemfile: gemfiles/activesupport_6_0_beta.gemfile
98
+ - publish:
99
+ filters:
100
+ branches:
101
+ only: master
102
+ tags:
103
+ ignore: /.*/
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .byebug_history
16
+ gemfiles/*.gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ dist: trusty
2
+ language: ruby
3
+ rvm:
4
+ - 2.4
5
+ gemfile:
6
+ - gemfiles/activesupport_5_2.gemfile
7
+ notifications:
8
+ email: false
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "activesupport-5-2" do
2
+ gem "activesupport", "~> 5.2.0"
3
+ end
4
+
5
+ appraise 'activesupport-6-0-beta' do
6
+ gem 'activesupport', '~> 6.0.0.beta'
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in iknow_params.gemspec
4
+ gemspec
5
+ gem 'rspec_junit_formatter'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 DMM.com
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # IknowParams
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'iknow_params'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install iknow_params
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/iknow_params/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+ rescue LoadError
7
+ puts "rspec unavailable"
8
+ end
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rspec_junit_formatter"
6
+ gem "activesupport", "~> 5.2.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rspec_junit_formatter"
6
+ gem "activesupport", "~> 6.0.0.beta"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iknow_params/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "iknow_params"
8
+ spec.version = IknowParams::VERSION
9
+ spec.authors = ["dev@iknow.jp"]
10
+ spec.email = [""]
11
+ spec.summary = %q{Rails parameter parser for iKnow.}
12
+ spec.description = %q{}
13
+ spec.homepage = "http://github.com/iknow/iknow_params"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "actionpack", "> 5.0"
26
+ spec.add_development_dependency "rails", "> 5.0"
27
+
28
+ spec.add_development_dependency "byebug"
29
+ spec.add_development_dependency "appraisal"
30
+
31
+ spec.add_dependency "activesupport", "> 5.0"
32
+ spec.add_dependency "tzinfo"
33
+ spec.add_dependency "json-schema", "~> 2.8.0"
34
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IknowParams
4
+ require "iknow_params/version"
5
+ require "iknow_params/parser"
6
+ require "iknow_params/serializer"
7
+ end
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+ require "active_support/inflector"
5
+ require "active_support/core_ext/object/blank"
6
+
7
+ # IknowParams::Parser provides a mix-in for ActiveRecord controllers to parse input parameters.
8
+ module IknowParams::Parser
9
+ require "iknow_params/parser/hash_parser"
10
+
11
+ extend ActiveSupport::Concern
12
+
13
+ class ParseError < RuntimeError
14
+ attr_accessor :param, :value
15
+
16
+ def initialize(message, param, value)
17
+ super(message)
18
+ self.param = param
19
+ self.value = value
20
+ end
21
+ end
22
+
23
+ PARAM_REQUIRED = Object.new
24
+ BLANK = Object.new
25
+
26
+ class << self
27
+ def parse_hash(hash, &block)
28
+ HashParser.new(hash).parse(&block)
29
+ end
30
+
31
+ def parse_value(value, **args)
32
+ HashParser.new({ sentinel: value }).parse_param(:sentinel, **args)
33
+ end
34
+
35
+ def parse_values(values, **args)
36
+ HashParser.new({ sentinel: values }).parse_array_param(:sentinel, **args)
37
+ end
38
+ end
39
+
40
+ # Parse the specified parameter, optionally deserializing with the provided
41
+ # IKnowParams::Serializer. If the parameter is missing and no default is
42
+ # provided, raises a ParseError.
43
+ #
44
+ # If `BLANK` is provided as a default, return a placeholder object that can be
45
+ # later stripped out with `remove_blanks`
46
+ #
47
+ # If `dump` is true, use the serializer to re-serialize any successfully
48
+ # parsed argument back to a canonical string. This can be useful to validate
49
+ # and normalize the input to another service without parsing it. A serializer
50
+ # must be passed to use this option.
51
+ def parse_param(param, with: nil, default: PARAM_REQUIRED, dump: false)
52
+ serializer =
53
+ case with
54
+ when String, Symbol
55
+ IknowParams::Serializer.for!(with)
56
+ else
57
+ with
58
+ end
59
+
60
+ parse =
61
+ if !params.has_key?(param)
62
+ raise ParseError.new("Required parameter '#{param}' missing", param, nil) if default == PARAM_REQUIRED
63
+ default
64
+ else
65
+ val = params[param]
66
+ if serializer.present?
67
+ begin
68
+ serializer.load(val)
69
+ rescue IknowParams::Serializer::LoadError => ex
70
+ raise ParseError.new("Invalid parameter '#{param}': '#{val.inspect}' - #{ex.message}", param, val)
71
+ end
72
+ else
73
+ val
74
+ end
75
+ end
76
+
77
+ if dump && parse != BLANK
78
+ begin
79
+ parse = serializer.dump(parse)
80
+ rescue NoMethodError => ex
81
+ raise ParseError.new("Serializer '#{serializer}' can't dump param '#{param}' #{val.inspect} - #{ex.message}", param, val)
82
+ end
83
+ end
84
+
85
+ parse
86
+ end
87
+
88
+ # Parse an array-typed param using the provided serializer for each member element.
89
+ def parse_array_param(param, with: nil, default: PARAM_REQUIRED, dump: false)
90
+ serializer =
91
+ case with
92
+ when String, Symbol
93
+ IknowParams::Serializer.for!(with)
94
+ else
95
+ with
96
+ end
97
+
98
+ vals = params[param]
99
+
100
+ parses =
101
+ if vals.nil?
102
+ raise ParseError.new("Required parameter '#{param}' missing", param, nil) if default == PARAM_REQUIRED
103
+ default
104
+ elsif !vals.is_a?(Array)
105
+ raise ParseError.new("Invalid type for parameter '#{param}': '#{vals.class.name}'", param, vals)
106
+ elsif serializer.present?
107
+ vals.map do |val|
108
+ begin
109
+ serializer.load(val)
110
+ rescue IknowParams::Serializer::LoadError => ex
111
+ raise ParseError.new("Invalid member in array parameter '#{param}': '#{val.inspect}' - #{ex.message}", param, val)
112
+ end
113
+ end
114
+ else
115
+ vals
116
+ end
117
+
118
+ if dump && parses != BLANK
119
+ parses.map! { |v| serializer.dump(v) }
120
+ end
121
+
122
+ parses
123
+ end
124
+
125
+ # Convenience method to make it simpler to build a hash structure with
126
+ # optional members from parsed data. This method recursively traverses the
127
+ # provided structure and removes any instances of the sentinel value
128
+ # Parser::BLANK.
129
+ def remove_blanks(arg)
130
+ case arg
131
+ when Hash
132
+ arg.each do |k, v|
133
+ if v == BLANK
134
+ arg.delete(k)
135
+ else
136
+ remove_blanks(v)
137
+ end
138
+ end
139
+ when Array
140
+ arg.delete(BLANK)
141
+ arg.each { |e| remove_blanks(e) }
142
+ end
143
+ end
144
+
145
+ # Allow serializers to register themselves
146
+ def self.register_serializer(name, serializer)
147
+ define_method(:"parse_#{name.underscore}_param") do |param, default: PARAM_REQUIRED|
148
+ parse_param(param, with: serializer, default: default)
149
+ end
150
+ define_method(:"parse_#{name.underscore}_array_param") do |param, default: PARAM_REQUIRED|
151
+ parse_array_param(param, with: serializer, default: default)
152
+ end
153
+ end
154
+ end