Class: Licensee::License

Inherits:
Object
  • Object
show all
Defined in:
lib/licensee/license.rb

Constant Summary collapse

YAML_DEFAULTS =
{
  "featured" => false,
  "hidden" => false,
  "variant" => false
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ License

Returns a new instance of License.



17
18
19
# File 'lib/licensee/license.rb', line 17

def initialize(key)
  @key=key.downcase
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/licensee/license.rb', line 9

def key
  @key
end

Class Method Details

.allObject



5
6
7
# File 'lib/licensee/license.rb', line 5

def self.all
  Licensee::licenses
end

Instance Method Details

#bodyObject Also known as: to_s, text

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



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

def body
  @body ||= parts[2] if parts && parts[2]
end

#body_normalizedObject

License body with all whitespace replaced with a single space



67
68
69
# File 'lib/licensee/license.rb', line 67

def body_normalized
  @content_normalized ||= body.to_s.downcase.gsub(/\s+/, " ").strip
end

#contentObject

Raw content of license file, including YAML front matter



27
28
29
30
31
32
33
34
35
# File 'lib/licensee/license.rb', line 27

def content
  @content ||= if File.exists?(path)
    File.open(path).read
  elsif key == "other" # A pseudo-license with no content
    nil
  else
    raise Licensee::InvalidLicense, "'#{key}' is not a valid license key"
  end
end

#featured?Boolean Also known as: featured

Returns:

  • (Boolean)


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

def featured?
  !!(meta["featured"] if meta)
end

#hashsigObject

Git-computed hash signature for the license file



72
73
74
75
76
77
# File 'lib/licensee/license.rb', line 72

def hashsig
  @hashsig ||= Rugged::Blob::HashSignature.new(
    body, Rugged::Blob::HashSignature::WHITESPACE_SMART)
rescue Rugged::InvalidError
  nil
end

#inspectObject



79
80
81
# File 'lib/licensee/license.rb', line 79

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

#metaObject

License metadata from YAML front matter



38
39
40
41
42
43
44
45
46
47
# File 'lib/licensee/license.rb', line 38

def meta
  @meta ||= if parts && parts[1]
    if YAML.respond_to? :safe_load
      meta = YAML.safe_load(parts[1])
    else
      meta = YAML.load(parts[1])
    end
    YAML_DEFAULTS.merge(meta)
  end
end

#nameObject

Returns the human-readable license name



50
51
52
# File 'lib/licensee/license.rb', line 50

def name
  meta.nil? ? key.capitalize : meta["title"]
end

#pathObject

Path to vendored license file on disk



22
23
24
# File 'lib/licensee/license.rb', line 22

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

#urlObject



83
84
85
# File 'lib/licensee/license.rb', line 83

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