class PuTTY::Key::PPK::Writer
Handles writing .ppk files.
@private
Attributes
The number of bytes that have been written.
@return [Integer] The number of bytes that have been written.
Public Class Methods
Initializes a new {Writer} with an IO
-like instance to write to.
@param file [Object] The IO
-like instance to write to.
# File lib/putty/key/ppk.rb, line 706 def initialize(file) @file = file @bytes_written = 0 end
Opens a .ppk file for writing (or uses the provided IO
-like instance), creates a new instance of Writer
and yields it to the caller.
@param path_or_io [Object] The path of the .ppk file to be written or an IO
-like instance.
@return [Object] The result of yielding to the caller.
@raise [Errno::ENOENT] If a directory specified by path
does not exist.
# File lib/putty/key/ppk.rb, line 692 def self.open(path_or_io) if path_or_io.kind_of?(String) || path_or_io.kind_of?(Pathname) File.open(path_or_io.to_s, 'wb') do |file| yield Writer.new(file) end else path_or_io.binmode if path_or_io.respond_to?(:binmode) yield Writer.new(path_or_io) end end
Public Instance Methods
Writes a blob to the file (Lines field and Base64 encoded value).
@param name [String] The name of the blob (used as a prefix for the lines field). @param blob [String] The value of the blob. This is Base64 encoded before being written to the file.
# File lib/putty/key/ppk.rb, line 728 def blob(name, blob) lines = [blob].pack('m48').split("\n") field("#{name}-Lines", lines.length) lines.each do |line| write(line) write_line end end
Writes a field to the file.
@param name [String] The field name. @param value [Object] The field value.
# File lib/putty/key/ppk.rb, line 715 def field(name, value) write(name) write(': ') write(value.to_s) write_line end
Private Instance Methods
Writes a string to the file.
@param string [String] The string to be written.
# File lib/putty/key/ppk.rb, line 747 def write(string) @bytes_written += @file.write(string) end
Writes a line separator to the file (n on all platforms).
# File lib/putty/key/ppk.rb, line 740 def write_line write("\n") end