Module: RPSL
- Defined in:
- lib/rpsl.rb
Overview
Library which provides functions for reading and writing Routing Policy Specification Language (RPSL, RFC2622).
Defined Under Namespace
Classes: DumpError, ParserError, RPSLError
Class Method Summary collapse
-
.dump(obj, value_column = nil) ⇒ Object
Converts a hash into the RPSL format.
-
.load(rpsl) ⇒ Hash
Parses a string as RPSL.
Class Method Details
.dump(obj, value_column = nil) ⇒ Object
Converts a hash into the RPSL format.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/rpsl.rb', line 162 def self.dump(obj, value_column = nil) rpsl = '' obj.each do |key, value| lines = value.to_s.lines lines.map!(&:chomp) first_value = lines.shift key = key.to_s if value_column padding_length = value_column.to_i - 1 - key.length raise DumpError, 'Key too long.' if padding_length.negative? padding = ' ' * padding_length end rpsl += "#{key}:#{padding}#{first_value}\n" lines.each do |line| rpsl += if line.empty? "+\n" elsif value_column "#{' ' * value_column}#{line}\n" else " #{line}\n" end end end return rpsl end |
.load(rpsl) ⇒ Hash
Parses a string as RPSL.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rpsl.rb', line 75 def self.load(rpsl) obj = {} current_section = nil rpsl.to_s.each_line.with_index do |line, lineno| line.chomp! case line when /^([\d\w\-_]+): *(.*)$/ current_section = $1 obj[current_section] = "#{obj[current_section]}#{$2}\n" when /^[+ \s] *(.+)$/ raise ParserError, "Value without key in line #{lineno + 1}" unless current_section obj[current_section] = "#{obj[current_section]}#{$1}\n" when /^\+ *$/ raise ParserError, "Empty line without key in line #{lineno + 1}" unless current_section obj[current_section] = "#{obj[current_section]}\n" else raise ParserError, "Failed to parse line #{lineno + 1}." end end return obj end |