ostruct-sanitizer 0.3.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +22 -0
- data/.github/workflows/release.yml +20 -0
- data/.gitignore +1 -0
- data/.travis.yml +4 -1
- data/README.md +1 -1
- data/lib/ostruct/sanitizer.rb +46 -20
- data/lib/ostruct/sanitizer/version.rb +1 -1
- data/ostruct-sanitizer.gemspec +4 -1
- metadata +22 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 21d9b00bfcb23a7e0c361f8299a563506598de0ee343200b20b30c05a4a70c32
|
4
|
+
data.tar.gz: 1a154510321649d9f97e77c64919c095fe18b8d512b35e82e57321e6e714c7c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b4fe46013b220f8242de2277a25fff91334246a38116f2723e96dc3f686139a2758167637a7408f7fd5a88ef8d5662b1cabc0e2aaee42c2d08edfb720845e79
|
7
|
+
data.tar.gz: 1e498bfb88218ee6400517b79cff65ae162ad71309e328e7238e224b2f941d36d98f3fd0158f9a7a581a42e8d1f3edd3df33e33622f686a74992a3b4a4444d7f
|
@@ -0,0 +1,22 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: push
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
name: Tests
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
strategy:
|
10
|
+
fail-fast: false
|
11
|
+
matrix:
|
12
|
+
ruby:
|
13
|
+
- "2.7"
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- uses: ruby/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
19
|
+
bundler: "2"
|
20
|
+
bundler-cache: true
|
21
|
+
- name: Run tests
|
22
|
+
run: bundle exec rake spec
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: Publish Gem
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- main
|
7
|
+
tags:
|
8
|
+
- v*
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
steps:
|
13
|
+
- uses: actions/checkout@v1
|
14
|
+
- name: Release Gem
|
15
|
+
uses: cadwallion/publish-rubygems-action@master
|
16
|
+
if: contains(github.ref, 'refs/tags/v')
|
17
|
+
env:
|
18
|
+
GITHUB_TOKEN: ${{ secrets.github_token }}
|
19
|
+
RUBYGEMS_API_KEY: ${{ secrets.rubygems_api_key }}
|
20
|
+
RELEASE_COMMAND: rake release
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
data/lib/ostruct/sanitizer.rb
CHANGED
@@ -7,10 +7,10 @@ module OStruct
|
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# class Person < OpenStruct
|
10
|
-
# include
|
10
|
+
# include OStruct::Sanitizer
|
11
11
|
#
|
12
12
|
# truncate :name, length: 20
|
13
|
-
#
|
13
|
+
# alphanumeric :name
|
14
14
|
# sanitize :middle_name do |value|
|
15
15
|
# # Perform a more complex sanitization process
|
16
16
|
# end
|
@@ -28,32 +28,57 @@ module OStruct
|
|
28
28
|
# Initializes the OpenStruct applying any registered sanitization rules
|
29
29
|
#
|
30
30
|
def initialize(attrs = {})
|
31
|
-
super
|
31
|
+
super nil
|
32
32
|
attrs.each_pair do |field, value|
|
33
|
-
self
|
33
|
+
self[field] = value
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
#
|
37
|
+
# Creates a setter method for the corresponding field which applies any
|
38
|
+
# existing sanitization rules
|
38
39
|
#
|
39
|
-
# @param [
|
40
|
-
# @
|
40
|
+
# @param [Symbol] method the missing method
|
41
|
+
# @param [Array<Any>] args the method's arguments list
|
41
42
|
#
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
43
|
+
def method_missing(method, *args)
|
44
|
+
# Give OpenStruct a chance to create getters and setters for the
|
45
|
+
# corresponding field
|
46
|
+
super method, *args
|
47
|
+
|
48
|
+
if field = setter?(method)
|
49
|
+
# override setter logic to apply any existing sanitization rules before
|
50
|
+
# assigning the new value to the field
|
51
|
+
override_setter_for(field) if sanitize?(field)
|
52
|
+
# uses the newly created setter to set the field's value and apply any
|
53
|
+
# existing sanitization rules
|
54
|
+
send(method, args[0])
|
49
55
|
end
|
50
|
-
|
56
|
+
end
|
57
|
+
|
58
|
+
# Set attribute's value via setter so that any existing sanitization rules
|
59
|
+
# may be applied
|
60
|
+
#
|
61
|
+
# @param [Symbol|String] name the attribute's name
|
62
|
+
# @param [Any] value the attribute's value
|
63
|
+
#
|
64
|
+
def []=(name, value)
|
65
|
+
send("#{name}=", value)
|
51
66
|
end
|
52
67
|
|
53
68
|
private
|
54
69
|
|
70
|
+
def setter?(method)
|
71
|
+
method[/.*(?==\z)/m].to_s.to_sym
|
72
|
+
end
|
73
|
+
|
74
|
+
def override_setter_for(field)
|
75
|
+
define_singleton_method("#{field}=") do |value|
|
76
|
+
modifiable?[field] = sanitize(field, value)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
55
80
|
def sanitize(field, value)
|
56
|
-
return value if value.nil?
|
81
|
+
return value if value.nil?
|
57
82
|
self.class.sanitizers[field].reduce(value) do |current_value, sanitizer|
|
58
83
|
sanitizer.call(current_value)
|
59
84
|
end
|
@@ -77,7 +102,7 @@ module OStruct
|
|
77
102
|
def sanitize(*fields, &block)
|
78
103
|
@sanitizers ||= {}
|
79
104
|
fields.each do |field|
|
80
|
-
field_sanitizers = @sanitizers[field] ||= []
|
105
|
+
field_sanitizers = @sanitizers[field.to_sym] ||= []
|
81
106
|
field_sanitizers << block
|
82
107
|
end
|
83
108
|
end
|
@@ -89,16 +114,17 @@ module OStruct
|
|
89
114
|
# @param [Boolean] strip_whitespaces whether or not to strip whitespaces
|
90
115
|
#
|
91
116
|
def truncate(*fields, length:, strip_whitespaces: true)
|
117
|
+
strip(*fields) if strip_whitespaces
|
92
118
|
sanitize(*fields) { |value| value[0...length] }
|
93
119
|
strip(*fields) if strip_whitespaces
|
94
120
|
end
|
95
121
|
|
96
|
-
#
|
122
|
+
# Remove any non-alphanumeric character from the field's value
|
97
123
|
#
|
98
124
|
# @param [Array<Symbol>] a list of field names to be sanitized
|
99
125
|
#
|
100
|
-
def
|
101
|
-
sanitize(*fields) { |value| value.gsub(/[
|
126
|
+
def alphanumeric(*fields)
|
127
|
+
sanitize(*fields) { |value| value.gsub(/[^A-Za-z0-9\s]/, '') }
|
102
128
|
end
|
103
129
|
|
104
130
|
# Strips out leading and trailing spaces from the values of the given fields
|
data/ostruct-sanitizer.gemspec
CHANGED
@@ -21,7 +21,10 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
-
spec.
|
24
|
+
spec.required_ruby_version = '>= 2.7'
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.2.11"
|
25
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
28
|
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "byebug"
|
27
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ostruct-sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego Borges
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.2.11
|
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:
|
26
|
+
version: 2.2.11
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Provides Rails-like sanitization rules for OpenStruct fields.
|
56
70
|
email:
|
57
71
|
- drborges.cic@gmail.com
|
@@ -59,6 +73,8 @@ executables: []
|
|
59
73
|
extensions: []
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
76
|
+
- ".github/workflows/ci.yml"
|
77
|
+
- ".github/workflows/release.yml"
|
62
78
|
- ".gitignore"
|
63
79
|
- ".rspec"
|
64
80
|
- ".travis.yml"
|
@@ -84,15 +100,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
100
|
requirements:
|
85
101
|
- - ">="
|
86
102
|
- !ruby/object:Gem::Version
|
87
|
-
version: '
|
103
|
+
version: '2.7'
|
88
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
105
|
requirements:
|
90
106
|
- - ">="
|
91
107
|
- !ruby/object:Gem::Version
|
92
108
|
version: '0'
|
93
109
|
requirements: []
|
94
|
-
|
95
|
-
rubygems_version: 2.4.8
|
110
|
+
rubygems_version: 3.1.6
|
96
111
|
signing_key:
|
97
112
|
specification_version: 4
|
98
113
|
summary: Provides Rails-like sanitization rules for OpenStruct fields.
|