utf8_attribute_sanitiser 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 138d5535c2fb1140fddb6bab8404f4f94b4a7da8
4
- data.tar.gz: 8fbf3eb56cd339505aa474a56b0a943696f3ccbc
3
+ metadata.gz: cbb7d85533242790e75e8f9e43895bf387de33ab
4
+ data.tar.gz: 54c1ea7a00a4484421aa15a70e4aab27cb9afe23
5
5
  SHA512:
6
- metadata.gz: 9211b4d1f72ef09a3a65660fba1867c601694a9c35d7493f239dd855684b5241ee1a185f3afbffeaaf2b870d40d3ff58f23a2ea8a9669434d5616c11151fde5f
7
- data.tar.gz: 7e1925b3c6b60d7f22916d98f96be48a322e08e1b2f81da186297059f16c96877aaa36e95130932f2c80ec668046b816cf8d16c80fb25f8f38b5e3a83ad5f594
6
+ metadata.gz: 932b99e00cbd3efd7623202b5d37a46abb4331ca898be055f4ea57099cee8051650beaab4ebcb038973f5b3ed83d2bbc50b9fa97efbfd45d3196bb6b3c6d9edb
7
+ data.tar.gz: af36edb47e5ff513201b4d0598071d0d1dd2cb8ae845e259490718e6d04048cc7c87988f5df7cdcef5bf6dda9228ef7185bce1c469a3286031798cd76a7e6099
data/README.md CHANGED
@@ -26,13 +26,21 @@ In your model add utf8_attribute_sanitiser, specyfing list of attribute names to
26
26
  utf8_attribute_sanitiser :name, :address
27
27
  end
28
28
 
29
+ Default cleaning method is `utf8mb4only` and it only affects 4 byte characters
30
+
31
+ You can use aggressive clean method that strips out all invalud utf8 characters like
32
+
33
+ class User < ActiveRecord::Base
34
+ utf8_attribute_sanitiser :name, :address, method: :aggressive
35
+ end
36
+
29
37
  ## Running tests
30
38
 
31
39
  $ rspec
32
40
 
33
41
  ## Contributing
34
42
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/utf8_attribute_sanitiser.
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tomdracz/utf8_attribute_sanitiser.
36
44
 
37
45
 
38
46
  ## License
@@ -1,11 +1,15 @@
1
1
  require "utf8_attribute_sanitiser/version"
2
2
 
3
3
  module Utf8AttributeSanitiser
4
- def utf8_attribute_sanitiser(*attributes)
4
+ def utf8_attribute_sanitiser(*attributes, method: :utf8mb4only)
5
5
  attributes.each do |attribute|
6
6
  before_validation do |record|
7
7
  value = record[attribute]
8
- sanitised_value = value.respond_to?(:encode) ? value.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: "") : value
8
+ if method == :utf8mb4only
9
+ sanitised_value = value.respond_to?(:encode) ? value.each_char.select{|c| c.bytes.count < 4 }.join('') : value
10
+ elsif method == :aggressive
11
+ sanitised_value = value.respond_to?(:gsub) ? value.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: "") : value
12
+ end
9
13
  record[attribute] = sanitised_value
10
14
  end
11
15
  end
@@ -1,3 +1,3 @@
1
1
  module Utf8AttributeSanitiser
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utf8_attribute_sanitiser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Dracz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-24 00:00:00.000000000 Z
11
+ date: 2017-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.6.8
93
+ rubygems_version: 2.6.10
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Sanitise attributes to avoid invalid byte sequence argument errors