Class: Bibliothecary::Parsers::Hex

Inherits:
Object
  • Object
show all
Defined in:
lib/bibliothecary/parsers/hex.rb

Constant Summary collapse

PLATFORM_NAME =
'hex'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



18
19
20
21
# File 'lib/bibliothecary/parsers/hex.rb', line 18

def self.analyse(folder_path, file_list)
  [analyse_mix(folder_path, file_list),
  analyse_mix_lock(folder_path, file_list)]
end

.analyse_mix(folder_path, file_list) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bibliothecary/parsers/hex.rb', line 23

def self.analyse_mix(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^mix\.exs$/) }
  return unless path

  manifest = File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_mix(manifest)
  }
end

.analyse_mix_lock(folder_path, file_list) ⇒ Object



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

def self.analyse_mix_lock(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^mix\.lock$/) }
  return unless path

  manifest = File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_mix_lock(manifest)
  }
end

.parse(filename, file_contents) ⇒ Object



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

def self.parse(filename, file_contents)
  if filename.match(/^mix\.exs$/)
    parse_mix(file_contents)
  elsif filename.match(/^mix\.lock$/)
    parse_mix_lock(file_contents)
  else
    []
  end
end

.parse_mix(manifest) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bibliothecary/parsers/hex.rb', line 49

def self.parse_mix(manifest)
  response = Typhoeus.post("https://mix-deps-json.herokuapp.com/", body: manifest)
  json = JSON.parse response.body

  json.map do |name, version|
    {
      name: name,
      version: version,
      type: "runtime"
    }
  end
end

.parse_mix_lock(manifest) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bibliothecary/parsers/hex.rb', line 62

def self.parse_mix_lock(manifest)
  response = Typhoeus.post("https://mix-deps-json.herokuapp.com/lock", body: manifest)
  json = JSON.parse response.body

  json.map do |name, info|
    {
      name: name,
      version: info['version'],
      type: "runtime"
    }
  end
end