#!/usr/bin/env ruby
# outputs the list of CNA organizationNames and the securityAdvisory urls from the json file here:
# https://raw.githubusercontent.com/CVEProject/cve-website/dev/src/assets/data/CNAsList.json

require 'json'
require 'rest-client'

class CnaSecurityAdvisories 
  attr_accessor :url
  def initialize
    @url = 'https://raw.githubusercontent.com/CVEProject/cve-website/dev/src/assets/data/CNAsList.json'
  end

  def send_request_rest
    RestClient::Request.execute(
      method: :get,
      url: url
    )
  end

  def parse_res(response)
    JSON.parse(response.body)
  end

  def get_json
    res = send_request_rest
    if res.code == 200
      parse_res(res)
    else
      "HTTP Status: #{res.code}"
    end
  end

  def perform
    json = get_json
    json.map do |d|
      org_name = d.dig('organizationName')
      security_advisories = d.dig('securityAdvisories')
      security_advisory_urls = security_advisories.dig('advisories').map { |adv| adv.dig('url') }
      { orgName: org_name, security_advisories_urls: security_advisory_urls }
    end
  end
end
