Class: NVD::JSONFeeds::ZipFeedFile

Inherits:
FeedFile
  • Object
show all
Defined in:
lib/nvd/json_feeds/zip_feed_file.rb

Overview

Represents a .json.zip feed file.

Instance Attribute Summary

Attributes inherited from FeedFile

#path

Instance Method Summary collapse

Methods inherited from FeedFile

#initialize, #inspect, #json, parse, #parse, #sha256, #to_s

Constructor Details

This class inherits a constructor from NVD::JSONFeeds::FeedFile

Instance Method Details

#extractJSONFeedFile

Extracts the feed file.

Returns:

Raises:

  • (ExtractFailed)

    The unzip command failed or the .json file was not extracted.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nvd/json_feeds/zip_feed_file.rb', line 47

def extract
  extracted_dir = File.dirname(@path)

  unless system('unzip', '-qq', '-d', extracted_dir, @path, json_filename)
    raise(ExtractFailed,"unzip command failed")
  end

  extracted_path = File.join(extracted_dir,json_filename)

  unless File.file?(extracted_path)
    raise(ExtractFailed,"extraction failed: #{@path.inspect}")
  end

  return JSONFeedFile.new(extracted_path)
end

#json_filenameString

The filename of the .json file within the .zip archive.

Returns:

  • (String)


21
22
23
# File 'lib/nvd/json_feeds/zip_feed_file.rb', line 21

def json_filename
  File.basename(@path,'.zip')
end

#readString

Reads the compressed feed file.

Returns:

  • (String)

Raises:

  • (ReadFailed)

    The unzip command is not installed.



33
34
35
36
37
# File 'lib/nvd/json_feeds/zip_feed_file.rb', line 33

def read
  `unzip -qq -p #{Shellwords.escape(@path)} #{Shellwords.escape(json_filename)}`
rescue Errno::ENOENT
  raise(ReadFailed,"unzip command is not installed")
end