Class: Meshchat::Configuration::Settings

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

Constant Summary collapse

FILENAME =
'settings.json'
DEFAULT_SETTINGS =
{
  'alias' => '',
  'port' => '8009',
  'ip' => 'localhost',
  'uid' => '',
  'publickey' => '',
  'relays' => [
    'wss://mesh-relay-in-us-1.herokuapp.com',
    'wss://mesh-relay-us-2.herokuapp.com'
  ]
}.freeze

Instance Attribute Summary

Attributes inherited from HashFile

#_hash

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.



84
85
86
87
88
# File 'lib/meshchat/configuration/settings.rb', line 84

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

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/meshchat/configuration/settings.rb', line 19

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

#errorsObject



94
95
96
97
98
99
100
101
# File 'lib/meshchat/configuration/settings.rb', line 94

def errors
  messages = []
  messages << 'must have an alias' unless self['alias'].present?
  messages << 'must have ip set' unless self['ip'].present?
  messages << 'must have port set' unless self['port'].present?
  messages << 'must have uid set' unless self['uid'].present?
  messages
end

#generate_keysObject



62
63
64
65
66
67
# File 'lib/meshchat/configuration/settings.rb', line 62

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



57
58
59
60
# File 'lib/meshchat/configuration/settings.rb', line 57

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

#identityObject



76
77
78
# File 'lib/meshchat/configuration/settings.rb', line 76

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

#identity_as_jsonObject



23
24
25
26
27
28
29
30
# File 'lib/meshchat/configuration/settings.rb', line 23

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

#is_complete?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/meshchat/configuration/settings.rb', line 103

def is_complete?
  valid? && self['privatekey'] && self['publickey']
end

#key_pairObject



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

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)


40
41
42
# File 'lib/meshchat/configuration/settings.rb', line 40

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

#locationObject



80
81
82
# File 'lib/meshchat/configuration/settings.rb', line 80

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

#private_keyObject



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

def private_key
  self['privatkey']
end

#public_keyObject



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

def public_key
  self['publickey']
end

#shareObject



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

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)


44
45
46
# File 'lib/meshchat/configuration/settings.rb', line 44

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

#valid?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/meshchat/configuration/settings.rb', line 90

def valid?
  errors.empty?
end