Module: Licensee::License::IdentityMethods

Included in:
Licensee::License
Defined in:
lib/licensee/license/identity_methods.rb

Overview

Instance methods for license identity (SPDX ID, name, title matching).

Constant Summary collapse

SOURCE_PREFIX =
%r{https?://(?:www\.)?}i
SOURCE_SUFFIX =
%r{(?:\.html?|\.txt|/)(?:\?[^\s]*)?}i

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



90
91
92
# File 'lib/licensee/license/identity_methods.rb', line 90

def ==(other)
  other.is_a?(self.class) && key == other.key
end

#creative_commons?Boolean Also known as: cc?

Is this license a Creative Commons license?

Returns:

  • (Boolean)


107
108
109
# File 'lib/licensee/license/identity_methods.rb', line 107

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

#gpl?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/licensee/license/identity_methods.rb', line 98

def gpl?
  ['gpl-2.0', 'gpl-3.0'].include?(key)
end

#key_title_regexObject



65
66
67
68
# File 'lib/licensee/license/identity_methods.rb', line 65

def key_title_regex
  s = key.sub('-', '[- ]').sub('.', '\\.')
  Regexp.new("#{s}(?:\\ licen[sc]e)?", 'i')
end

#lgpl?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/licensee/license/identity_methods.rb', line 102

def lgpl?
  ['lgpl-2.1', 'lgpl-3.0'].include?(key)
end

#nameObject

Returns the human-readable license name



18
19
20
21
22
# File 'lib/licensee/license/identity_methods.rb', line 18

def name
  return key.tr('-', ' ').capitalize if pseudo_license?

  title || spdx_id
end

#name_without_versionObject



24
25
26
27
# File 'lib/licensee/license/identity_methods.rb', line 24

def name_without_version
  match = name.match(/\s+v?\d\.\d/i)
  match ? name[0, match.begin(0)] : name
end

#nickname_title_regexObject



70
71
72
# File 'lib/licensee/license/identity_methods.rb', line 70

def nickname_title_regex
  Regexp.new(meta.nickname.sub(/\bGNU /i, '(?:GNU )?'))
end

#normalize_version_capture(string) ⇒ Object



54
55
56
57
58
59
# File 'lib/licensee/license/identity_methods.rb', line 54

def normalize_version_capture(string)
  match = string.match(/\d++\\.(\d+)/)
  return string unless match

  string.sub(/\\ (\d++)(\\.\d+)/, version_substitution(match[1]))
end

#normalized_title_regexObject



45
46
47
48
49
50
51
52
# File 'lib/licensee/license/identity_methods.rb', line 45

def normalized_title_regex
  string = name.downcase.tr('*', 'u')
  string = string.sub(/\Athe /i, '')
  string = string.sub(/,? version /, ' ').sub(/v(\d+\.\d+)/, '\\1')
  string = Regexp.escape(string).sub(/\\ licen[sc]e/i, '(?:\\ licen[sc]e)?')
  string = normalize_version_capture(string)
  Regexp.new(string.sub(/\bgnu\\ /, '(?:GNU )?'), 'i')
end

#other?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/licensee/license/identity_methods.rb', line 94

def other?
  key == 'other'
end

#pseudo_license?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/licensee/license/identity_methods.rb', line 112

def pseudo_license?
  PSEUDO_LICENSES.include?(key)
end

#simple_title_regexObject



41
42
43
# File 'lib/licensee/license/identity_methods.rb', line 41

def simple_title_regex
  Regexp.new(name.downcase.tr('*', 'u'), 'i')
end

#source_regexObject

Returns a regex that will match the license source



75
76
77
78
79
80
81
82
83
84
# File 'lib/licensee/license/identity_methods.rb', line 75

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

#spdx_idObject



10
11
12
13
14
15
# File 'lib/licensee/license/identity_methods.rb', line 10

def spdx_id
  return meta.spdx_id if meta.spdx_id
  return 'NOASSERTION' if key == 'other'

  'NONE' if key == 'no-license'
end

#title_regexObject



29
30
31
32
33
# File 'lib/licensee/license/identity_methods.rb', line 29

def title_regex
  return @title_regex if defined?(@title_regex)

  @title_regex = Regexp.union(title_regex_parts)
end

#title_regex_partsObject



35
36
37
38
39
# File 'lib/licensee/license/identity_methods.rb', line 35

def title_regex_parts
  parts = [simple_title_regex, normalized_title_regex, key_title_regex]
  parts << nickname_title_regex if meta.nickname
  parts
end

#urlObject



86
87
88
# File 'lib/licensee/license/identity_methods.rb', line 86

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

#version_substitution(minor) ⇒ Object



61
62
63
# File 'lib/licensee/license/identity_methods.rb', line 61

def version_substitution(minor)
  minor == '0' ? ',?\s+(?:version\ |v(?:\. )?)?\\1(\\2)?' : ',?\s+(?:version\ |v(?:\. )?)?\\1\\2'
end