gem: refactor to prepare supporting flexible list headers

This commit is contained in:
cn 2022-01-17 11:37:50 +01:00 committed by Christian Nicolai
parent 406ed867f7
commit 39d2b3ce94
6 changed files with 72 additions and 18 deletions

View File

@ -50,20 +50,36 @@ thead {
<div>
<table>
<thead>
<td class="first">Common Name</td>
<td class="middle">Real Address</td>
<td class="middle">Data Received</td>
<td class="middle">Data Sent</td>
<td class="last">Connected Since</td>
<% status.client_list_headers.each_with_index do |header,i| %>
<% if i == 0 %>
<td class="first">
<% elsif i == status.client_list_headers.length-1 %>
<td class="last">
<% else %>
<td class="middle">
<% end %>
<%= header %></td>
<% end %>
</thead>
<tbody>
<% status.client_list.each do |client| %>
<tr>
<td class="first"><%= client[0] %></td>
<td class="middle"><%= client[1] %></td>
<td class="middle"><%= client[2].to_i.as_bytes %></td>
<td class="middle"><%= client[3].to_i.as_bytes %></td>
<td class="last"><%= client[4].strftime('%-d.%-m.%Y %H:%M:%S') %></td>
<% status.client_list_headers.each_with_index do |header,i| %>
<% if i == 0 %>
<td class="first">
<% elsif i == status.client_list_headers.length-1 %>
<td class="last">
<% else %>
<td class="middle">
<% end %>
<% if header =~ /(Received|Sent)/ %>
<%= client[i].to_i.as_bytes %></td>
<% elsif client[i].is_a? DateTime %>
<%= client[i].strftime('%-d.%-m.%Y %H:%M:%S') %></td>
<% else %>
<%= client[i] %></td>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
@ -74,18 +90,34 @@ thead {
<div>
<table>
<thead>
<td class="first">Virtual Address</td>
<td class="middle">Common Name</td>
<td class="middle">Real Address</td>
<td class="last">Last Ref</td>
<% status.routing_table_headers.each_with_index do |header,i| %>
<% if i == 0 %>
<td class="first">
<% elsif i == status.routing_table_headers.length-1 %>
<td class="last">
<% else %>
<td class="middle">
<% end %>
<%= header %></td>
<% end %>
</thead>
<tbody>
<% status.routing_table.each do |route| %>
<tr>
<td class="first"><%= route[0] %></td>
<td class="middle"><%= route[1] %></td>
<td class="middle"><%= route[2] %></td>
<td class="last"><%= route[3].strftime('%-d.%-m.%Y %H:%M:%S') %></td>
<% status.routing_table_headers.each_with_index do |header,i| %>
<% if i == 0 %>
<td class="first">
<% elsif i == status.routing_table_headers.length-1 %>
<td class="last">
<% else %>
<td class="middle">
<% end %>
<% if route[i].is_a? DateTime %>
<%= route[i].strftime('%-d.%-m.%Y %H:%M:%S') %></td>
<% else %>
<%= route[i] %></td>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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