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
Public Class Methods
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 20 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) if options[:featured].nil? output else output.select { |l| l.featured? == options[:featured] } end end end
# File lib/licensee/license.rb, line 44 def find(key, options = {}) options = { hidden: true }.merge(options) keys_licenses(options)[key.downcase] end
Given a license title or nickname, fuzzy match the license
# File lib/licensee/license.rb, line 52 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
# File lib/licensee/license.rb, line 38 def keys @keys ||= license_files.map do |license_file| ::File.basename(license_file, '.txt').downcase end + PSEUDO_LICENSES end
# File lib/licensee/license.rb, line 58 def license_dir ::File.expand_path '../../vendor/choosealicense.com/_licenses', __dir__ end
# File lib/licensee/license.rb, line 62 def license_files @license_files ||= Dir.glob("#{license_dir}/*.txt") end
# File lib/licensee/license.rb, line 113 def initialize(key) @key = key.downcase end
# File lib/licensee/license.rb, line 66 def spdx_dir ::File.expand_path '../../vendor/license-list-XML/src', __dir__ end
Private Class Methods
# File lib/licensee/license.rb, line 76 def keys_licenses(options = {}) @keys_licenses[options] ||= all(options).to_h { |l| [l.key, l] } end
# File lib/licensee/license.rb, line 72 def licenses @licenses ||= keys.map { |key| new(key) } end
Public Instance Methods
# File lib/licensee/license.rb, line 226 def ==(other) other.is_a?(self.class) && key == other.key end
The license body (e.g., contents - frontmatter)
# File lib/licensee/license.rb, line 215 def content @content ||= parts[2] if parts && parts[2] end
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 249 def content_for_mustache @content_for_mustache ||= content.gsub(LicenseField::FIELD_REGEX, '{{{\1}}}') end
Is this license a Creative Commons license?
# File lib/licensee/license.rb, line 209 def creative_commons? key.start_with?('cc-') end
Returns an array of strings of substitutable fields in the license body
# File lib/licensee/license.rb, line 243 def fields @fields ||= LicenseField.from_content(content) end
# File lib/licensee/license.rb, line 200 def gpl? key == 'gpl-2.0' || key == 'gpl-3.0' end
# File lib/licensee/license.rb, line 238 def inspect "#<Licensee::License key=#{key}>" end
# File lib/licensee/license.rb, line 204 def lgpl? key == 'lgpl-2.1' || key == 'lgpl-3.0' end
License metadata from YAML front matter with defaults merged in
# File lib/licensee/license.rb, line 123 def meta @meta ||= LicenseMeta.from_yaml(yaml) end
Returns the human-readable license name
# File lib/licensee/license.rb, line 134 def name return key.tr('-', ' ').capitalize if pseudo_license? title || spdx_id end
# File lib/licensee/license.rb, line 140 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end
# File lib/licensee/license.rb, line 196 def other? key == 'other' end
Path to vendored license file on disk
# File lib/licensee/license.rb, line 118 def path @path ||= File.expand_path "#{@key}.txt", Licensee::License.license_dir end
# File lib/licensee/license.rb, line 230 def pseudo_license? PSEUDO_LICENSES.include?(key) end
# File lib/licensee/license.rb, line 234 def rules @rules ||= LicenseRules.from_meta(meta) end
Returns a regex that will match the license source
The following variations are supported (as presumed identical):
-
HTTP or HTTPS
-
www or non-www
-
.txt, .html, .htm, or / suffix
Returns the regex, or nil if no source exists
# File lib/licensee/license.rb, line 185 def source_regex return @source_regex if defined? @source_regex return unless meta.source source = meta.source.dup.sub(/\A#{SOURCE_PREFIX}/o, '') source = source.sub(/#{SOURCE_SUFFIX}\z/o, '') escaped_source = Regexp.escape(source) @source_regex = /#{SOURCE_PREFIX}#{escaped_source}(?:#{SOURCE_SUFFIX})?/i end
# File lib/licensee/license.rb, line 127 def spdx_id return meta.spdx_id if meta.spdx_id return 'NOASSERTION' if key == 'other' return 'NONE' if key == 'no-license' end
# File lib/licensee/license.rb, line 144 def title_regex return @title_regex if defined? @title_regex string = name.downcase.sub('*', 'u') simple_title_regex = Regexp.new string, 'i' 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)?') version_match = string.match(/\d+\\.(\d+)/) if version_match vsub = if version_match[1] == '0' ',?\s+(?:version\ |v(?:\. )?)?\1(\2)?' else ',?\s+(?:version\ |v(?:\. )?)?\1\2' end string = string.sub(/\\ (\d+)(\\.\d+)/, vsub) end 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 = [simple_title_regex, title_regex, key_regex] parts.push Regexp.new meta.nickname.sub(/\bGNU /i, '(?:GNU )?') if meta.nickname @title_regex = Regexp.union parts end
# File lib/licensee/license.rb, line 222 def url URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s end
Private Instance Methods
# File lib/licensee/license.rb, line 263 def parts return unless raw_content @parts ||= raw_content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a end
Raw content of license file, including YAML front matter
# File lib/licensee/license.rb, line 256 def raw_content return if pseudo_license? raise Licensee::InvalidLicense, "'#{key}' is not a valid license key" unless File.exist?(path) @raw_content ||= File.read(path, encoding: 'utf-8') end
# File lib/licensee/license.rb, line 273 def spdx_alt_segments @spdx_alt_segments ||= begin path = File.expand_path "#{spdx_id}.xml", Licensee::License.spdx_dir raw_xml = File.read(path, encoding: 'utf-8') text = raw_xml.match(%r{<text>(.*)</text>}m)[1] text.gsub!(%r{<copyrightText>.*?</copyrightText>}m, '') text.gsub!(%r{<titleText>.*?</titleText>}m, '') text.gsub!(%r{<optional.*?>.*?</optional>}m, '') text.scan(/<alt .*?>/m).size end end
# File lib/licensee/license.rb, line 269 def yaml @yaml ||= parts[1] if parts end