class Licensee::License

Constants

DEFAULT_OPTIONS

Default options to use when retrieving licenses via all

HASH_METHODS
PSEUDO_LICENSES

Pseudo-license are license placeholders with no content

`other` - The project had a license, but we were not able to detect it `no-license` - The project is not licensed (e.g., all rights reserved)

Note: A lack of detected license will be a nil license

SOURCE_PREFIX
SOURCE_SUFFIX
YAML_DEFAULTS

Preserved for backwards compatibility

Attributes

key[R]

Public Class Methods

[](key, options = {})
Alias for: find
all(options = {}) click to toggle source

All license objects defined via Licensee (via choosealicense.com)

Options:

  • :hidden - boolean, return hidden licenses (default: false)

  • :featured - boolean, return only (non)featured licenses (default: all)

Returns an Array of License objects.

# File lib/licensee/license.rb, line 19
def all(options = {})
  @all[options] ||= begin
    # TODO: Remove in next major version to avoid breaking change
    options[:pseudo] ||= options[:psuedo] unless options[:psuedo].nil?

    options = DEFAULT_OPTIONS.merge(options)
    output = licenses.dup
    output.reject!(&:hidden?) unless options[:hidden]
    output.reject!(&:pseudo_license?) unless options[:pseudo]
    output.sort_by!(&:key)
    return output if options[:featured].nil?

    output.select { |l| l.featured? == options[:featured] }
  end
end
find(key, options = {}) click to toggle source
# File lib/licensee/license.rb, line 41
def find(key, options = {})
  options = { hidden: true }.merge(options)
  keys_licenses(options)[key.downcase]
end
Also aliased as: [], find_by_key
find_by_key(key, options = {})
Alias for: find
find_by_title(title) click to toggle source

Given a license title or nickname, fuzzy match the license

# File lib/licensee/license.rb, line 49
def find_by_title(title)
  License.all(hidden: true, pseudo: false).find do |license|
    title =~ /\A(the )?#{license.title_regex}( license)?\z/i
  end
end
keys() click to toggle source
# File lib/licensee/license.rb, line 35
def keys
  @keys ||= license_files.map do |license_file|
    ::File.basename(license_file, '.txt').downcase
  end + PSEUDO_LICENSES
end
license_dir() click to toggle source
# File lib/licensee/license.rb, line 55
def license_dir
  dir = ::File.dirname(__FILE__)
  ::File.expand_path '../../vendor/choosealicense.com/_licenses', dir
end
license_files() click to toggle source
# File lib/licensee/license.rb, line 60
def license_files
  @license_files ||= Dir.glob("#{license_dir}/*.txt")
end
new(key) click to toggle source
# File lib/licensee/license.rb, line 107
def initialize(key)
  @key = key.downcase
end

Private Class Methods

keys_licenses(options = {}) click to toggle source
# File lib/licensee/license.rb, line 70
def keys_licenses(options = {})
  @keys_licenses[options] ||= all(options).map { |l| [l.key, l] }.to_h
end
licenses() click to toggle source
# File lib/licensee/license.rb, line 66
def licenses
  @licenses ||= keys.map { |key| new(key) }
end

Public Instance Methods

==(other) click to toggle source
# File lib/licensee/license.rb, line 213
def ==(other)
  !other.nil? && key == other.key
end
body()
Alias for: content
cc?()
Alias for: creative_commons?
content() click to toggle source

The license body (e.g., contents - frontmatter)

# File lib/licensee/license.rb, line 202
def content
  @content ||= parts[2] if parts && parts[2]
end
Also aliased as: to_s, text, body
content_for_mustache() click to toggle source

Returns a string with `[fields]` replaced by `{{{fields}}}` Does not mangle non-supported fields in the form of `[field]`

# File lib/licensee/license.rb, line 236
def content_for_mustache
  @content_for_mustache ||= begin
    content.gsub(LicenseField::FIELD_REGEX, '{{{\1}}}')
  end
end
creative_commons?() click to toggle source

Is this license a Creative Commons license?

# File lib/licensee/license.rb, line 196
def creative_commons?
  key.start_with?('cc-')
end
Also aliased as: cc?
fields() click to toggle source

Returns an array of strings of substitutable fields in the license body

# File lib/licensee/license.rb, line 230
def fields
  @fields ||= LicenseField.from_content(content)
end
gpl?() click to toggle source
# File lib/licensee/license.rb, line 187
def gpl?
  key == 'gpl-2.0' || key == 'gpl-3.0'
end
inspect() click to toggle source
# File lib/licensee/license.rb, line 225
def inspect
  "#<Licensee::License key=#{key}>"
end
lgpl?() click to toggle source
# File lib/licensee/license.rb, line 191
def lgpl?
  key == 'lgpl-2.1' || key == 'lgpl-3.0'
end
meta() click to toggle source

License metadata from YAML front matter with defaults merged in

# File lib/licensee/license.rb, line 117
def meta
  @meta ||= LicenseMeta.from_yaml(yaml)
end
name() click to toggle source

Returns the human-readable license name

# File lib/licensee/license.rb, line 128
def name
  return key.tr('-', ' ').capitalize if pseudo_license?

  title || spdx_id
end
name_without_version() click to toggle source
# File lib/licensee/license.rb, line 134
def name_without_version
  /(.+?)(( v?\d\.\d)|$)/.match(name)[1]
end
other?() click to toggle source
# File lib/licensee/license.rb, line 183
def other?
  key == 'other'
end
path() click to toggle source

Path to vendored license file on disk

# File lib/licensee/license.rb, line 112
def path
  @path ||= File.expand_path "#{@key}.txt", Licensee::License.license_dir
end
pseudo_license?() click to toggle source
# File lib/licensee/license.rb, line 217
def pseudo_license?
  PSEUDO_LICENSES.include?(key)
end
rules() click to toggle source
# File lib/licensee/license.rb, line 221
def rules
  @rules ||= LicenseRules.from_meta(meta)
end
source_regex() click to toggle source

Returns a regex that will match the license source

The following variations are supported (as presumed identical):

  1. HTTP or HTTPS

  2. www or non-www

  3. .txt, .html, .htm, or / suffix

Returns the regex, or nil if no source exists

# File lib/licensee/license.rb, line 172
def source_regex
  return @source_regex if defined? @source_regex
  return unless meta.source

  source = meta.source.dup.sub(/\A#{SOURCE_PREFIX}/, '')
  source = source.sub(/#{SOURCE_SUFFIX}\z/, '')

  escaped_source = Regexp.escape(source)
  @source_regex = /#{SOURCE_PREFIX}#{escaped_source}(?:#{SOURCE_SUFFIX})?/i
end
spdx_id() click to toggle source
# File lib/licensee/license.rb, line 121
def spdx_id
  return meta.spdx_id if meta.spdx_id
  return 'NOASSERTION' if key == 'other'
  return 'NONE' if key == 'no-license'
end
text()
Alias for: content
title_regex() click to toggle source
# File lib/licensee/license.rb, line 138
def title_regex
  return @title_regex if defined? @title_regex

  string = name.downcase.sub('*', 'u')
  string.sub!(/\Athe /i, '')
  string.sub!(/,? version /, ' ')
  string.sub!(/v(\d+\.\d+)/, '\1')
  string = Regexp.escape(string)
  string = string.sub(/\\ licen[sc]e/i, '(?:\ licen[sc]e)?')
  string = string.sub(/\\ (\d+\\.\d+)/, ',?\s+(?:version\ |v(?:\. )?)?\1')
  string = string.sub(/\bgnu\\ /, '(?:GNU )?')
  title_regex = Regexp.new string, 'i'

  string = key.sub('-', '[- ]')
  string.sub!('.', '\.')
  string << '(?:\ licen[sc]e)?'
  key_regex = Regexp.new string, 'i'

  parts = [title_regex, key_regex]
  if meta.nickname
    parts.push Regexp.new meta.nickname.sub(/\bGNU /i, '(?:GNU )?')
  end

  @title_regex = Regexp.union parts
end
to_s()
Alias for: content
url() click to toggle source
# File lib/licensee/license.rb, line 209
def url
  URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s
end

Private Instance Methods

parts() click to toggle source
# File lib/licensee/license.rb, line 254
def parts
  return unless raw_content

  @parts ||= raw_content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a
end
raw_content() click to toggle source

Raw content of license file, including YAML front matter

# File lib/licensee/license.rb, line 245
def raw_content
  return if pseudo_license?
  unless File.exist?(path)
    raise Licensee::InvalidLicense, "'#{key}' is not a valid license key"
  end

  @raw_content ||= File.read(path, encoding: 'utf-8')
end
yaml() click to toggle source
# File lib/licensee/license.rb, line 260
def yaml
  @yaml ||= parts[1] if parts
end