Module: LetsCert::FileIOPluginMixin

Included in:
AccountKey, CertFile, ChainFile, KeyFile
Defined in:
lib/letscert/io_plugin.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)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/letscert/io_plugin.rb', line 95

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

  begin
    content = File.read(@name)
  rescue SystemCallError => ex
    if ex.is_a? Errno::ENOENT
      logger.info { "no #@name file" }
      return self.class.empty_data
    end
    raise
  end

  load_from_content(content)
end

#load_from_content(content) ⇒ Hash

This method is abstract.

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


113
114
115
# File 'lib/letscert/io_plugin.rb', line 113

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)


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/letscert/io_plugin.rb', line 120

def save_to_file(data)
  return if data.nil?

  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