1
0
mirror of https://github.com/cmur2/openvpn-status-web.git synced 2024-12-22 12:54:24 +01:00

Refactor V2 and V3 parser into one

This commit is contained in:
cn 2013-05-03 20:16:17 +02:00
parent 33013c56f3
commit a1a6b33902
4 changed files with 40 additions and 16 deletions

View File

@ -12,6 +12,7 @@ require 'better_errors'
require 'openvpn-status-web/status'
require 'openvpn-status-web/parser/v1'
require 'openvpn-status-web/parser/v2'
require 'openvpn-status-web/parser/v3'
require 'openvpn-status-web/int_patch'
require 'openvpn-status-web/version'
@ -64,6 +65,8 @@ module OpenVPNStatusWeb
OpenVPNStatusWeb::Parser::V1.new.parse_status_log(text)
when 2
OpenVPNStatusWeb::Parser::V2.new.parse_status_log(text)
when 3
OpenVPNStatusWeb::Parser::V3.new.parse_status_log(text)
else
raise "No suitable parser for status-version #{vpn['version']}"
end

View File

@ -0,0 +1,22 @@
module OpenVPNStatusWeb
module Parser
class ModernStateless
def self.parse_status_log(text, sep)
status = Status.new
status.client_list = []
status.routing_table = []
status.global_stats = []
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"
end
status
end
end
end
end

View File

@ -1,24 +1,11 @@
require 'openvpn-status-web/parser/modern_stateless'
module OpenVPNStatusWeb
module Parser
class V2
def parse_status_log(text)
client_list = []
routing_table = []
global_stats = []
text.lines.each do |line|
parts = line.strip.split(',')
client_list << parts[1..5] if parts[0] == "CLIENT_LIST"
routing_table << parts[1..4] if parts[0] == "ROUTING_TABLE"
global_stats << parts[1..2] if parts[0] == "GLOBAL_STATS"
end
status = Status.new
status.client_list = client_list
status.routing_table = routing_table
status.global_stats = global_stats
status
OpenVPNStatusWeb::Parser::ModernStateless.parse_status_log(text, ',')
end
end
end

View File

@ -0,0 +1,12 @@
require 'openvpn-status-web/parser/modern_stateless'
module OpenVPNStatusWeb
module Parser
class V3
def parse_status_log(text)
OpenVPNStatusWeb::Parser::ModernStateless.parse_status_log(text, "\t")
end
end
end
end