Class: Coder::Cleaner::Iconv
- Inherits:
-
Object
- Object
- Coder::Cleaner::Iconv
- Defined in:
- lib/coder/cleaner/iconv.rb
Class Method Summary collapse
Instance Method Summary collapse
- #clean(str) ⇒ Object
-
#initialize(encoding) ⇒ Iconv
constructor
A new instance of Iconv.
Constructor Details
#initialize(encoding) ⇒ Iconv
Returns a new instance of Iconv.
34 35 36 37 38 39 40 41 |
# File 'lib/coder/cleaner/iconv.rb', line 34 def initialize(encoding) @encoding = encoding.to_s @nullbyte = "\0" @iconv = ::Iconv.new("#{encoding}//ignore", @encoding) @nullbyte.encode! @encoding if @nullbyte.respond_to? :encode! rescue ::Iconv::InvalidEncoding => e raise Coder::InvalidEncoding, e. end |
Class Method Details
.available? ⇒ Boolean
27 28 29 30 31 32 |
# File 'lib/coder/cleaner/iconv.rb', line 27 def self.available? load_iconv !!::Iconv.conv("iso-8859-1//ignore", "utf-8", "\305\253" + "a"*8160) rescue Exception false end |
.load_iconv ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/coder/cleaner/iconv.rb', line 8 def self.load_iconv return if defined? ::Iconv stderr_was, $stderr = $stderr, StringIO.new require 'iconv' ensure $stderr = stderr_was if stderr_was end |
.new ⇒ Object
16 17 18 19 |
# File 'lib/coder/cleaner/iconv.rb', line 16 def self.new(*) load_iconv super end |
.supports?(encoding) ⇒ Boolean
21 22 23 24 25 |
# File 'lib/coder/cleaner/iconv.rb', line 21 def self.supports?(encoding) encoding.to_s !~ /^ucs/i and ::Iconv.new("#{encoding}//ignore", encoding.to_s) rescue Exception false end |
Instance Method Details
#clean(str) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/coder/cleaner/iconv.rb', line 43 def clean(str) string = @iconv.iconv(str).gsub(@nullbyte, "") Coder.force_encoding! string, @encoding rescue ::Iconv::Failure => e raise Coder::Error, e. end |