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 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
# File lib/licensee/license.rb, line 41 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 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
# 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
# File lib/licensee/license.rb, line 55 def license_dir dir = ::File.dirname(__FILE__) ::File.expand_path '../../vendor/choosealicense.com/_licenses', dir end
# File lib/licensee/license.rb, line 60 def license_files @license_files ||= Dir.glob("#{license_dir}/*.txt") end
# File lib/licensee/license.rb, line 107 def initialize(key) @key = key.downcase end
Private Class Methods
# File lib/licensee/license.rb, line 70 def keys_licenses(options = {}) @keys_licenses[options] ||= all(options).map { |l| [l.key, l] }.to_h end
# File lib/licensee/license.rb, line 66 def licenses @licenses ||= keys.map { |key| new(key) } end
Public Instance Methods
# File lib/licensee/license.rb, line 213 def ==(other) !other.nil? && key == other.key end
The license body (e.g., contents - frontmatter)
# File lib/licensee/license.rb, line 202 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 236 def content_for_mustache @content_for_mustache ||= begin content.gsub(LicenseField::FIELD_REGEX, '{{{\1}}}') end end
Is this license a Creative Commons license?
# File lib/licensee/license.rb, line 196 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 230 def fields @fields ||= LicenseField.from_content(content) end
# File lib/licensee/license.rb, line 187 def gpl? key == 'gpl-2.0' || key == 'gpl-3.0' end
# File lib/licensee/license.rb, line 225 def inspect "#<Licensee::License key=#{key}>" end
# File lib/licensee/license.rb, line 191 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 117 def meta @meta ||= LicenseMeta.from_yaml(yaml) end
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
# File lib/licensee/license.rb, line 134 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end
# File lib/licensee/license.rb, line 183 def other? key == 'other' end
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
# File lib/licensee/license.rb, line 217 def pseudo_license? PSEUDO_LICENSES.include?(key) end
# File lib/licensee/license.rb, line 221 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 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
# 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
# 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
# File lib/licensee/license.rb, line 209 def url URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s end
Private Instance Methods
# 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 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
# File lib/licensee/license.rb, line 260 def yaml @yaml ||= parts[1] if parts end