Sha256: 6a5d3672f181a37a358794a0bd9921f69d3c1e746bac0dd715303f276f2e4e3f

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module OpenVPNStatusWeb
  module Parser
    class ModernStateless
      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_method 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

      private_class_method 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

      private_class_method def self.parse_global(global)
        global[1] = global[1].to_i
        global
      end

      private_class_method 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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
openvpn-status-web-3.4.0 lib/openvpn-status-web/parser/modern_stateless.rb
openvpn-status-web-3.3.0 lib/openvpn-status-web/parser/modern_stateless.rb