Class: Iconmap::Npm

Inherits:
Object
  • Object
show all
Defined in:
lib/iconmap/npm.rb

Defined Under Namespace

Classes: Error, HTTPError, OutdatedPackage, VulnerablePackage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iconmap_path = 'config/iconmap.rb') ⇒ Npm

Returns a new instance of Npm.



21
22
23
# File 'lib/iconmap/npm.rb', line 21

def initialize(iconmap_path = 'config/iconmap.rb')
  @iconmap_path = Pathname.new(iconmap_path)
end

Instance Attribute Details

#base_uriObject

Returns the value of attribute base_uri.



18
19
20
# File 'lib/iconmap/npm.rb', line 18

def base_uri
  @base_uri
end

Instance Method Details

#outdated_packagesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/iconmap/npm.rb', line 25

def outdated_packages
  packages_with_versions.each.with_object([]) do |(package, current_version, icon_path), outdated_packages|
    outdated_package = OutdatedPackage.new(package: package, icon_path: icon_path, current_version: current_version)

    latest_version = jsdelivr.resolve_version(package)

    if latest_version.nil?
      outdated_package.error = 'Response error'
    elsif outdated?(current_version, latest_version)
      outdated_package.latest_version = latest_version
    else
      next
    end

    outdated_packages << outdated_package
  end.sort_by(&:icon_path)
end

#packages_with_versionsObject



54
55
56
57
58
59
# File 'lib/iconmap/npm.rb', line 54

def packages_with_versions
  iconmap.scan(/^pin ['"]([^'"]+)['"].*#\s*@(\S+)/).map do |package_with_path, version|
    package = extract_package_name(package_with_path)
    [package, version, package_with_path]
  end
end

#vulnerable_packagesObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/iconmap/npm.rb', line 43

def vulnerable_packages
  get_audit.flat_map do |package, vulnerabilities|
    vulnerabilities.map do |vulnerability|
      VulnerablePackage.new(name: package,
                            severity: vulnerability['severity'],
                            vulnerable_versions: vulnerability['vulnerable_versions'],
                            vulnerability: vulnerability['title'])
    end
  end.sort_by { |p| [p.name, p.severity] }
end