Class: RubyGPG2::ParameterFileContents

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_gpg2/parameter_file_contents.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ ParameterFileContents

Returns a new instance of ParameterFileContents.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#expiryObject (readonly)

Returns the value of attribute expiry.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def expiry
  @expiry
end

#key_lengthObject (readonly)

Returns the value of attribute key_length.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def key_length
  @key_length
end

#key_typeObject (readonly)

Returns the value of attribute key_type.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def key_type
  @key_type
end

#owner_commentObject (readonly)

Returns the value of attribute owner_comment.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def owner_comment
  @owner_comment
end

#owner_emailObject (readonly)

Returns the value of attribute owner_email.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def owner_email
  @owner_email
end

#owner_nameObject (readonly)

Returns the value of attribute owner_name.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def owner_name
  @owner_name
end

#passphraseObject (readonly)

Returns the value of attribute passphrase.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def passphrase
  @passphrase
end

#subkey_lengthObject (readonly)

Returns the value of attribute subkey_length.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def subkey_length
  @subkey_length
end

#subkey_typeObject (readonly)

Returns the value of attribute subkey_type.



5
6
7
# File 'lib/ruby_gpg2/parameter_file_contents.rb', line 5

def subkey_type
  @subkey_type
end

Instance Method Details

#in_temp_file(tmpdir = nil) ⇒ Object



53
54
55
56
57
58
59
# 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_sObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



47
48
49
50
51
# 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