Sha256: 1e739a6f13a47893817790509085aca2bb58395936b7a47ceccc68b8e297f108

Contents?: true

Size: 658 Bytes

Versions: 19

Compression:

Stored size: 658 Bytes

Contents

# typed: true

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

module Sqreen
  class EncodingSanitizer
    def self.sanitize(obj)
      case obj
      when String
        sanitize_string(obj)
      when Array
        obj.map { |e| sanitize(e) }
      when Hash
        obj.each_with_object({}) { |(k, v), h| h[k] = sanitize(v) }
      else
        obj
      end
    end

    def self.sanitize_string(s)
      return s if s.encoding.name == 'UTF-8' && s.valid_encoding?

      s.encode('UTF-16', :invalid => :replace, :undef => :replace).encode('UTF-8')
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
sqreen-1.20.4.beta1 lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.4 lib/sqreen/encoding_sanitizer.rb
sqreen-1.20.3 lib/sqreen/encoding_sanitizer.rb
sqreen-1.20.2 lib/sqreen/encoding_sanitizer.rb
sqreen-1.21.0.beta2 lib/sqreen/encoding_sanitizer.rb
sqreen-1.21.0.beta1 lib/sqreen/encoding_sanitizer.rb
sqreen-1.20.1-java lib/sqreen/encoding_sanitizer.rb
sqreen-1.20.1 lib/sqreen/encoding_sanitizer.rb
sqreen-1.20.0-java lib/sqreen/encoding_sanitizer.rb
sqreen-1.20.0 lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.3-java lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.3 lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.2-java lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.2 lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.1-java lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.1 lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.0-java lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.0 lib/sqreen/encoding_sanitizer.rb
sqreen-1.19.0.beta1 lib/sqreen/encoding_sanitizer.rb