module Spdx

Constants

VERSION

Public Class Methods

exception_exists?(id) click to toggle source
# File lib/spdx.rb, line 26
def self.exception_exists?(id)
  exceptions_downcase.key?(id.to_s.downcase)
end
exceptions() click to toggle source
# File lib/spdx.rb, line 14
def self.exceptions
  @exceptions ||=
    JSON.parse(File.read(File.expand_path("../exceptions.json", __dir__)))["exceptions"]
      .each_with_object({}) do |details, all|
      all[details.delete("licenseExceptionId")] = details
    end
end
exceptions_downcase() click to toggle source
# File lib/spdx.rb, line 44
def self.exceptions_downcase
  @exceptions_downcase ||= exceptions.keys.each_with_object({}) do |(id, _license), all|
    all[id.downcase] = id
  end
end
license_exists?(id) click to toggle source
# File lib/spdx.rb, line 22
def self.license_exists?(id)
  licenses_downcase.key?(id.to_s.downcase)
end
licenses() click to toggle source
# File lib/spdx.rb, line 30
def self.licenses
  @licenses ||=
    JSON.parse(File.read(File.expand_path("../licenses.json", __dir__)))["licenses"]
      .each_with_object({}).each do |details, all|
        all[details.delete("licenseId")] = details
      end
end
licenses_downcase() click to toggle source
# File lib/spdx.rb, line 38
def self.licenses_downcase
  @licenses_downcase ||= licenses.keys.each_with_object({}) do |(id, _license), all|
    all[id.downcase] = id
  end
end
names() click to toggle source
# File lib/spdx.rb, line 10
def self.names
  (licenses.keys + licenses.map { |_k, v| v["name"] }).sort
end
normalize(spdx_string, top_level_parens: false) click to toggle source
# File lib/spdx.rb, line 50
def self.normalize(spdx_string, top_level_parens: false)
  normalize_tree(SpdxParser.parse(spdx_string), parens: top_level_parens)
end
parse(spdx_string) click to toggle source
# File lib/spdx.rb, line 106
def self.parse(spdx_string)
  SpdxParser.parse(spdx_string)
end
valid?(spdx_string) click to toggle source
# File lib/spdx.rb, line 97
def self.valid?(spdx_string)
  return false unless spdx_string.is_a?(String)

  SpdxParser.parse(spdx_string)
  true
rescue SpdxGrammar::SpdxParseError
  false
end

Private Class Methods

normalize_tree(node, parens: true) click to toggle source
# File lib/spdx.rb, line 54
                     def self.normalize_tree(node, parens: true)
  case node
  when SpdxGrammar::LogicalAnd
    left = normalize_tree(node.left)
    right = normalize_tree(node.right)
    if parens
      "(#{left} AND #{right})"
    else
      "#{left} AND #{right}"
    end
  when SpdxGrammar::LogicalOr
    left = normalize_tree(node.left)
    right = normalize_tree(node.right)
    if parens
      "(#{left} OR #{right})"
    else
      "#{left} OR #{right}"
    end
  when SpdxGrammar::With
    license = normalize_tree(node.license)
    exception = normalize_tree(node.exception)
    if parens
      "(#{license} WITH #{exception})"
    else
      "#{license} WITH #{exception}"
    end
  when SpdxGrammar::None
    "NONE"
  when SpdxGrammar::NoAssertion
    "NOASSERTION"
  when SpdxGrammar::License
    licenses_downcase[node.text_value.downcase]
  when SpdxGrammar::LicensePlus
    "#{normalize_tree(node.child)}+"
  when SpdxGrammar::LicenseRef
    node.text_value
  when SpdxGrammar::DocumentRef
    node.text_value
  when SpdxGrammar::LicenseException
    exceptions_downcase[node.text_value.downcase]
  end
end