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
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 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
# 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
# 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
# File lib/licensee/license.rb, line 36 def license_dir dir = File.dirname(__FILE__) File.expand_path '../../vendor/choosealicense.com/_licenses', dir end
# File lib/licensee/license.rb, line 41 def license_files @license_files ||= Dir.glob("#{license_dir}/*.txt") end
# File lib/licensee/license.rb, line 70 def initialize(key) @key = key.downcase end
Private Class Methods
# File lib/licensee/license.rb, line 47 def licenses @licenses ||= keys.map { |key| new(key) } end
Public Instance Methods
# File lib/licensee/license.rb, line 128 def ==(other) !other.nil? && key == other.key end
The license body (e.g., contents - frontmatter)
# File lib/licensee/license.rb, line 117 def content @content ||= parts[2] if parts && parts[2] end
# File lib/licensee/license.rb, line 104 def featured? return YAML_DEFAULTS['featured'] unless meta meta['featured'] end
License metadata from YAML front matter
# File lib/licensee/license.rb, line 80 def meta @meta ||= if parts && parts[1] meta = if YAML.respond_to? :safe_load YAML.safe_load(parts[1]) else YAML.load(parts[1]) end YAML_DEFAULTS.merge(meta) end end
Returns the human-readable license name
# File lib/licensee/license.rb, line 92 def name meta.nil? ? key.capitalize : meta['title'] end
# File lib/licensee/license.rb, line 100 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end
# File lib/licensee/license.rb, line 96 def nickname meta['nickname'] if meta end
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
# File lib/licensee/license.rb, line 124 def url URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s end
Private Instance Methods
# File lib/licensee/license.rb, line 148 def parts return unless raw_content @parts ||= raw_content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a end
# File lib/licensee/license.rb, line 134 def pseudo_license? PSEUDO_LICENSES.include?(key) end
Raw content of license file, including YAML front matter
# File lib/licensee/license.rb, line 139 def raw_content return if pseudo_license? @raw_content ||= if File.exist?(path) File.read(path, encoding: 'utf-8') else raise Licensee::InvalidLicense, "'#{key}' is not a valid license key" end end