Sha256: e171661829f527f9bd08036281992d69d41353ebb6286e1b75a47528a2f293a9

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

require 'csv'

module Bibliothecary
  module Parsers
    class Generic
      include Bibliothecary::Analyser

      def self.mapping
        {
          match_filename("dependencies.csv") => {
            kind: 'lockfile',
            parser: :parse_lockfile
          }
        }
      end

      def self.parse_lockfile(file_contents)
        table = CSV.parse(file_contents, headers: true)

        required_headers = ["platform", "name", "requirement"]
        missing_headers = required_headers - table.headers
        raise "Missing headers #{missing_headers} in CSV" unless missing_headers.empty?

        table.map.with_index do |row, idx|
          line = idx + 1
          required_headers.each do |h|
            raise "missing field '#{h}' on line #{line}" if row[h].empty?
          end
          {
            platform: row['platform'],
            name: row['name'],
            requirement: row['requirement'],
            type: row.fetch('type', 'runtime'),
          }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bibliothecary-7.3.5 lib/bibliothecary/parsers/generic.rb
bibliothecary-7.3.4 lib/bibliothecary/parsers/generic.rb
bibliothecary-7.3.3 lib/bibliothecary/parsers/generic.rb