class LicenseScout::SPDX

Public Class Methods

exceptions() click to toggle source

@return [Hash] The SPDX license data in Hash form

# File lib/license_scout/spdx.rb, line 52
def exceptions
  @@license_data ||= FFI_Yajl::Parser.parse(File.read(File.expand_path("data/exceptions.json", __dir__)))["exceptions"]
end
find(license_id, force = false) click to toggle source

Try to find the SPDX ID that most closely matches the given license ID

@param license_id [String, nil] The license ID @return [String, nil, false] Returns either the SPDX ID, false if the

license_id was nil, or nil if we could not find a valid SPDX ID
# File lib/license_scout/spdx.rb, line 32
def find(license_id, force = false)
  return license_id if force
  return nil if license_id.nil? || %w{ NOASSERTION NONE }.include?(license_id)

  lookup(license_id) || find_by_special_case(license_id) || closest(license_id) || license_id
end
known_ids() click to toggle source
# File lib/license_scout/spdx.rb, line 56
def known_ids
  @@known_ids ||= licenses.map { |l| l["licenseId"] }
end
known_names() click to toggle source
# File lib/license_scout/spdx.rb, line 60
def known_names
  @@known_names ||= licenses.map { |l| l["name"] }
end
licenses() click to toggle source

@return [Hash] The SPDX license data in Hash form

# File lib/license_scout/spdx.rb, line 47
def licenses
  @@license_data ||= FFI_Yajl::Parser.parse(File.read(File.expand_path("data/licenses.json", __dir__)))["licenses"]
end
parse(license_string) click to toggle source

Right now this just returns the license keys that are present in the string. In the future, we should handle a proper compound structure like github.com/jslicense/spdx-expression-parse.js

# File lib/license_scout/spdx.rb, line 42
def parse(license_string)
  license_string.nil? ? [] : (license_string.tr("()", "").split("\s") - spdx_join_words)
end

Private Class Methods

closest(license_id) click to toggle source
# File lib/license_scout/spdx.rb, line 79
def closest(license_id)
  spdx_for(FuzzyMatch.new(known_names).find(license_id)) || FuzzyMatch.new(known_ids).find(license_id)
end
find_by_special_case(license_id) click to toggle source
# File lib/license_scout/spdx.rb, line 72
def find_by_special_case(license_id)
  gpl = gpl_match(license_id)
  return gpl unless gpl.nil?

  lookup(special_cases[license_id.downcase])
end
gpl_match(license_id) click to toggle source
# File lib/license_scout/spdx.rb, line 83
def gpl_match(license_id)
  match = license_id.match(/^(l|a)?gpl-?\s?_?v?(1|2|3)\.?(\d)?(\+)?$/i)
  return unless match

  lookup("#{match[1]}GPL-#{match[2]}.#{match[3] || 0}#{match[4]}".upcase)
end
lookup(license_id) click to toggle source
# File lib/license_scout/spdx.rb, line 66
def lookup(license_id)
  return license_id if known_ids.include?(license_id)
  return spdx_for(license_id) if (Array(license_id) & known_names).any?
  return license_id if (parse(license_id) & known_ids).any?
end
spdx_for(license_name) click to toggle source
# File lib/license_scout/spdx.rb, line 90
def spdx_for(license_name)
  licenses.find { |n| n["name"] == license_name }["licenseId"]
end
spdx_join_words() click to toggle source
# File lib/license_scout/spdx.rb, line 94
def spdx_join_words
  %w{WITH AND OR}
end
special_cases() click to toggle source
# File lib/license_scout/spdx.rb, line 98
def special_cases
  {
    "agpl_3" => "AGPL-3.0",
    "apache_1_1" => "Apache-1.1",
    "apache_2_0" => "Apache-2.0",
    "artistic_1" => "Artistic-1.0",
    "artistic_2" => "Artistic-2.0",
    "bsd" => "BSD-3-Clause",
    "freebsd" => "BSD-2-Clause-FreeBSD",
    "gfdl_1_2" => "GFDL-1.2-only",
    "gfdl_1_3" => "GFDL-1.3-only",
    "lgpl_2_1" => "LGPL-2.1-only",
    "lgpl_3_0" => "LGPL-3.0-only",
    "mit" => "MIT",
    "mozilla_1_0" => "MPL-1.0",
    "mozilla_1_1" => "MPL-1.1",
    "mplv1.0" => "MPL-1.0",
    "mplv1.1" => "MPL-1.1",
    "openssl" => "OpenSSL",
    "qpl_1_0" => "QPL-1.0",
    "perl" => "Artistic-1.0-Perl",
    "perl_5" => "Artistic-1.0-Perl",
    "ssleay" => "OpenSSL",
    "sun" => "SISSL",
    "zlib" => "Zlib",
  }
end