Sha256: 74164b40fe411e31f84de6d3dee67ad5167f90fb69ac0c14cd463ad57a3753ea

Contents?: true

Size: 1.45 KB

Versions: 14

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true
require 'json'
require 'net/http'
require 'uri'

licenses_json = Net::HTTP.get(URI('https://spdx.org/licenses/licenses.json'))
licenses = JSON.parse(licenses_json)['licenses'].map do |licenseObject|
  licenseObject['licenseId']
end
exceptions_json = Net::HTTP.get(URI('https://spdx.org/licenses/exceptions.json'))
exceptions = JSON.parse(exceptions_json)['exceptions'].map do |exceptionObject|
  exceptionObject['licenseExceptionId']
end

open 'lib/rubygems/util/licenses.rb', 'w' do |io|
  io.write <<-RUBY
# frozen_string_literal: true
require 'rubygems/text'

class Gem::Licenses
  extend Gem::Text

  NONSTANDARD = 'Nonstandard'.freeze

  # Software Package Data Exchange (SPDX) standard open-source software
  # license identifiers
  LICENSE_IDENTIFIERS = %w(
      #{licenses.sort.join "\n      "}
  ).freeze

  # exception identifiers
  EXCEPTION_IDENTIFIERS = %w(
      #{exceptions.sort.join "\n      "}
  ).freeze

  REGEXP = %r{
    \\A
    (
      \#{Regexp.union(LICENSE_IDENTIFIERS)}
      \\+?
      (\\s WITH \\s \#{Regexp.union(EXCEPTION_IDENTIFIERS)})?
      | \#{NONSTANDARD}
    )
    \\Z
  }ox.freeze

  def self.match?(license)
    !REGEXP.match(license).nil?
  end

  def self.suggestions(license)
    by_distance = LICENSE_IDENTIFIERS.group_by do |identifier|
      levenshtein_distance(identifier, license)
    end
    lowest = by_distance.keys.min
    return unless lowest < license.size
    by_distance[lowest]
  end
end
  RUBY
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rubygems-update-3.0.9 util/generate_spdx_license_list.rb
rubygems-update-2.7.11 util/generate_spdx_license_list.rb
rubygems-update-3.0.8 util/generate_spdx_license_list.rb
rubygems-update-3.0.7 util/generate_spdx_license_list.rb
rubygems-update-3.0.6 util/generate_spdx_license_list.rb
rubygems-update-3.0.5 util/generate_spdx_license_list.rb
rubygems-update-3.0.4 util/generate_spdx_license_list.rb
rubygems-update-2.7.10 util/generate_spdx_license_list.rb
rubygems-update-2.7.9 util/generate_spdx_license_list.rb
rubygems-update-3.0.3 util/generate_spdx_license_list.rb
rubygems-update-3.0.2 util/generate_spdx_license_list.rb
rubygems-update-3.0.1 util/generate_spdx_license_list.rb
rubygems-update-3.0.0 util/generate_spdx_license_list.rb
rubygems-update-2.7.8 util/generate_spdx_license_list.rb