Sha256: 72702f2ae5aaa49a29e08cdb8d6fc2da47115bbcc204216f6805507da4eb0c69

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Spandx
  class Catalogue
    include Enumerable

    def initialize(catalogue = {})
      @catalogue = catalogue
    end

    def [](id)
      identity_map[id]
    end

    def version
      catalogue[:licenseListVersion]
    end

    def each
      licenses.each do |license|
        yield license
      end
    end

    class << self
      def latest(gateway: ::Spandx::Gateways::Spdx.new)
        new(gateway.fetch)
      end

      def from_json(json)
        new(JSON.parse(json, symbolize_names: true))
      end

      def from_file(path)
        from_json(IO.read(path))
      end

      def from_git
        from_json(Spandx.db.read('json/licenses.json'))
      end

      def empty
        @empty ||= new(licenses: [])
      end
    end

    private

    attr_reader :catalogue

    def licenses
      @licenses ||= identity_map.values.sort
    end

    def present?(item)
      item && !item.empty?
    end

    def identity_map
      @identity_map ||=
        catalogue.fetch(:licenses, []).each_with_object({}) do |hash, memo|
          license = License.new(hash)
          memo[license.id] = license if present?(license.id)
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spandx-0.4.1 lib/spandx/catalogue.rb
spandx-0.4.0 lib/spandx/catalogue.rb
spandx-0.3.0 lib/spandx/catalogue.rb
spandx-0.2.0 lib/spandx/catalogue.rb
spandx-0.1.7 lib/spandx/catalogue.rb
spandx-0.1.6 lib/spandx/catalogue.rb