Class: Comuna::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/comuna/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



8
9
10
# File 'lib/comuna/config.rb', line 8

def initialize
  load!
end

Instance Attribute Details

#interfacesObject (readonly)

Returns the value of attribute interfaces.



5
6
7
# File 'lib/comuna/config.rb', line 5

def interfaces
  @interfaces
end

#matchesObject (readonly)

Returns the value of attribute matches.



5
6
7
# File 'lib/comuna/config.rb', line 5

def matches
  @matches
end

#upstreamObject (readonly)

Returns the value of attribute upstream.



5
6
7
# File 'lib/comuna/config.rb', line 5

def upstream
  @upstream
end

Instance Method Details

#extract_with_defaults!(config) ⇒ Object



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

def extract_with_defaults!(config)
  port = config.fetch('port', 5300)
  
  @interfaces = config.fetch('interfaces', [[:udp, '0.0.0.0', port],
                                            [:tcp, '0.0.0.0', port]])
  @upstream = config.fetch('upstream', [[:udp, '8.8.8.8', 53],
                                        [:tcp, '8.8.8.8', 53]])
  @matches = config.fetch('matches', {myself: '0.0.0.0'})
end

#load!Object



12
13
14
# File 'lib/comuna/config.rb', line 12

def load!
  extract_with_defaults!(parsed)
end

#parsedObject



26
27
28
29
# File 'lib/comuna/config.rb', line 26

def parsed
  read = YAML.load(raw)
  read.is_a?(Hash) ? read : {}
end

#rawObject



16
17
18
19
20
21
22
23
24
# File 'lib/comuna/config.rb', line 16

def raw
  if File.exist?('config.yml')
    File.read('config.yml')
  elsif File.exist?('config.yaml')
    File.read('config.yaml')
  else
    ''
  end
end