Sha256: 1b5bb0589e9df257d9680a10b48add74c06a6670d24212e5561dc72d2ccd939b

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'nvd/json_feeds/schema/cve_feed'

require 'multi_json'
require 'digest/sha2'

module NVD
  module JSONFeeds
    class FeedFile

      # The path to the feed file.
      #
      # @return [String]
      attr_reader :path

      #
      # Initializes the feed file.
      #
      # @param [String] path
      #   The path to the feed file.
      #
      def initialize(path)
        @path = File.expand_path(path)
      end

      #
      # @see #parse
      #
      def self.parse(path)
        new(path).parse
      end

      #
      # Calculates the SHA256 checksum of the feed file.
      #
      # @return [String]
      #
      # @note NVD uses all upper-case SHA256 checksums.
      #
      def sha256
        Digest::SHA256.hexdigest(read).upcase
      end

      #
      # Reads the feed file.
      #
      # @return [String]
      #
      # @abstract
      #
      def read
        raise(NotImplementedError,"#{self.class}#read not implemented")
      end

      #
      # Parses the JSON.
      #
      # @return [Hash{String => Object}]
      #   The parsed JSON.
      #
      def json
        MultiJson.load(read)
      end

      #
      # Loads the CVE data from the feed file.
      #
      # @return [CVEFeed]
      #   The CVE feed data.
      #
      def parse
        Schema::CVEFeed.load(json)
      end

      #
      # Converts the feed file to a String.
      #
      # @return [String]
      #   The feed file path.
      #
      def to_s
        @path
      end

      #
      # Inspects the feed file.
      #
      # @return [String]
      #
      def inspect
        "#<#{self.class}: #{self}>"
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nvd-json_feeds-0.1.0 lib/nvd/json_feeds/feed_file.rb