Sha256: 362f8e87e7cb45613c7997c6ad5615521cc1f2b58600d4b227392bf481d0e418
Contents?: true
Size: 1.19 KB
Versions: 1
Compression:
Stored size: 1.19 KB
Contents
require 'active_support/multibyte' require 'active_support/core_ext/object/acts_like' require 'active_support/core_ext/string/behavior' require 'ensure_valid_encoding' require 'gigo/rchardet' require 'gigo/version' module GIGO def self.load(data) return data if data.nil? || !data.acts_like?(:string) encoded_string = safe_detect_and_encoder(data) return data if data.encoding == forced_encoding && data == encoded_string encoded_string end protected def self.safe_detect_and_encoder(data) string = data begin string = ActiveSupport::Multibyte.proxy_class.new(string).tidy_bytes rescue Exception => e begin encoding = CharDet.detect(string.dup)['encoding'] || string.encoding || Encoding.default_internal || forced_encoding string = string.force_encoding(encoding).encode forced_encoding, :undef => :replace, :invalid => :replace rescue Exception => e string = string.encode forced_encoding, :undef => :replace, :invalid => :replace end end EnsureValidEncoding.ensure_valid_encoding string, invalid: :replace, replace: "?" end def self.forced_encoding Encoding.default_internal || Encoding::UTF_8 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gigo-1.1.0 | lib/gigo.rb |