Sha256: 22428ba184e3bf74b2fd76be47237b986681e1dfefce925d0176d9e6ac2d3094

Contents?: true

Size: 1.21 KB

Versions: 20

Compression:

Stored size: 1.21 KB

Contents

require 'active_support/concern'
require 'charlock_holmes'

module Elasticsearch
  module Git
    module EncoderHelper
      extend ActiveSupport::Concern

      included do
        def encode!(message)
          return nil unless message.respond_to? :force_encoding

          # if message is utf-8 encoding, just return it
          message.force_encoding("UTF-8")
          return message if message.valid_encoding?

          # return message if message type is binary
          detect = CharlockHolmes::EncodingDetector.detect(message)
          return message.force_encoding("BINARY") if detect && detect[:type] == :binary

          # encoding message to detect encoding
          if detect && detect[:encoding]
            message.force_encoding(detect[:encoding])
          end

          # encode and clean the bad chars
          message.replace clean(message)
        rescue
          encoding = detect ? detect[:encoding] : "unknown"
          "--broken encoding: #{encoding}"
        end

        private

        def clean(message)
          message.encode("UTF-16BE", undef: :replace, invalid: :replace, replace: "")
          .encode("UTF-8")
          .gsub("\0".encode("UTF-8"), "")
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
gitlab-elasticsearch-git-1.2.0 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-1.1.1 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-1.1.0 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-1.0.2 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-1.0.1 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-1.0.0 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.17 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.16 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.15 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.14 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.13 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.12 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.11 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.10 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.9 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.8 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.7 lib/elasticsearch/git/encoder_helper.rb
gitlab-elasticsearch-git-0.0.6 lib/elasticsearch/git/encoder_helper.rb
elasticsearch-git-0.0.5 lib/elasticsearch/git/encoder_helper.rb
elasticsearch-git-0.0.4 lib/elasticsearch/git/encoder_helper.rb