Class: Licensee::License

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashHelper

#to_h

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

#keyObject (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(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 = {}) ⇒ Object Also known as: [], find_by_key



39
40
41
42
# File 'lib/licensee/license.rb', line 39

def find(key, options = {})
  options = { hidden: true }.merge(options)
  keys_licenses(options)[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

.keysObject



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_dirObject



53
54
55
56
# File 'lib/licensee/license.rb', line 53

def license_dir
  dir = ::File.dirname(__FILE__)
  ::File.expand_path '../../vendor/choosealicense.com/_licenses', dir
end

.license_filesObject



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

#contentObject 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_mustacheObject

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?

Returns:

  • (Boolean)


198
199
200
# File 'lib/licensee/license.rb', line 198

def creative_commons?
  key.start_with?('cc-')
end

#fieldsObject

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

Returns:

  • (Boolean)


189
190
191
# File 'lib/licensee/license.rb', line 189

def gpl?
  key == 'gpl-2.0' || key == 'gpl-3.0'
end

#inspectObject



227
228
229
# File 'lib/licensee/license.rb', line 227

def inspect
  "#<Licensee::License key=#{key}>"
end

#lgpl?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/licensee/license.rb', line 193

def lgpl?
  key == 'lgpl-2.1' || key == 'lgpl-3.0'
end

#metaObject

License metadata from YAML front matter with defaults merged in



121
122
123
# File 'lib/licensee/license.rb', line 121

def meta
  @meta ||= LicenseMeta.from_yaml(yaml)
end

#nameObject

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_versionObject



138
139
140
# File 'lib/licensee/license.rb', line 138

def name_without_version
  /(.+?)(( v?\d\.\d)|$)/.match(name)[1]
end

#other?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/licensee/license.rb', line 185

def other?
  key == 'other'
end

#pathObject

Path to vendored license file on disk



116
117
118
# File 'lib/licensee/license.rb', line 116

def path
  @path ||= File.expand_path "#{@key}.txt", Licensee::License.license_dir
end

#pseudo_license?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/licensee/license.rb', line 219

def pseudo_license?
  PSEUDO_LICENSES.include?(key)
end

#rulesObject



223
224
225
# File 'lib/licensee/license.rb', line 223

def rules
  @rules ||= LicenseRules.from_meta(meta)
end

#source_regexObject

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



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 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_idObject



125
126
127
128
129
# File 'lib/licensee/license.rb', line 125

def spdx_id
  return meta.spdx_id if meta.spdx_id
  return 'NOASSERTION' if key == 'other'
  return 'NONE' if key == 'no-license'
end

#title_regexObject



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 meta.nickname
    parts.push Regexp.new meta.nickname.sub(/\bGNU /i, '(?:GNU )?')
  end

  @title_regex = Regexp.union parts
end

#urlObject



211
212
213
# File 'lib/licensee/license.rb', line 211

def url
  URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s
end