Sterilizer
A simple module that extends a String, giving it a sterilize!
method to ensure the string is always valid UTF-8
Installation
Add this line to your application's Gemfile:
gem 'sterilizer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sterilizer
Usage
Handy when you want to ensure incoming strings will contain invalid characters
dodgy_params = {
:ok => "Hello",
:dodgy => "hi \255",
:accented => "\xE9",
}
guarantee_strings!(dodgy_params)
# =>
{
:ok=>"Hello",
:dodgy=>"hi "
:accented=>"é"
}
private
def guarantee_strings!(unclean_params)
unclean_params.each do |_,v|
v.sterilize!
end
end
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
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request