Sha256: 1d401c3f1c4e2a24b454f87cd9d20e152c024c3d64f10c8a842da06f3dd856ab

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/utils/string_utils'
require 'contrast/utils/sha256_builder'
require 'yaml'

module Contrast
  module Api
    module Decorators
      # Used to decorate the Library protobuf model to handle Gem::Specification translation
      module Library
        def self.included klass
          klass.extend(ClassMethods)
        end
        # Used to add class methods to the Library class on inclusion of the decorator
        module ClassMethods
          def build digest, gem_specification
            msg = new
            msg.file_path = Contrast::Utils::StringUtils.force_utf8(gem_specification.name)
            msg.hash_code   = Contrast::Utils::StringUtils.force_utf8(digest)
            msg.version     = Contrast::Utils::StringUtils.force_utf8(gem_specification.version)
            msg.manifest    = Contrast::Utils::StringUtils.force_utf8(build_manifest(gem_specification))
            msg.external_ms = date_to_ms(gem_specification.date)
            msg.internal_ms = msg.external_ms
            msg.url         = Contrast::Utils::StringUtils.force_utf8(gem_specification.homepage)
            msg.class_count = file_count(gem_specification.full_gem_path.to_s)
            msg.used_class_count = 0
            msg
          end

          # These are all the code files that are located in the Gem directory loaded
          # by the current environment; this includes more than Ruby files
          def file_count path
            Contrast::Utils::Sha256Builder.instance.files(path).length
          end

          def build_manifest spec
            Contrast::Utils::StringUtils.force_utf8(spec.to_yaml.to_s)
          rescue StandardError
            nil
          end

          def date_to_ms date
            (date.to_f * 1000.0).to_i
          end
        end
      end
    end
  end
end

Contrast::Api::Dtm::Library.include(Contrast::Api::Decorators::Library)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
contrast-agent-4.2.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.1.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.0.0 lib/contrast/api/decorators/library.rb