Module: LetsCert::FileIOPluginMixin

Included in:
AccountKey, CertFile, ChainFile, KeyFile
Defined in:
lib/letscert/io_plugins/file_io_plugin_mixin.rb

Overview

Mixin for IOPmugin subclasses that handle files

Author:

  • Sylvain Daubert

Instance Method Summary collapse

Instance Method Details

#loadHash

Load data from file named #name

Returns:

  • (Hash)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/letscert/io_plugins/file_io_plugin_mixin.rb', line 30

def load
  logger.debug { "Loading #{@name}" }

  begin
    content = File.read(@name)
  rescue Errno::ENOENT
    logger.info { "no #{@name} file" }
    return self.class.empty_data
  end

  load_from_content(content)
end

#load_from_content(_content) ⇒ Hash

This method is abstract.

Parameters:

  • _content (String)

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/letscert/io_plugins/file_io_plugin_mixin.rb', line 46

def load_from_content(_content)
  raise NotImplementedError
end

#save_to_file(data) ⇒ void

This method returns an undefined value.

Save data to file #name

Parameters:

  • data (Hash)

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/letscert/io_plugins/file_io_plugin_mixin.rb', line 54

def save_to_file(data)
  return if data.nil?

  # Return if content did not change
  if File.exist? name
    old_content = File.read(name)
    return if old_content == data
  end

  logger.info { "saving #{@name}" }
  begin
    File.open(name, 'w') do |f|
      f.write(data)
    end
  rescue Errno => ex
    @logger.error { ex.message }
    raise Error, "Error when saving #{@name}"
  end
end