Class: Bibliothecary::Parsers::Generic

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/generic.rb

Class Method Summary collapse

Methods included from Analyser

create_analysis, create_error_analysis, included

Class Method Details

.mappingObject



8
9
10
11
12
13
14
15
# File 'lib/bibliothecary/parsers/generic.rb', line 8

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

.parse_lockfile(file_contents) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bibliothecary/parsers/generic.rb', line 17

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