Class: MyPrecious::MarkdownAdapter

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

Overview

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).

Constant Summary collapse

QS_VALUE_UNSAFE =
/#{URI::UNSAFE}|[&=]/
NVD_CVE_URL_TEMPLATE =

Render links to NIST’s NVD

"https://nvd.nist.gov/vuln/detail/%s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dep) ⇒ MarkdownAdapter

Returns a new instance of MarkdownAdapter.



435
436
437
438
# File 'lib/myprecious.rb', line 435

def initialize(dep)
  super()
  @dependency = dep
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object

Delegate other attribute queries to the base dependency object

Errors are caught and rendered as “(error)”



618
619
620
621
622
623
624
# File 'lib/myprecious.rb', line 618

def method_missing(meth, *args, &blk)
  dependency.send(meth, *args, &blk)
rescue NoMethodError
  raise
rescue StandardError
  "(error)"
end

Instance Attribute Details

#dependencyObject (readonly)

Returns the value of attribute dependency.



439
440
441
# File 'lib/myprecious.rb', line 439

def dependency
  @dependency
end

Class Method Details

.embellish(attr_name, &blk) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# 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

Instance Method Details

#changelogObject

Render short links for http: or https: changelog URLs



545
546
547
548
549
550
551
552
553
554
555
# File 'lib/myprecious.rb', line 545

embellish(:changelog) do |base_val|
  begin
    uri = URI.parse(base_val)
    if ['http', 'https'].include?(uri.scheme)
      next "[on #{uri.hostname}](#{base_val})"
    end
  rescue StandardError
  end
  
  base_val
end

#colorObject

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



589
590
591
592
593
594
595
596
597
598
599
600
601
602
# 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_swatchObject

Markdown for an obsolescence color swatch

Sourced from: stackoverflow.com/a/41247934



609
610
611
# File 'lib/myprecious.rb', line 609

def color_swatch
  "![##{color}](https://placehold.it/15/#{color}/000000?text=+)"
end

#latest_versionObject

Decorate the latest version as a link to the release history



489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/myprecious.rb', line 489

embellish(:latest_version) do |base_val|
  release_history_url = begin
    dependency.release_history_url
  rescue StandardError
    nil
  end
  
  if release_history_url
    "[#{base_val}](#{release_history_url})"
  else
    base_val
  end
end

#licenseObject

Include update info in the license column



532
533
534
535
536
537
538
539
540
# File 'lib/myprecious.rb', line 532

embellish(:license) do |base_val|
  begin
    update_info = base_val.update_info
  rescue NoMethodError
    base_val
  else
    update_info.to_s.empty? ? base_val : "#{base_val}<br/>(#{update_info})"
  end
end

#nameObject

Generate Markdown linking the name to the homepage for the dependency



475
476
477
478
479
480
481
482
483
484
# 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

#obsolescenceObject



580
581
582
583
584
# File 'lib/myprecious.rb', line 580

def obsolescence
  color_swatch
rescue StandardError
  ''
end

Include information about temporal difference between current and recommended versions



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/myprecious.rb', line 507

embellish(:recommended_version) do |recced_ver|
  if recced_ver.kind_of?(String)
    recced_ver = Gem::Version.new(recced_ver)
  end
  next recced_ver if dependency.current_version.nil? || dependency.current_version >= recced_ver
  
  span_comment = begin
    if days_newer = dependency.days_between_current_and_recommended
      " -- #{days_newer} days newer"
    else
      ""
    end
  end
  
  cve_url = URI('https://nvd.nist.gov/products/cpe/search/results')
  cve_url.query = URI.encode_www_form(
    keyword: "cpe:2.3:a:*:#{dependency.name.downcase}:#{recced_ver}",
  )
  
  "**#{recced_ver}**#{span_comment} ([current CVEs](#{cve_url}))"
end