Sha256: 9bf7b3ef2877eb78182c5048e3e0a61dbbfbe7977401f955b7000fc09f7ed316

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

# typed: true
# frozen_string_literal: true

module Workato
  module Connector
    module Sdk
      class Summarize
        ARRAY_SUMMARIZATION_LIMIT = 100
        STRING_SUMMARIZATION_LIMIT = 1024

        def initialize(data:, paths:)
          @paths = paths
          @data = data
        end

        def call
          return data if paths.blank?

          summarized = data
          paths.each do |path|
            steps = path.split('.')
            if above_summarization_limit?(summarized.dig(*steps))
              summarized = data.deep_dup if summarized.equal?(data)
              apply_summarization_limit(summarized, steps)
            end
          end

          summarized
        end

        private

        attr_reader :data
        attr_reader :paths

        def above_summarization_limit?(candidate)
          candidate.is_a?(::Array) && candidate.length > ARRAY_SUMMARIZATION_LIMIT ||
            candidate.is_a?(::String) && candidate.length > STRING_SUMMARIZATION_LIMIT
        end

        def apply_summarization_limit(summarized, steps)
          container = if steps.length > 1
                        summarized.dig(*steps[0..-2])
                      else
                        summarized
                      end
          candidate = container[steps.last]
          case candidate
          when Array
            candidate[(ARRAY_SUMMARIZATION_LIMIT - 2)..-2] =
              "... #{candidate.length - ARRAY_SUMMARIZATION_LIMIT} items ..."
          when String
            candidate[(STRING_SUMMARIZATION_LIMIT - 1)..-1] =
              "... #{candidate.length - STRING_SUMMARIZATION_LIMIT} characters ..."
          else
            candidate
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workato-connector-sdk-1.2.0 lib/workato/connector/sdk/summarize.rb
workato-connector-sdk-1.0.3 lib/workato/connector/sdk/summarize.rb
workato-connector-sdk-1.1.0 lib/workato/connector/sdk/summarize.rb
workato-connector-sdk-1.0.2 lib/workato/connector/sdk/summarize.rb