class RubyGPG2::ParameterFileContents

Attributes

expiry[R]
key_length[R]
key_type[R]
owner_comment[R]
owner_email[R]
owner_name[R]
passphrase[R]
subkey_length[R]
subkey_type[R]

Public Class Methods

new(opts) click to toggle source
# File lib/ruby_gpg2/parameter_file_contents.rb, line 16
def initialize(opts)
  @key_type = opts.fetch(:key_type, 'RSA')
  @key_length = opts.fetch(:key_length,
      (@key_type.to_s == 'default' ? nil : 2048))
  @subkey_type = opts.fetch(:subkey_type, 'RSA')
  @subkey_length = opts.fetch(:subkey_length,
      ((@subkey_type.nil? || @subkey_type.to_s == 'default') ? nil : 2048))
  @owner_name = opts.fetch(:owner_name, nil)
  @owner_email = opts.fetch(:owner_email, nil)
  @owner_comment = opts.fetch(:owner_comment, nil)
  @expiry = opts.fetch(:expiry, :never)
  @passphrase = opts.fetch(:passphrase, nil)

  owner_name_present = !@owner_name.nil?
  owner_email_present = !@owner_email.nil?
  missing_count = [
      owner_name_present,
      owner_email_present
  ].count(false)
  missing_names = [
      owner_name_present ? nil : :owner_name,
      owner_email_present ? nil : :owner_email
  ].compact

  unless missing_count == 0
    raise RuntimeError.new(
        "Missing required parameter#{missing_count > 1 ? 's' : ''}: " +
            "#{missing_names}.")
  end
end

Public Instance Methods

in_temp_file(tmpdir = nil) { |f| ... } click to toggle source
# File lib/ruby_gpg2/parameter_file_contents.rb, line 53
def in_temp_file(tmpdir = nil)
  Tempfile.create('parameter-file', tmpdir) do |f|
    f.write(to_s)
    f.flush
    yield f
  end
end
to_s() click to toggle source
# File lib/ruby_gpg2/parameter_file_contents.rb, line 61
def to_s
  [
      parm("Key-Type", key_type),
      parm("Key-Length", key_length),
      parm("Subkey-Type", subkey_type),
      parm('Subkey-Length', subkey_length),
      parm('Name-Real', owner_name),
      parm('Name-Comment', owner_comment),
      parm('Name-Email', owner_email),
      parm('Expire-Date',
          expiry == :never ? 0 : expiry),
      parm('Passphrase', passphrase),
  ].compact.join("\n") + "\n"
end
write_to(path) click to toggle source
# File lib/ruby_gpg2/parameter_file_contents.rb, line 47
def write_to(path)
  File.open(path, 'w') do |f|
    f.write(to_s)
  end
end

Private Instance Methods

parm(name, value) click to toggle source
# File lib/ruby_gpg2/parameter_file_contents.rb, line 78
def parm(name, value)
  value ? "#{name}: #{value}" : nil
end