Sha256: ee15b60fc95765116d692fba0b5e8f3dc88cfa4536da8127408e48e6d2058597

Contents?: true

Size: 1.63 KB

Versions: 49

Compression:

Stored size: 1.63 KB

Contents

module Rollbar
  module Encoding
    class Encoder
      ALL_ENCODINGS = [::Encoding::UTF_8, ::Encoding::ISO_8859_1, ::Encoding::ASCII_8BIT, ::Encoding::US_ASCII]
      ASCII_ENCODINGS = [::Encoding::US_ASCII, ::Encoding::ASCII_8BIT, ::Encoding::ISO_8859_1]
      ENCODING_OPTIONS = { :invalid => :replace, :undef => :replace, :replace => '' }
      UTF8 = 'UTF-8'.freeze
      BINARY = 'binary'.freeze

      attr_accessor :object

      def initialize(object)
        @object = object
      end

      def encode
        value = object.to_s
        encoding = value.encoding

        # This will be most of cases so avoid force anything for them
        if encoding == ::Encoding::UTF_8 && value.valid_encoding?
          encoded_value = value
        else
          encoded_value = force_encoding(value).encode(*encoding_args(value))
        end

        object.is_a?(Symbol) ? encoded_value.to_sym : encoded_value
      end

      private

      def force_encoding(value)
        return value if value.frozen?

        value.force_encoding(detect_encoding(value)) if value.encoding == ::Encoding::UTF_8

        value
      end

      def detect_encoding(v)
        value = v.dup

        ALL_ENCODINGS.detect do |encoding|
          begin
            # Seems #codepoints is faster than #valid_encoding?
            value.force_encoding(encoding).encode(::Encoding::UTF_8).codepoints
            true
          rescue
            false
          end
        end
      end

      def encoding_args(value)
        args = [UTF8]
        args << BINARY if ASCII_ENCODINGS.include?(value.encoding)
        args << ENCODING_OPTIONS

        args
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
rollbar-2.13.2 lib/rollbar/encoding/encoder.rb
rollbar-2.13.1 lib/rollbar/encoding/encoder.rb
rollbar-2.13.0 lib/rollbar/encoding/encoder.rb
rollbar-2.12.0 lib/rollbar/encoding/encoder.rb
rollbar-2.11.5 lib/rollbar/encoding/encoder.rb
rollbar-2.11.4 lib/rollbar/encoding/encoder.rb
rollbar-2.11.3 lib/rollbar/encoding/encoder.rb
rollbar-2.11.2 lib/rollbar/encoding/encoder.rb
rollbar-2.11.1 lib/rollbar/encoding/encoder.rb
rollbar-2.11.0 lib/rollbar/encoding/encoder.rb
rollbar-2.10.0 lib/rollbar/encoding/encoder.rb
rollbar-2.9.1 lib/rollbar/encoding/encoder.rb
rollbar-2.9.0 lib/rollbar/encoding/encoder.rb
rollbar-2.8.3 lib/rollbar/encoding/encoder.rb
rollbar-2.8.2 lib/rollbar/encoding/encoder.rb
rollbar-2.8.1 lib/rollbar/encoding/encoder.rb
rollbar-2.8.0 lib/rollbar/encoding/encoder.rb
rollbar-2.7.1 lib/rollbar/encoding/encoder.rb
rollbar-2.7.0 lib/rollbar/encoding/encoder.rb
rollbar-2.6.3 lib/rollbar/encoding/encoder.rb