Sha256: 17b9444a951d645168a0950f53d1f22f14247b217eea58abc0490c69c99275fe

Contents?: true

Size: 1.36 KB

Versions: 11

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Spandx
  module Spdx
    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::Spdx::Gateway.new)
          new(gateway.fetch)
        end

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

        def from_file(path)
          from_json(Pathname.new(path).read)
        end

        def from_git
          from_json(Spandx.git[:spdx].read('json/licenses.json'))
        end

        def default
          from_git
        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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spandx-0.18.2 lib/spandx/spdx/catalogue.rb
spandx-0.18.1 lib/spandx/spdx/catalogue.rb
spandx-0.18.0 lib/spandx/spdx/catalogue.rb
spandx-0.17.0 lib/spandx/spdx/catalogue.rb
spandx-0.16.1 lib/spandx/spdx/catalogue.rb
spandx-0.16.0 lib/spandx/spdx/catalogue.rb
spandx-0.15.1 lib/spandx/spdx/catalogue.rb
spandx-0.15.0 lib/spandx/spdx/catalogue.rb
spandx-0.14.0 lib/spandx/spdx/catalogue.rb
spandx-0.13.5 lib/spandx/spdx/catalogue.rb
spandx-0.13.4 lib/spandx/spdx/catalogue.rb