Skip to content

sanemat/awesome_scrub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AwesomeScrub

Easy to use String#scrub. Prevent Invalid byte sequence in UTF-8. Wrap params[:name].respond_to?(:scrub) ? params[:name].scrub : params[:name] to params[:name].awesome_scrub.

Gem Version Build Status Code Climate

Use case

Before:

@name = params[:name].presence || 'nobody'

After:

@name = awesome_scrub(params[:name]).presence || 'nobody'

Point

Almost time this is no problem, but params[:name] include invalid byte sequence in UTF-8, like invalid_byte_sequence.presence (invalid_byte_sequence.present?) raises ArgumentError: invalid byte sequence in UTF-8.

This problem's solution is String#scrub(Feature of Ruby2.1, and string-scrub gem which backport for Ruby2.0), this replaces invalid characters. Feature #6752: Replacing ill-formed subsequencce - ruby-trunk - Ruby Issue Tracking System

This stil has problem for writing code, params[:name] is String or nil, and some case Array, Fixnum, and other object does not have #scrub method.

Work around

name = params[:name].respond_to?(:scrub) ? params[:name].scrub : params[:name]
@name = name.presence || 'nobody'

Solution

@name = awesome_scrub(params[:name]).presence || 'nobody'

Usage

Methods

# call scrub
awesome_scrub(args) #=> args.scrub

# call scrub!
awesome_scrub!(args) #=> args.scrub!

Examples

class Foo
  include AwesomeScrub

  def bar
    awesome_scrub(any)
  end
end

OR

class Foo
  extend AwesomeScrub

  def bar
    awesome_scrub(any)
  end
end

OR

AwesomeScrub::awesome_scrub(any)

Installation

Add this line to your application's Gemfile:

gem 'awesome_scrub'

And then execute:

$ bundle

Or install it yourself as:

$ gem install awesome_scrub

Contributing

  1. Fork it ( http://github.com/sanemat/awesome_scrub/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Easy to use `String#scrub`. like: `awesome_scrub(params[:name]).present?`

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages