Sha256: 937d101aaaf32ec0567e862d215103e0bb6fce824a8d17a35a7be73183899867

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.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 + 2 # use 1-based index just like the 'csv' std lib, and count the headers as first row.
          required_headers.each do |h|
            raise "missing field '#{h}' on line #{line}" if row[h].nil? || 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

1 entries across 1 versions & 1 rubygems

Version Path
bibliothecary-7.3.6 lib/bibliothecary/parsers/generic.rb