Class: Licensee::License
- Inherits:
-
Object
- Object
- Licensee::License
- Extended by:
- Forwardable
- Includes:
- ContentHelper, HashHelper
- Defined in:
- lib/licensee/license.rb
Constant Summary collapse
- YAML_DEFAULTS =
Preserved for backwards compatibility
Licensee::LicenseMeta.members
- 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
%w[other no-license].freeze
- DEFAULT_OPTIONS =
Default options to use when retrieving licenses via #all
{ hidden: false, featured: nil, pseudo: true }.freeze
- ALT_TITLE_REGEX =
{ 'bsd-2-clause' => /bsd 2-clause(?: \"simplified\")?/i, 'bsd-3-clause' => /bsd 3-clause(?: \"new\" or \"revised\")?/i, 'bsd-3-clause-clear' => /(?:clear bsd|bsd 3-clause(?: clear)?)/i }.freeze
- SOURCE_PREFIX =
%r{https?://(?:www\.)?}i.freeze
- SOURCE_SUFFIX =
%r{(?:\.html?|\.txt|\/)(?:\?[^\s]*)?}i.freeze
- HASH_METHODS =
%i[ key spdx_id meta url rules fields other? gpl? lgpl? cc? ].freeze
Constants included from ContentHelper
ContentHelper::DIGEST, ContentHelper::END_OF_TERMS_REGEX, ContentHelper::NORMALIZATIONS, ContentHelper::REGEXES, ContentHelper::START_REGEX, ContentHelper::STRIP_METHODS, ContentHelper::VARIETAL_WORDS
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
All license objects defined via Licensee (via choosealicense.com).
- .find(key, options = {}) ⇒ Object (also: [], find_by_key)
-
.find_by_title(title) ⇒ Object
Given a license title or nickname, fuzzy match the license.
- .keys ⇒ Object
- .license_dir ⇒ Object
- .license_files ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#content ⇒ Object
(also: #to_s, #text, #body)
The license body (e.g., contents - frontmatter).
-
#content_for_mustache ⇒ Object
Returns a string with ‘[fields]` replaced by `{{fields}}` Does not mangle non-supported fields in the form of `[field]`.
-
#creative_commons? ⇒ Boolean
(also: #cc?)
Is this license a Creative Commons license?.
-
#fields ⇒ Object
Returns an array of strings of substitutable fields in the license body.
- #gpl? ⇒ Boolean
-
#initialize(key) ⇒ License
constructor
A new instance of License.
- #inspect ⇒ Object
- #lgpl? ⇒ Boolean
-
#meta ⇒ Object
License metadata from YAML front matter with defaults merged in.
-
#name ⇒ Object
Returns the human-readable license name.
- #name_without_version ⇒ Object
- #other? ⇒ Boolean
-
#path ⇒ Object
Path to vendored license file on disk.
- #pseudo_license? ⇒ Boolean
- #rules ⇒ Object
-
#source_regex ⇒ Object
Returns a regex that will match the license source.
- #spdx_id ⇒ Object
- #title_regex ⇒ Object
- #url ⇒ Object
Methods included from HashHelper
Methods included from ContentHelper
const_missing, #content_hash, #content_normalized, #content_without_title_and_version, format_percent, #length, #length_delta, #max_delta, #similarity, title_regex, #wordset, wrap
Constructor Details
#initialize(key) ⇒ License
Returns a new instance of License.
111 112 113 |
# File 'lib/licensee/license.rb', line 111 def initialize(key) @key = key.downcase end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
73 74 75 |
# File 'lib/licensee/license.rb', line 73 def key @key end |
Class Method Details
.all(options = {}) ⇒ Object
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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/licensee/license.rb', line 17 def all( = {}) @all[] ||= begin # TODO: Remove in next major version to avoid breaking change [:pseudo] ||= [:psuedo] unless [:psuedo].nil? = DEFAULT_OPTIONS.merge() output = licenses.dup output.reject!(&:hidden?) unless [:hidden] output.reject!(&:pseudo_license?) unless [:pseudo] output.sort_by!(&:key) return output if [:featured].nil? output.select { |l| l.featured? == [:featured] } end end |
.find(key, options = {}) ⇒ Object Also known as: [], find_by_key
39 40 41 42 |
# File 'lib/licensee/license.rb', line 39 def find(key, = {}) = { hidden: true }.merge() keys_licenses()[key.downcase] end |
.find_by_title(title) ⇒ Object
Given a license title or nickname, fuzzy match the license
47 48 49 50 51 |
# File 'lib/licensee/license.rb', line 47 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 ⇒ Object
33 34 35 36 37 |
# File 'lib/licensee/license.rb', line 33 def keys @keys ||= license_files.map do |license_file| ::File.basename(license_file, '.txt').downcase end + PSEUDO_LICENSES end |
.license_dir ⇒ Object
53 54 55 56 |
# File 'lib/licensee/license.rb', line 53 def license_dir dir = ::File.dirname(__FILE__) ::File. '../../vendor/choosealicense.com/_licenses', dir end |
.license_files ⇒ Object
58 59 60 |
# File 'lib/licensee/license.rb', line 58 def license_files @license_files ||= Dir.glob("#{license_dir}/*.txt") end |
Instance Method Details
#==(other) ⇒ Object
215 216 217 |
# File 'lib/licensee/license.rb', line 215 def ==(other) !other.nil? && key == other.key end |
#content ⇒ Object Also known as: to_s, text, body
The license body (e.g., contents - frontmatter)
204 205 206 |
# File 'lib/licensee/license.rb', line 204 def content @content ||= parts[2] if parts && parts[2] end |
#content_for_mustache ⇒ Object
Returns a string with ‘[fields]` replaced by `{{fields}}` Does not mangle non-supported fields in the form of `[field]`
238 239 240 241 242 |
# File 'lib/licensee/license.rb', line 238 def content_for_mustache @content_for_mustache ||= begin content.gsub(LicenseField::FIELD_REGEX, '{{{\1}}}') end end |
#creative_commons? ⇒ Boolean Also known as: cc?
Is this license a Creative Commons license?
198 199 200 |
# File 'lib/licensee/license.rb', line 198 def creative_commons? key.start_with?('cc-') end |
#fields ⇒ Object
Returns an array of strings of substitutable fields in the license body
232 233 234 |
# File 'lib/licensee/license.rb', line 232 def fields @fields ||= LicenseField.from_content(content) end |
#gpl? ⇒ Boolean
189 190 191 |
# File 'lib/licensee/license.rb', line 189 def gpl? key == 'gpl-2.0' || key == 'gpl-3.0' end |
#inspect ⇒ Object
227 228 229 |
# File 'lib/licensee/license.rb', line 227 def inspect "#<Licensee::License key=#{key}>" end |
#lgpl? ⇒ Boolean
193 194 195 |
# File 'lib/licensee/license.rb', line 193 def lgpl? key == 'lgpl-2.1' || key == 'lgpl-3.0' end |
#meta ⇒ Object
License metadata from YAML front matter with defaults merged in
121 122 123 |
# File 'lib/licensee/license.rb', line 121 def @meta ||= LicenseMeta.from_yaml(yaml) end |
#name ⇒ Object
Returns the human-readable license name
132 133 134 135 136 |
# File 'lib/licensee/license.rb', line 132 def name return key.tr('-', ' ').capitalize if pseudo_license? title || spdx_id end |
#name_without_version ⇒ Object
138 139 140 |
# File 'lib/licensee/license.rb', line 138 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end |
#other? ⇒ Boolean
185 186 187 |
# File 'lib/licensee/license.rb', line 185 def other? key == 'other' end |
#path ⇒ Object
Path to vendored license file on disk
116 117 118 |
# File 'lib/licensee/license.rb', line 116 def path @path ||= File. "#{@key}.txt", Licensee::License.license_dir end |
#pseudo_license? ⇒ Boolean
219 220 221 |
# File 'lib/licensee/license.rb', line 219 def pseudo_license? PSEUDO_LICENSES.include?(key) end |
#rules ⇒ Object
223 224 225 |
# File 'lib/licensee/license.rb', line 223 def rules @rules ||= LicenseRules.() end |
#source_regex ⇒ Object
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
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/licensee/license.rb', line 174 def source_regex return @source_regex if defined? @source_regex return unless .source source = .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 ⇒ Object
125 126 127 128 129 |
# File 'lib/licensee/license.rb', line 125 def spdx_id return .spdx_id if .spdx_id return 'NOASSERTION' if key == 'other' return 'NONE' if key == 'no-license' end |
#title_regex ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/licensee/license.rb', line 142 def title_regex return @title_regex if defined? @title_regex title_regex = ALT_TITLE_REGEX[key] title_regex ||= begin 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 )?') Regexp.new string, 'i' end parts = [title_regex, key] if .nickname parts.push Regexp.new .nickname.sub(/\bGNU /i, '(?:GNU )?') end @title_regex = Regexp.union parts end |