class Minimart::Mirror::DownloadMetadata

This class can be used to parse, and create `.minimart.json` files to store information about when, and how Minimart downloaded a given cookbook.

Constants

FILE_NAME

Attributes

metadata[R]

@return [Hash] the contents of the metadata file.

path_to_cookbook[R]

@return [String] the path to the directory containing the cookbook.

Public Class Methods

new(path_to_cookbook) click to toggle source

@param [String] path_to_cookbook The path to the directory containing the cookbook.

# File lib/minimart/mirror/download_metadata.rb, line 16
def initialize(path_to_cookbook)
  @path_to_cookbook = path_to_cookbook
  parse_file
end

Public Instance Methods

[](key) click to toggle source
# File lib/minimart/mirror/download_metadata.rb, line 39
def [](key)
  metadata[key] if metadata
end
downloaded_at() click to toggle source

@return [Time] The downloaded_at time found in the metadata file.

# File lib/minimart/mirror/download_metadata.rb, line 34
def downloaded_at
  return unless self['downloaded_at']
  Time.iso8601(metadata['downloaded_at']).utc
end
has_key?(key) click to toggle source
# File lib/minimart/mirror/download_metadata.rb, line 43
def has_key?(key)
  (metadata ? metadata.has_key?(key) : false)
end
write(contents = {}) click to toggle source

Write the given contents to the metadata file. This will overwrite any existing contents. @param [Hash] contents The hash of data to write to the file.

# File lib/minimart/mirror/download_metadata.rb, line 24
def write(contents = {})
  File.open(file_path, 'w+') do |file|
    @metadata = contents
    @metadata.merge!('downloaded_at' => Time.now.utc.iso8601)

    file.write(metadata.to_json)
  end
end

Private Instance Methods

file_path() click to toggle source
# File lib/minimart/mirror/download_metadata.rb, line 55
def file_path
  File.join(path_to_cookbook, FILE_NAME)
end
parse_file() click to toggle source
# File lib/minimart/mirror/download_metadata.rb, line 49
def parse_file
  return unless File.exists?(file_path)
  file_contents = File.open(file_path).read
  @metadata = JSON.parse(file_contents)
end