Sha256: 7225e1cd5bdd7682fd1b7675f25bb4167968ff671e93f0f9a285bac2a8df80c6

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class Guess
      attr_reader :catalogue

      def initialize(catalogue)
        @catalogue = catalogue
      end

      def license_for(raw)
        raw.is_a?(Hash) ? from_hash(raw) : from_string(raw)
      end

      private

      def from_hash(hash)
        from_string(hash[:name]) ||
          from_url(hash[:url]) ||
          unknown(hash[:name] || hash[:url])
      end

      def from_string(raw)
        return if raw.nil?

        content = Content.new(raw)

        catalogue[raw] ||
          catalogue[raw.split(' ').join('-')] ||
          match_name(content) ||
          match_body(content) ||
          unknown(raw)
      end

      def from_url(url)
        return if url.nil? || url.empty?

        response = Spandx.http.get(url)
        return unless Spandx.http.ok?(response)

        license_for(response.body)
      end

      def match_name(content)
        return if content.tokens.size < 2 || content.tokens.size > 10

        result = from_expression(content)
        return result if result

        threshold = 85.0
        catalogue.find do |license|
          content.similar?(Content.new(license.name), threshold: threshold)
        end
      end

      def match_body(content)
        score = Score.new(nil, nil)
        threshold = 89.0
        catalogue.each do |license|
          next if license.deprecated_license_id?

          percentage = content.similarity_score(content_for(license))
          next if percentage < threshold
          next if score.score >= percentage

          score.update(percentage, license)
        end
        score&.item
      end

      def content_for(license)
        ::Spandx::Core::Content.new(Spandx.git[:spdx].read("text/#{license.id}.txt") || '')
      end

      def unknown(text)
        ::Spandx::Spdx::License.unknown(text)
      end

      def from_expression(content)
        Spandx::Spdx::CompositeLicense
          .from_expression(content.raw, catalogue)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spandx-0.13.1 lib/spandx/core/guess.rb
spandx-0.13.0 lib/spandx/core/guess.rb