class MyPrecious::MarkdownAdapter

Dependency info wrapper to generate nice Markdown columns

This wrapper takes basic data from the underlying dependency info object and returns enhanced Markdown for selected columns (e.g. name).

Constants

NVD_CVE_URL_TEMPLATE

Render links to NIST's NVD

QS_VALUE_UNSAFE

Attributes

dependency[R]

Public Class Methods

embellish(attr_name, &blk) click to toggle source
# File lib/myprecious.rb, line 441
def self.embellish(attr_name, &blk)
  define_method(attr_name) do
    value = begin
      dependency.send(attr_name)
    rescue NoMethodError
      raise
    rescue StandardError
      return "(error)"
    end
    
    begin
      instance_exec(value, &blk)
    rescue StandardError => ex
      err_key = [ex.backtrace[0], ex.to_s]
      unless (@errors ||= Set.new).include?(err_key)
        @errors << err_key
        if MyPrecious.tracing_errors?
          $stderr.puts("Traceback (most recent call last):")
          ex.backtrace[1..-1].reverse.each_with_index do |loc, i|
            $stderr.puts("#{(i + 1).to_s.rjust(8)}: #{loc}")
          end
          $stderr.puts("#{ex.backtrace[0]}: #{ex} (#{ex.class} while computing #{attr_name})")
        else
          $stderr.puts("#{ex} (while computing #{attr_name})")
        end
      end
      value
    end
  end
end
new(dep) click to toggle source
Calls superclass method
# File lib/myprecious.rb, line 435
def initialize(dep)
  super()
  @dependency = dep
end

Public Instance Methods

color() click to toggle source

Get a CSS-style hex color code corresponding to the obsolescence of the dependency

# File lib/myprecious.rb, line 589
def color
  red = "fb0e0e"
  
  if (dependency.cves.map(&:score).compact.max || 0) >= 7
    return red
  end
  
  case dependency.obsolescence
  when :mild then "dde418"
  when :moderate then "f9b733"
  when :severe then red
  else "4dda1b"
  end
end
color_swatch() click to toggle source

Markdown for an obsolescence color swatch

Sourced from: stackoverflow.com/a/41247934

# File lib/myprecious.rb, line 609
def color_swatch
  "![##{color}](https://placehold.it/15/#{color}/000000?text=+)"
end
method_missing(meth, *args, &blk) click to toggle source

Delegate other attribute queries to the base dependency object

Errors are caught and rendered as “(error)”

# File lib/myprecious.rb, line 618
def method_missing(meth, *args, &blk)
  dependency.send(meth, *args, &blk)
rescue NoMethodError
  raise
rescue StandardError
  "(error)"
end
obsolescence() click to toggle source
# File lib/myprecious.rb, line 580
def obsolescence
  color_swatch
rescue StandardError
  ''
end
unknown() click to toggle source

Generate Markdown linking the name to the homepage for the dependency

# File lib/myprecious.rb, line 475
embellish(:name) do |base_val|
  cswatch = begin
    color_swatch + ' '
  rescue StandardError
    ''
  end
  "#{cswatch}[#{base_val}](#{dependency.homepage_uri})"
rescue Gems::NotFound
  base_val
end