class CveReport
  attr_accessor :filename, :file
    # 1st way:
    # expects a hash with the following key:values # {:filename => "nvdcve-1.0-2002.json.gz",
    # :json => {"CVE_data_type"=>"CVE",
    #           "CVE_Items"= etc...etc...etc...}
    # 2nd way: 
    # a json gz compressed file of cve metadata downloaded from nvd
    # TO ADD: 1st way. 

    def initialize(filename) 
      @filename = filename 
      @file = from_file
    end

    def cve_ids
        # return a list of cve data from the given filename in an array.
        file["CVE_Items"].map do |item|
          item["cve"]["CVE_data_meta"]["ID"]
        end
    end

    def cve_id?(cve_id)
      cve_ids.include?(cve_id)
    end

    def cve(cve_id)
      find(cve_id)
    end

    def cves
      file["CVE_Items"].map do |cve|
        cve
      end
    end

    def from_file
        Zlib::GzipReader.open(filename) do |gz|
            JSON.parse(gz.read)
        end
    end

    def find(cve_id)
      result = cves.select do |cve|
        cve["cve"]["CVE_data_meta"]["ID"] == cve_id
      end
      if result.count == 1
        result.first
      else 
        "Could not find valid CVE for: #{cve}"
      end
    end

    def description(cve_id)
      cve = find(cve_id)
      cve['cve']['description']['description_data'].first['value']
    end

    def description_hash(cve_id)
      description_value = description(cve_id)
      { :cve_id => cve_id, :description => description_value }
    end

    def reference_data(cve_id)
      cve = find(cve_id)
      cve['cve']['references']['reference_data']
    end
end