class Licensee::License

Constants

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

YAML_DEFAULTS

These should be in sync with choosealicense.com's collection defaults

Attributes

key[R]

Public Class Methods

[](key, options = {})
Alias for: find
all(options = {}) click to toggle source

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 15
def all(options = {})
  options = { hidden: false, featured: nil }.merge(options)
  output = licenses.dup
  output.reject!(&:hidden?) unless options[:hidden]
  return output if options[:featured].nil?
  output.select { |l| l.featured? == options[:featured] }
end
find(key, options = {}) click to toggle source
# File lib/licensee/license.rb, line 29
def find(key, options = {})
  options = { hidden: true }.merge(options)
  all(options).find { |license| key.casecmp(license.key).zero? }
end
Also aliased as: [], find_by_key
find_by_key(key, options = {})
Alias for: find
keys() click to toggle source
# File lib/licensee/license.rb, line 23
def keys
  @keys ||= license_files.map do |license_file|
    File.basename(license_file, '.txt').downcase
  end + PSEUDO_LICENSES
end
license_dir() click to toggle source
# File lib/licensee/license.rb, line 36
def license_dir
  dir = File.dirname(__FILE__)
  File.expand_path '../../vendor/choosealicense.com/_licenses', dir
end
license_files() click to toggle source
# File lib/licensee/license.rb, line 41
def license_files
  @license_files ||= Dir.glob("#{license_dir}/*.txt")
end
new(key) click to toggle source
# File lib/licensee/license.rb, line 70
def initialize(key)
  @key = key.downcase
end

Private Class Methods

licenses() click to toggle source
# File lib/licensee/license.rb, line 47
def licenses
  @licenses ||= keys.map { |key| new(key) }
end

Public Instance Methods

==(other) click to toggle source
# File lib/licensee/license.rb, line 131
def ==(other)
  !other.nil? && key == other.key
end
body()
Alias for: content
content() click to toggle source

The license body (e.g., contents - frontmatter)

# File lib/licensee/license.rb, line 120
def content
  @content ||= parts[2] if parts && parts[2]
end
Also aliased as: to_s, text, body
creative_commons?() click to toggle source

Is this license a Creative Commons license?

# File lib/licensee/license.rb, line 115
def creative_commons?
  key.start_with?('cc-')
end
gpl?() click to toggle source
# File lib/licensee/license.rb, line 110
def gpl?
  key == 'gpl-2.0' || key == 'gpl-3.0'
end
hidden?() click to toggle source
# File lib/licensee/license.rb, line 106
def hidden?
  meta['hidden']
end
inspect() click to toggle source
# File lib/licensee/license.rb, line 154
def inspect
  "#<Licensee::License key=#{key}>"
end
meta() click to toggle source

License metadata from YAML front matter with defaults merged in

# File lib/licensee/license.rb, line 80
def meta
  @meta ||= begin
    return YAML_DEFAULTS unless parts && parts[1]
    meta = YAML.safe_load(parts[1])
    YAML_DEFAULTS.merge(meta)
  end
end
name() click to toggle source

Returns the human-readable license name

# File lib/licensee/license.rb, line 89
def name
  meta['title'] ? meta['title'] : key.capitalize
end
name_without_version() click to toggle source
# File lib/licensee/license.rb, line 97
def name_without_version
  /(.+?)(( v?\d\.\d)|$)/.match(name)[1]
end
nickname() click to toggle source
# File lib/licensee/license.rb, line 93
def nickname
  meta['nickname']
end
path() click to toggle source

Path to vendored license file on disk

# File lib/licensee/license.rb, line 75
def path
  @path ||= File.expand_path "#{@key}.txt", Licensee::License.license_dir
end
pseudo_license?() click to toggle source
# File lib/licensee/license.rb, line 135
def pseudo_license?
  PSEUDO_LICENSES.include?(key)
end
rules() click to toggle source

Returns a hash in the form of rule_group => rules describing what you legally can and can't do with the given license

# File lib/licensee/license.rb, line 141
def rules
  return @rules if defined? @rules
  @rules = {}

  Rule.groups.each do |group|
    @rules[group] = meta[group].map do |tag|
      Rule.find_by_tag_and_group(tag, group)
    end
  end

  @rules
end
text()
Alias for: content
to_s()
Alias for: content
url() click to toggle source
# File lib/licensee/license.rb, line 127
def url
  URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s
end

Private Instance Methods

parts() click to toggle source
# File lib/licensee/license.rb, line 169
def parts
  return unless raw_content
  @parts ||= raw_content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a
end
raw_content() click to toggle source

Raw content of license file, including YAML front matter

# File lib/licensee/license.rb, line 161
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