Class: NVD::JSONFeeds::FeedURI

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

Overview

Base class for all feed URIs.

Direct Known Subclasses

GzFeedURI, MetaFeedURI, ZipFeedURI

Constant Summary collapse

SCHEMA_VERSION =
'1.1'
BASE_URI =
"https://nvd.nist.gov/feeds/json/cve/#{SCHEMA_VERSION}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ext) ⇒ FeedURI

Initializes the feed URI.

Parameters:

  • name (:modified, :recent, Integer)

    The feed name or year.

  • ext (String)

    The feed file extension (ex: .json.gz).



45
46
47
48
49
50
51
# File 'lib/nvd/json_feeds/feed_uri.rb', line 45

def initialize(name,ext)
  @name = name
  @ext  = ext

  @filename = "nvdcve-#{SCHEMA_VERSION}-#{@name}#{@ext}"
  @uri      = URI("#{BASE_URI}/#{@filename}")
end

Instance Attribute Details

#extString (readonly)

The feed file extension.

Returns:

  • (String)


24
25
26
# File 'lib/nvd/json_feeds/feed_uri.rb', line 24

def ext
  @ext
end

#filenameString (readonly)

The file name of the feed.

Returns:

  • (String)


29
30
31
# File 'lib/nvd/json_feeds/feed_uri.rb', line 29

def filename
  @filename
end

#name:modified, ... (readonly)

The feed name or year.

Returns:

  • (:modified, :recent, Integer)


19
20
21
# File 'lib/nvd/json_feeds/feed_uri.rb', line 19

def name
  @name
end

#uriURI::HTTPS (readonly)

The feed URI.

Returns:

  • (URI::HTTPS)


34
35
36
# File 'lib/nvd/json_feeds/feed_uri.rb', line 34

def uri
  @uri
end

Instance Method Details

#download(dest) ⇒ Object

Downloads the feed to the given destination.

Parameters:

  • dest (String)

    Either a destination file or directory.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nvd/json_feeds/feed_uri.rb', line 86

def download(dest)
  dest_path = if File.directory?(dest) then File.join(dest,@filename)
              else                          dest
              end

  File.open(dest_path,'w') do |file|
    get do |chunk|
      file.write(chunk)
    end
  end

  return dest_path
end

#get {|chunk| ... } ⇒ String

Performs and HTTP GET request to the feed URI.

Yields:

  • (chunk)

    If a block is given, it will be passed each chunk read from the response.

Yield Parameters:

  • chunk (String)

    A chunk of data read from the response body.

Returns:

  • (String)

    If no block is given, the full response body will be returned.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nvd/json_feeds/feed_uri.rb', line 66

def get(&block)
  if block
    Net::HTTP.start(@uri.host,@uri.port, use_ssl: true) do |http|
      request = Net::HTTP::Get.new(uri)

      http.request(request) do |response|
        response.read_body(&block)
      end
    end
  else
    Net::HTTP.get(@uri)
  end
end

#inspectString

Inspects the feed URI.

Returns:

  • (String)


125
126
127
# File 'lib/nvd/json_feeds/feed_uri.rb', line 125

def inspect
  "#<#{self.class}: #{self}>"
end

#to_sString

Converts the feed URI to a String.

Returns:

  • (String)

    The String version of the feed URI.



116
117
118
# File 'lib/nvd/json_feeds/feed_uri.rb', line 116

def to_s
  @uri.to_s
end

#to_uriURI::HTTPS

Converts the feed URI to a regular URI.

Returns:

  • (URI::HTTPS)

    The feed URI.



106
107
108
# File 'lib/nvd/json_feeds/feed_uri.rb', line 106

def to_uri
  @uri
end