From 39d2b3ce94b17f4cbc02a5652cc8f84d2547299a Mon Sep 17 00:00:00 2001 From: cn Date: Mon, 17 Jan 2022 11:37:50 +0100 Subject: [PATCH] gem: refactor to prepare supporting flexible list headers --- lib/openvpn-status-web/main.html.erb | 68 ++++++++++++++----- .../parser/modern_stateless.rb | 2 + lib/openvpn-status-web/parser/v1.rb | 2 + lib/openvpn-status-web/status.rb | 2 + .../parser/modern_stateless_spec.rb | 8 +++ spec/openvpn-status-web/parser/v1_spec.rb | 8 +++ 6 files changed, 72 insertions(+), 18 deletions(-) diff --git a/lib/openvpn-status-web/main.html.erb b/lib/openvpn-status-web/main.html.erb index e984e28..1d6b198 100644 --- a/lib/openvpn-status-web/main.html.erb +++ b/lib/openvpn-status-web/main.html.erb @@ -50,20 +50,36 @@ thead {
- - - - - +<% status.client_list_headers.each_with_index do |header,i| %> +<% if i == 0 %> + +<% end %> <% status.client_list.each do |client| %> - - - - - +<% status.client_list_headers.each_with_index do |header,i| %> +<% if i == 0 %> + +<% elsif client[i].is_a? DateTime %> + <%= client[i].strftime('%-d.%-m.%Y %H:%M:%S') %> +<% else %> + <%= client[i] %> +<% end %> +<% end %> <% end %> @@ -74,18 +90,34 @@ thead {
Common NameReal AddressData ReceivedData SentConnected Since +<% elsif i == status.client_list_headers.length-1 %> + +<% else %> + +<% end %> + <%= header %>
<%= client[0] %><%= client[1] %><%= client[2].to_i.as_bytes %><%= client[3].to_i.as_bytes %><%= client[4].strftime('%-d.%-m.%Y %H:%M:%S') %> +<% elsif i == status.client_list_headers.length-1 %> + +<% else %> + +<% end %> +<% if header =~ /(Received|Sent)/ %> + <%= client[i].to_i.as_bytes %>
- - - - +<% status.routing_table_headers.each_with_index do |header,i| %> +<% if i == 0 %> + +<% end %> <% status.routing_table.each do |route| %> - - - - +<% status.routing_table_headers.each_with_index do |header,i| %> +<% if i == 0 %> + +<% else %> + <%= route[i] %> +<% end %> +<% end %> <% end %> diff --git a/lib/openvpn-status-web/parser/modern_stateless.rb b/lib/openvpn-status-web/parser/modern_stateless.rb index b8e65fa..f27beba 100644 --- a/lib/openvpn-status-web/parser/modern_stateless.rb +++ b/lib/openvpn-status-web/parser/modern_stateless.rb @@ -11,7 +11,9 @@ module OpenVPNStatusWeb text.lines.each do |line| parts = line.strip.split(sep) + status.client_list_headers = ['Common Name', 'Real Address', 'Data Received', 'Data Sent', 'Connected Since'] status.client_list << parse_client(parts[1..5]) if parts[0] == 'CLIENT_LIST' + status.routing_table_headers = ['Virtual Address', 'Common Name', 'Real Address', 'Last Ref'] 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 diff --git a/lib/openvpn-status-web/parser/v1.rb b/lib/openvpn-status-web/parser/v1.rb index 4c806b5..f46ed9d 100644 --- a/lib/openvpn-status-web/parser/v1.rb +++ b/lib/openvpn-status-web/parser/v1.rb @@ -26,7 +26,9 @@ module OpenVPNStatusWeb end status = Status.new + status.client_list_headers = ['Common Name', 'Real Address', 'Data Received', 'Data Sent', 'Connected Since'] status.client_list = client_list[2..-1].map { |client| parse_client(client) } + status.routing_table_headers = ['Virtual Address', 'Common Name', 'Real Address', 'Last Ref'] status.routing_table = routing_table[1..-1].map { |route| parse_route(route) } status.global_stats = global_stats.map { |global| parse_global(global) } status diff --git a/lib/openvpn-status-web/status.rb b/lib/openvpn-status-web/status.rb index f9861fb..da21b48 100644 --- a/lib/openvpn-status-web/status.rb +++ b/lib/openvpn-status-web/status.rb @@ -2,7 +2,9 @@ module OpenVPNStatusWeb class Status + attr_accessor :client_list_headers attr_accessor :client_list + attr_accessor :routing_table_headers attr_accessor :routing_table attr_accessor :global_stats end diff --git a/spec/openvpn-status-web/parser/modern_stateless_spec.rb b/spec/openvpn-status-web/parser/modern_stateless_spec.rb index 2ab9417..d9e7aab 100644 --- a/spec/openvpn-status-web/parser/modern_stateless_spec.rb +++ b/spec/openvpn-status-web/parser/modern_stateless_spec.rb @@ -32,6 +32,10 @@ describe OpenVPNStatusWeb::Parser::ModernStateless do ] ) end + + it 'has the same number of headers' do + expect(status.client_list[0].length).to eq(status.client_list_headers.length) + end end context 'with routing table' do @@ -55,6 +59,10 @@ describe OpenVPNStatusWeb::Parser::ModernStateless do ] ) end + + it 'has the same number of headers' do + expect(status.routing_table[0].length).to eq(status.routing_table_headers.length) + end end it 'parses global stats' do diff --git a/spec/openvpn-status-web/parser/v1_spec.rb b/spec/openvpn-status-web/parser/v1_spec.rb index aa8088a..8a84997 100644 --- a/spec/openvpn-status-web/parser/v1_spec.rb +++ b/spec/openvpn-status-web/parser/v1_spec.rb @@ -31,6 +31,10 @@ describe OpenVPNStatusWeb::Parser::V1 do ] ) end + + it 'has the same number of headers' do + expect(status.client_list[0].length).to eq(status.client_list_headers.length) + end end context 'with routing table' do @@ -54,6 +58,10 @@ describe OpenVPNStatusWeb::Parser::V1 do ] ) end + + it 'has the same number of headers' do + expect(status.routing_table[0].length).to eq(status.routing_table_headers.length) + end end it 'parses global stats' do
Virtual AddressCommon NameReal AddressLast Ref +<% elsif i == status.routing_table_headers.length-1 %> + +<% else %> + +<% end %> + <%= header %>
<%= route[0] %><%= route[1] %><%= route[2] %><%= route[3].strftime('%-d.%-m.%Y %H:%M:%S') %> +<% elsif i == status.routing_table_headers.length-1 %> + +<% else %> + +<% end %> +<% if route[i].is_a? DateTime %> + <%= route[i].strftime('%-d.%-m.%Y %H:%M:%S') %>