class OpenVPNStatusWeb::Parser::ModernStateless

Public Class Methods

parse_status_log(text, sep) click to toggle source
# File lib/openvpn-status-web/parser/modern_stateless.rb, line 6
def self.parse_status_log(text, sep)
  status = Status.new
  status.client_list_headers = []
  status.client_list = []
  status.routing_table_headers = []
  status.routing_table = []
  status.global_stats = []

  text.lines.each do |line|
    parts = line.strip.split(sep)
    status.client_list_headers = parts[2..] if parts[0] == 'HEADER' && parts[1] == 'CLIENT_LIST'
    status.client_list << parse_client(parts[1..], status.client_list_headers) if parts[0] == 'CLIENT_LIST'
    status.routing_table_headers = parts[2..] if parts[0] == 'HEADER' && parts[1] == 'ROUTING_TABLE'
    status.routing_table << parse_route(parts[1..], status.routing_table_headers) if parts[0] == 'ROUTING_TABLE'
    status.global_stats << parse_global(parts[1..2]) if parts[0] == 'GLOBAL_STATS'
  end

  status
end

Private Class Methods

parse_client(client, headers) click to toggle source
# File lib/openvpn-status-web/parser/modern_stateless.rb, line 26
                     def self.parse_client(client, headers)
  headers.each_with_index do |header, i|
    client[i] = parse_date(client[i]) if header.end_with?('Since')
    client[i] = client[i].to_i if header.start_with?('Bytes')
  end

  client
end
parse_date(date_string) click to toggle source
# File lib/openvpn-status-web/parser/modern_stateless.rb, line 48
                     def self.parse_date(date_string)
  DateTime.strptime(date_string, '%a %b %d %k:%M:%S %Y')
rescue ArgumentError
  DateTime.strptime(date_string, '%Y-%m-%d %k:%M:%S')
end
parse_global(global) click to toggle source
# File lib/openvpn-status-web/parser/modern_stateless.rb, line 43
                     def self.parse_global(global)
  global[1] = global[1].to_i
  global
end
parse_route(route, headers) click to toggle source
# File lib/openvpn-status-web/parser/modern_stateless.rb, line 35
                     def self.parse_route(route, headers)
  headers.each_with_index do |header, i|
    route[i] = parse_date(route[i]) if header.end_with?('Last Ref')
  end

  route
end