Class: Bibliothecary::Parsers::Packagist

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

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.map_dependencies(hash, key, type) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/bibliothecary/parsers/packagist.rb', line 37

def self.map_dependencies(hash, key, type)
  hash.fetch(key,[]).map do |name, requirement|
    {
      name: name,
      requirement: requirement,
      type: type
    }
  end
end

.parse(filename, path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bibliothecary/parsers/packagist.rb', line 8

def self.parse(filename, path)
  if filename.match(/^composer\.json$/)
    file_contents = File.open(path).read
    json = JSON.parse(file_contents)
    parse_manifest(json)
  elsif filename.match(/^composer\.lock$/)
    file_contents = File.open(path).read
    json = JSON.parse(file_contents)
    parse_lockfile(json)
  else
    []
  end
end

.parse_lockfile(manifest) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/bibliothecary/parsers/packagist.rb', line 22

def self.parse_lockfile(manifest)
  manifest.fetch('packages',[]).map do |dependency|
    {
      name: dependency["name"],
      requirement: dependency["version"],
      type: 'runtime'
    }
  end
end

.parse_manifest(manifest) ⇒ Object



32
33
34
35
# File 'lib/bibliothecary/parsers/packagist.rb', line 32

def self.parse_manifest(manifest)
  map_dependencies(manifest, 'require', 'runtime') +
  map_dependencies(manifest, 'require-dev', 'development')
end