Class: MeshChat::Config::Settings

Inherits:
HashFile
  • Object
show all
Defined in:
lib/meshchat/config/settings.rb

Constant Summary collapse

FILENAME =
'settings.json'
DEFAULT_SETTINGS =
{
  'alias' => 'alias',
  'port' => '2008',
  'ip' => 'localhost',
  'uid' => '1',
  'publicKey' => 'replace this'
}

Instance Attribute Summary

Attributes inherited from HashFile

#_hash

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HashFile

#[], #[]=, #as_hash, #default_settings, #display, #exists?, #filename, #load, #read_file, #save, #set

Constructor Details

#initializeSettings

Returns a new instance of Settings.



92
93
94
95
96
# File 'lib/meshchat/config/settings.rb', line 92

def initialize
  @default_settings = DEFAULT_SETTINGS
  @filename = FILENAME
  super
end

Class Method Details

.instanceObject



22
23
24
# File 'lib/meshchat/config/settings.rb', line 22

def instance
  @instance ||= new
end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/meshchat/config/settings.rb', line 27

def debug?
  ['true', '1', 'yes', 'y', 't'].include?(self['debug'].try(:downcase))
end

#errorsObject



102
103
104
105
106
107
108
109
# File 'lib/meshchat/config/settings.rb', line 102

def errors
  messages = []
  messages << 'must have an alias' if !self['alias']
  messages << 'must have ip set' if !self['ip']
  messages << 'must have port set' if !self['port']
  messages << 'must have uid set' if !self['uid']
  messages
end

#generate_keysObject



70
71
72
73
74
75
# File 'lib/meshchat/config/settings.rb', line 70

def generate_keys
  @key_pair = OpenSSL::PKey::RSA.new(2048)
  self['publicKey'] = @key_pair.public_key.to_pem
  self['privateKey'] = @key_pair.to_pem
  Display.success 'new keys generated'
end

#generate_uidObject

generates 32 bytes



65
66
67
68
# File 'lib/meshchat/config/settings.rb', line 65

def generate_uid
  self['uid'] = SecureRandom.hex(32)
  Display.success 'new uid set'
end

#identityObject



84
85
86
# File 'lib/meshchat/config/settings.rb', line 84

def identity
  "#{self['alias']}@#{location}##{self['uid']}"
end

#identity_as_jsonObject



31
32
33
34
35
36
37
38
# File 'lib/meshchat/config/settings.rb', line 31

def identity_as_json
  {
    'alias' => self['alias'],
    'location' => location,
    'uid' => self['uid'],
    'publicKey' => public_key
  }
end

#key_pairObject



77
78
79
80
81
82
# File 'lib/meshchat/config/settings.rb', line 77

def key_pair
  if !@key_pair && keys_exist?
    @key_pair = OpenSSL::PKey::RSA.new(self['privateKey'] + self['publicKey'])
  end
  @key_pair
end

#keys_exist?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/meshchat/config/settings.rb', line 48

def keys_exist?
  public_key.present? && private_key.present?
end

#locationObject



88
89
90
# File 'lib/meshchat/config/settings.rb', line 88

def location
  "#{self['ip']}:#{self['port']}"
end

#private_keyObject



60
61
62
# File 'lib/meshchat/config/settings.rb', line 60

def private_key
  self['privatKey']
end

#public_keyObject



56
57
58
# File 'lib/meshchat/config/settings.rb', line 56

def public_key
  self['publicKey']
end

#shareObject



40
41
42
43
44
45
46
# File 'lib/meshchat/config/settings.rb', line 40

def share
  data = identity_as_json.to_json

  filename = "#{identity_as_json['alias']}.json"
  File.open(filename, 'w'){ |f| f.syswrite(data) }
  Display.info "#{filename} written..."
end

#uid_exists?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/meshchat/config/settings.rb', line 52

def uid_exists?
  self['uid'].present?
end

#valid?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/meshchat/config/settings.rb', line 98

def valid?
  errors.empty?
end