Class: Avmtrf1::Openvpn::Config::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/avmtrf1/openvpn/config/builder.rb

Instance Method Summary collapse

Instance Method Details

#dataObject


17
18
19
# File 'lib/avmtrf1/openvpn/config/builder.rb', line 17

def data
  [simple, tags].reject(&:blank?).join("\n")
end

#on_data_fileObject


21
22
23
24
25
26
# File 'lib/avmtrf1/openvpn/config/builder.rb', line 21

def on_data_file
  ::EacRubyUtils::Fs::Temp.on_file do |file|
    file.write(data)
    yield(file)
  end
end

#simpleObject


28
29
30
31
32
# File 'lib/avmtrf1/openvpn/config/builder.rb', line 28

def simple
  source_data.fetch(:simple)
             .map { |s| ::Shellwords.join([s.fetch(:attribute)] + s.fetch(:args)) }
             .map { |s| "#{s}\n" }.join
end

#simple_add_or_update(name, args) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'lib/avmtrf1/openvpn/config/builder.rb', line 34

def simple_add_or_update(name, args)
  new_data = { attribute: name, args: args }
  s = simple_find(name)
  if s
    s.merge!(new_data)
  else
    source_data.fetch(:simple) << new_data
  end
end

#simple_find(name) ⇒ Object


53
54
55
# File 'lib/avmtrf1/openvpn/config/builder.rb', line 53

def simple_find(name)
  source_data.fetch(:simple).find { |s| s.fetch(:attribute) == name }
end

#tag_to_data(tag) ⇒ Object


48
49
50
51
# File 'lib/avmtrf1/openvpn/config/builder.rb', line 48

def tag_to_data(tag)
  ["<#{tag.fetch(:name)}>", tag.fetch(:content), "</#{tag.fetch(:name)}>"]
    .map { |v| "#{v}\n" }.join
end

#tagsObject


44
45
46
# File 'lib/avmtrf1/openvpn/config/builder.rb', line 44

def tags
  source_data.fetch(:tags).map { |t| tag_to_data(t) }
end