Sha256: a9e6d3920acf049747bbd00c4e8489466c0ee82b67d82dc44dcc0cd547e792d4

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Akita
  module HarLogger
    class HarUtils
      # Rack apparently uses 8-bit ASCII for everything, even when the string
      # is not 8-bit ASCII. This reinterprets 8-bit ASCII strings as UTF-8.
      #
      # If we are unable to do this reinterpretation, return the string
      # unchanged, but log a warning that points to the caller.
      def self.fixEncoding(v)
        if v != nil && v.encoding == Encoding::ASCII_8BIT then
          forced = String.new(v).force_encoding(Encoding::UTF_8)
          if forced.valid_encoding? then
            v = forced
          else
            Rails.logger.warn "[#{caller_locations(1, 1)}] Unable to fix encoding: not a valid UTF-8 string. This will likely cause JSON serialization to fail."
          end
        end

        v
      end

      # Converts a Hash into a list of Hash objects. Each entry in the given
      # Hash will be represented in the output by a Hash object that maps
      # 'name' to the entry's key and 'value' to the entry's value.
      def self.hashToList(hash)
        hash.reduce([]) { |accum, (k, v)|
          accum.append({
            name: fixEncoding(k),
            value: fixEncoding(v),
          })
        }
      end

      # Determines whether all values in a Hash are strings.
      def self.allValuesAreStrings(hash)
        hash.each do |_, value|
          if !(value.is_a? String) then
            return false
          end
        end

        return true
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
akita-har_logger-0.2.5 lib/akita/har_logger/har_utils.rb