Sha256: 63500c3593c2cc4fa933d6f9546289d7cd073c6543eac1cb27ac3d850c1137f5

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

class Licensee
  class InvalidLicense < ArgumentError; end
  class License

    def self.all
      Licensee::licenses
    end

    attr_reader :key

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

    # Path to vendored license file on disk
    def path
      @path ||= File.expand_path "#{@key}.txt", Licensee::Licenses.base
    end

    # Raw content of license file, including YAML front matter
    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

    # License metadata from YAML front matter
    def meta
      @meta ||= YAML.load(parts[1]) if parts && parts[1]
    end

    # Returns the human-readable license name
    def name
      meta.nil? ? key.capitalize : meta["title"]
    end

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

    # The license body (e.g., contents - frontmatter)
    def body
      @body ||= parts[2] if parts && parts[2]
    end
    alias_method :to_s, :body
    alias_method :text, :body

    # License body with all whitespace replaced with a single space
    def body_normalized
      @content_normalized ||= body.to_s.downcase.gsub(/\s+/, " ").strip
    end

    # Git-computed hash signature for the license file
    def hashsig
      @hashsig ||= Rugged::Blob::HashSignature.new(
        body, Rugged::Blob::HashSignature::WHITESPACE_SMART)
    rescue Rugged::InvalidError
      nil
    end

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

    def url
      URI.join(Licensee::DOMAIN, meta["permalink"]).to_s
    end

    private

    def parts
      @parts ||= content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a if content
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
licensee-4.7.0 lib/licensee/license.rb