mirror of
https://github.com/cmur2/openvpn-status-web.git
synced 2025-06-29 10:30:23 +02:00
Add tests and bug fixed parser
This commit is contained in:
@ -10,13 +10,32 @@ module OpenVPNStatusWeb
|
||||
|
||||
text.lines.each do |line|
|
||||
parts = line.strip.split(sep)
|
||||
status.client_list << parts[1..5] if parts[0] == "CLIENT_LIST"
|
||||
status.routing_table << parts[1..4] if parts[0] == "ROUTING_TABLE"
|
||||
status.global_stats << parts[1..2] if parts[0] == "GLOBAL_STATS"
|
||||
status.client_list << parse_client(parts[1..5]) if parts[0] == "CLIENT_LIST"
|
||||
status.routing_table << parse_route(parts[1..4]) if parts[0] == "ROUTING_TABLE"
|
||||
status.global_stats << parse_global(parts[1..2]) if parts[0] == "GLOBAL_STATS"
|
||||
end
|
||||
|
||||
status
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.parse_client(client)
|
||||
client[2] = client[2].to_i
|
||||
client[3] = client[3].to_i
|
||||
client[4] = DateTime.strptime(client[4], '%a %b %d %k:%M:%S %Y')
|
||||
client
|
||||
end
|
||||
|
||||
def self.parse_route(route)
|
||||
route[3] = DateTime.strptime(route[3], '%a %b %d %k:%M:%S %Y')
|
||||
route
|
||||
end
|
||||
|
||||
def self.parse_global(global)
|
||||
global[1] = global[1].to_i
|
||||
global
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -15,18 +15,40 @@ module OpenVPNStatusWeb
|
||||
(current_section = :end; next) if line == "END\n"
|
||||
|
||||
case current_section
|
||||
when :cl then client_list << line.strip.split(',')
|
||||
when :rt then routing_table << line.strip.split(',')
|
||||
when :gs then global_stats << line.strip.split(',')
|
||||
when :cl
|
||||
client_list << line.strip.split(',')
|
||||
when :rt
|
||||
routing_table << line.strip.split(',')
|
||||
when :gs
|
||||
global_stats << line.strip.split(',')
|
||||
end
|
||||
end
|
||||
|
||||
status = Status.new
|
||||
status.client_list = client_list[2..-1]
|
||||
status.routing_table = routing_table[1..-1]
|
||||
status.global_stats = global_stats
|
||||
status.client_list = client_list[2..-1].map { |client| parse_client(client) }
|
||||
status.routing_table = routing_table[1..-1].map { |route| parse_route(route) }
|
||||
status.global_stats = global_stats.map { |global| parse_global(global) }
|
||||
status
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_client(client)
|
||||
client[2] = client[2].to_i
|
||||
client[3] = client[3].to_i
|
||||
client[4] = DateTime.strptime(client[4], '%a %b %d %k:%M:%S %Y')
|
||||
client
|
||||
end
|
||||
|
||||
def parse_route(route)
|
||||
route[3] = DateTime.strptime(route[3], '%a %b %d %k:%M:%S %Y')
|
||||
route
|
||||
end
|
||||
|
||||
def parse_global(global)
|
||||
global[1] = global[1].to_i
|
||||
global
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user