Sha256: f4df63f92979eb421bebdb6b14ae7a7105ba4a7a000887cd6d719b92fa941121

Contents?: true

Size: 1.66 KB

Versions: 192

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require 'pathname'

module Aws
  module Log
    # @api private
    class ParamFormatter

      # String longer than the max string size are truncated
      MAX_STRING_SIZE = 1000

      def initialize(options = {})
        @max_string_size = options[:max_string_size] || MAX_STRING_SIZE
      end

      def summarize(value)
        Hash === value ? summarize_hash(value) : summarize_value(value)
      end

      private

      def summarize_hash(hash)
        hash.keys.first.is_a?(String) ?
          summarize_string_hash(hash) :
          summarize_symbol_hash(hash)
      end

      def summarize_symbol_hash(hash)
        hash.map do |key,v|
          "#{key}:#{summarize_value(v)}"
        end.join(",")
      end

      def summarize_string_hash(hash)
        hash.map do |key,v|
          "#{key.inspect}=>#{summarize_value(v)}"
        end.join(",")
      end

      def summarize_string(str)
        if str.size > @max_string_size
          "#<String #{str[0...@max_string_size].inspect} ... (#{str.size} bytes)>"
        else
          str.inspect
        end
      end

      def summarize_value(value)
        case value
        when String   then summarize_string(value)
        when Hash     then '{' + summarize_hash(value) + '}'
        when Array    then summarize_array(value)
        when File     then summarize_file(value.path)
        when Pathname then summarize_file(value)
        else value.inspect
        end
      end

      def summarize_file(path)
        "#<File:#{path} (#{File.size(path)} bytes)>"
      end

      def summarize_array(array)
        "[" + array.map{|v| summarize_value(v) }.join(",") + "]"
      end

    end
  end
end

Version data entries

192 entries across 192 versions & 1 rubygems

Version Path
aws-sdk-core-3.180.3 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.180.2 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.180.1 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.180.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.179.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.178.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.177.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.176.1 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.176.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.175.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.174.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.173.1 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.173.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.172.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.171.1 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.171.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.170.1 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.170.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.169.0 lib/aws-sdk-core/log/param_formatter.rb
aws-sdk-core-3.168.4 lib/aws-sdk-core/log/param_formatter.rb