sterilizer 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +1 -0
- data/lib/sterilizer.rb +79 -0
- data/lib/sterilizer/version.rb +3 -0
- data/sterilizer.gemspec +21 -0
- metadata +86 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ben Thompson
|
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,53 @@
|
|
1
|
+
# Sterilizer
|
2
|
+
|
3
|
+
A simple module that extends a String, giving it a `sterilize!` method which to ensure the string is always valid UTF-8
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sterilizer'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sterilizer
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
Handy when you want to ensure incoming strings will contain invalid characters
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
dodgy_params = {
|
24
|
+
:ok => "Hello",
|
25
|
+
:dodgy => "hi \255",
|
26
|
+
:accented => "\xE9",
|
27
|
+
}
|
28
|
+
guarantee_strings!(dodgy_params)
|
29
|
+
# =>
|
30
|
+
{
|
31
|
+
:ok=>"Hello",
|
32
|
+
:dodgy=>"hi "
|
33
|
+
:accented=>"é"
|
34
|
+
}
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def guarantee_strings!(unclean_params)
|
39
|
+
unclean_params.each do |_,v|
|
40
|
+
v.sterilize!
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
It uses the https://github.com/oleander/rchardet gem to guess the character encoding if the string isn't valid when forced to UTF-8
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/sterilizer.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require "sterilizer/version"
|
2
|
+
require "rchardet19"
|
3
|
+
|
4
|
+
module Sterilizer
|
5
|
+
def sterilize!
|
6
|
+
|
7
|
+
return self unless !!defined?(Encoding)
|
8
|
+
# return if encoding is valid and equal to default_internal
|
9
|
+
return self if valid_when_default?
|
10
|
+
|
11
|
+
# force to default encoding if valid when forced
|
12
|
+
return self.force_encoding(Encoding.default_internal) if valid_when_forced?
|
13
|
+
|
14
|
+
# At this point, we know the string is not valid encoding, it the encoding is UTF-8,
|
15
|
+
# we must try a different encoding that is valid before forcefully encoding to UTF-8
|
16
|
+
# Otherwise, the encoding type is non-default. If it is valid, encode it to UTF-8, otherwise
|
17
|
+
# find an alternative before forcefully encoding to UTF-8
|
18
|
+
if encoding_is_default?
|
19
|
+
# Might have a situation where encoding is the same as default, but it's not valid
|
20
|
+
# Force it to something else so we can String#encode
|
21
|
+
non_default_encoding = find_a_valid_encoding
|
22
|
+
force_encoding_with(non_default_encoding)
|
23
|
+
else
|
24
|
+
if valid_when_forced?(self.encoding)
|
25
|
+
self.encode!(Encoding.default_internal, self.encoding, { :undef => :replace, :invalid => :replace})
|
26
|
+
else
|
27
|
+
alternative_encoding = find_a_valid_encoding(self.encoding)
|
28
|
+
force_encoding_with(alternative_encoding)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
rescue
|
32
|
+
self.force_encoding_with("ASCII")
|
33
|
+
end
|
34
|
+
|
35
|
+
def encoding_is_default?
|
36
|
+
self.encoding == Encoding.default_internal
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid_when_default?
|
40
|
+
self.valid_encoding? && encoding_is_default?
|
41
|
+
end
|
42
|
+
|
43
|
+
def valid_when_forced?(encoding = Encoding.default_internal)
|
44
|
+
self.dup.force_encoding(encoding).valid_encoding?
|
45
|
+
end
|
46
|
+
|
47
|
+
def find_a_valid_encoding(ignoring = [Encoding.default_internal], guessed_already = false)
|
48
|
+
# If we've already tried to guess the encoding, resort to picking one at random until valid
|
49
|
+
if guessed_already
|
50
|
+
provisional_encoding = Encoding.list.detect{ |encoding| !ignoring.include?(encoding) }
|
51
|
+
else # On first run, we'll try and guess the character encoding
|
52
|
+
provisional_encoding = guess_encoding
|
53
|
+
end
|
54
|
+
|
55
|
+
# If the provisional encoding is valid when string is forced to it, select it otherwise continue to find one
|
56
|
+
if valid_when_forced?(provisional_encoding)
|
57
|
+
provisional_encoding
|
58
|
+
else
|
59
|
+
find_a_valid_encoding(ignoring << provisional_encoding, :guess_failed)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Use an external library to attempt to (silently) guess the encoding
|
64
|
+
def guess_encoding(guesser = CharDet)
|
65
|
+
Encoding.find(guesser.detect(self, :silent => true)["encoding"])
|
66
|
+
end
|
67
|
+
|
68
|
+
def force_encoding_with(encoding)
|
69
|
+
self.force_encoding(encoding).encode(Encoding.default_internal, :invalid => :replace, :undef => :replace)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
# Add sterilize methods to a string if it is sterilized, saves clobbering every string!
|
75
|
+
class String
|
76
|
+
def sterilize!
|
77
|
+
self.extend(Sterilizer).sterilize!
|
78
|
+
end
|
79
|
+
end
|
data/sterilizer.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sterilizer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sterilizer"
|
8
|
+
gem.version = Sterilizer::VERSION
|
9
|
+
gem.authors = ["Ben Thompson"]
|
10
|
+
gem.email = ["ben@atechmedia.com"]
|
11
|
+
gem.description = %q{Guarantee a string}
|
12
|
+
gem.summary = %q{}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency "rchardet19"
|
20
|
+
gem.add_development_dependency 'pry'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sterilizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Thompson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rchardet19
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pry
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Guarantee a string
|
47
|
+
email:
|
48
|
+
- ben@atechmedia.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/sterilizer.rb
|
59
|
+
- lib/sterilizer/version.rb
|
60
|
+
- sterilizer.gemspec
|
61
|
+
homepage: ''
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.23
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: ''
|
85
|
+
test_files: []
|
86
|
+
has_rdoc:
|