Class: NVDFeedScraper::Meta

Inherits:
Object
  • Object
show all
Defined in:
lib/nvd_feed_api/meta.rb

Overview

Manage the meta file from a feed.

== Usage

Several ways to set the url:

m = NVDFeedScraper::Meta.new(metaUrl)
m.parse

or

m = NVDFeedScraper::Meta.new
m.url = metaUrl
m.parse

or

m = NVDFeedScraper::Meta.new
m.parse(metaUrl)

Examples:

s = NVDFeedScraper.new
s.scrap
metaUrl = s.feeds("CVE-2014").meta_url
m = NVDFeedScraper::Meta.new
m.url = metaUrl
m.parse
m.sha256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Meta

Returns a new instance of Meta.

Parameters:



63
64
65
# File 'lib/nvd_feed_api/meta.rb', line 63

def initialize(url = nil)
  @url = url
end

Instance Attribute Details

#gz_sizeString (readonly)

NVDFeedScraper::Meta gz size getter

Examples:

'2008357'

Returns:

  • (String)

    the size of the gz file.



54
55
56
# File 'lib/nvd_feed_api/meta.rb', line 54

def gz_size
  @gz_size
end

#last_modified_dateString (readonly)

NVDFeedScraper::Meta last modified date getter

Examples:

'2017-10-19T03:27:02-04:00'

Returns:

  • (String)

    the last modified date and time.



36
37
38
# File 'lib/nvd_feed_api/meta.rb', line 36

def last_modified_date
  @last_modified_date
end

#sha256String (readonly)

NVDFeedScraper::Meta JSON sha256 getter

Examples:

'33ED52D451692596D644F23742ED42B4E350258B11ACB900F969F148FCE3777B'

Returns:

  • (String)

    the SHA256 value of the uncompressed JSON file.



60
61
62
# File 'lib/nvd_feed_api/meta.rb', line 60

def sha256
  @sha256
end

#sizeString (readonly)

NVDFeedScraper::Meta JSON size getter

Examples:

'29443314'

Returns:

  • (String)

    the size of the JSON file uncompressed.



42
43
44
# File 'lib/nvd_feed_api/meta.rb', line 42

def size
  @size
end

#urlString

NVDFeedScraper::Meta URL getter.

Returns:

  • (String)

    The URL of the meta file of the feed.



69
70
71
# File 'lib/nvd_feed_api/meta.rb', line 69

def url
  @url
end

#zip_sizeString (readonly)

NVDFeedScraper::Meta zip size getter

Examples:

'2008493'

Returns:

  • (String)

    the size of the zip file.



48
49
50
# File 'lib/nvd_feed_api/meta.rb', line 48

def zip_size
  @zip_size
end

Instance Method Details

#parseInteger #parse(url) ⇒ Integer

Parse the meta file from the URL and set the attributes.

Overloads:

  • #parseInteger

    Parse the meta file from the URL and set the attributes.

    Returns:

    • (Integer)

      Returns 0 when there is no error.

  • #parse(url) ⇒ Integer

    Set the URL of the meta file of the feed and
    parse the meta file from the URL and set the attributes.

    Parameters:

    Returns:

    • (Integer)

      Returns 0 when there is no error.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nvd_feed_api/meta.rb', line 87

def parse(*arg)
  if arg.length == 1 # arg = url
    self.url = arg[0]
  elsif arg.length > 1
    raise 'Too much arguments'
  end

  raise "Can't parse if the URL is empty" if @url.nil?

  uri = URI(@url)

  meta = Net::HTTP.get(uri)

  meta = meta.split.to_h { |x| x.split(':', 2) }

  raise 'no lastModifiedDate attribute found' unless meta['lastModifiedDate']
  raise 'no valid size attribute found' unless /[0-9]+/.match?(meta['size'])
  raise 'no valid zipSize attribute found' unless /[0-9]+/.match?(meta['zipSize'])
  raise 'no valid gzSize attribute found' unless /[0-9]+/.match?(meta['gzSize'])
  raise 'no valid sha256 attribute found' unless /[0-9A-F]{64}/.match?(meta['sha256'])

  @last_modified_date = meta['lastModifiedDate']
  @size = meta['size']
  @zip_size = meta['zipSize']
  @gz_size = meta['gzSize']
  @sha256 = meta['sha256']

  0
end