Sha256: 4a6fb802d6277eb4a99aad6bb65608d25c386d62475665b2db48ab0de6c6b9a5

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Workato
  module Types
    class Binary < ::String
      TITLE_LENGTH = 16
      SUMMARY_LENGTH = 128

      def initialize(str)
        super(str)
        force_encoding(Encoding::ASCII_8BIT)
      end

      def to_s
        self
      end

      def to_json(_options = nil)
        summary
      end

      def binary?
        true
      end

      def base64
        Base64.strict_encode64(self)
      end

      def as_string(encoding)
        ::String.new(self, encoding: encoding).encode(encoding, invalid: :replace, undef: :replace)
      end

      def as_utf8
        as_string('utf-8')
      end

      def sha1
        Binary.new(::Digest::SHA1.digest(self))
      end

      private

      def summary
        if length.positive?
          left = "0x#{byteslice(0, SUMMARY_LENGTH).unpack1('H*')}"
          right = bytesize > SUMMARY_LENGTH ? "…(#{bytesize - SUMMARY_LENGTH} bytes more)" : ''
          "#{left}#{right}"
        else
          ''
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workato-connector-sdk-1.3.0 lib/workato/types/binary.rb