From 5a2c06fbe7a6a70edd02726da17d5fd931eecd8f Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 18 Jun 2020 05:02:13 +0530 Subject: Move state notes table generator to a module --- ara.pl | 30 ++++++++++++++--------------- lib/StateNotes.pm | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 lib/StateNotes.pm diff --git a/ara.pl b/ara.pl index 0bbf062..b1766b0 100755 --- a/ara.pl +++ b/ara.pl @@ -3,6 +3,9 @@ use strict; use warnings; +use lib::relative 'lib'; +use StateNotes; + use Path::Tiny; use Time::Moment; use Text::ASCIITable; @@ -167,11 +170,7 @@ my $statewise = $json_data->{statewise}; my ( $covid_19_data, $notes_table, @months, $today ); -if ( $state_notes ) { - $notes_table = Text::ASCIITable->new( { drawRowLine => 1 } ); - $notes_table->setCols( qw( State Notes ) ); - $notes_table->setColWidth( 'Notes', 74 ); -} else { +unless ( $state_notes ) { # Map month number to Months. @months = qw( lol Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); @@ -238,12 +237,7 @@ foreach my $i ( 0 ... scalar @$statewise - 1 ) { $state = $statewise->[$i]{statecode} if length $state > 16; - if ( $state_notes ) { - $notes_table->addRow( - $state, - $statewise->[$i]{statenotes}, - ) unless length($statewise->[$i]{statenotes}) == 0; - } else { + unless ( $state_notes ) { my $update_info; my $lastupdatedtime = $statewise->[$i]{lastupdatedtime}; my $last_update_dmy = substr( $lastupdatedtime, 0, 10 ); @@ -336,11 +330,17 @@ foreach my $i ( 0 ... scalar @$statewise - 1 ) { $rows_printed++; } -die "No rows in table\n" unless $rows_printed; - -# Generate tables. if ( $state_notes ) { - print $notes_table; + my ( $state_notes_table, $rows_in_table ) = StateNotes::get( + $statewise, + \%hide, + \%show, + $rows_to_print + ); + + die "No rows in table\n" unless $rows_in_table; + print $state_notes_table; } else { + die "No rows in table\n" unless $rows_printed; print $covid_19_data; } diff --git a/lib/StateNotes.pm b/lib/StateNotes.pm new file mode 100644 index 0000000..9a3cab0 --- /dev/null +++ b/lib/StateNotes.pm @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +package StateNotes; + +use strict; +use warnings; + +use Text::ASCIITable; +use Carp qw( croak carp ); + +sub get { + my ( $statewise, $hide, $show, $rows_to_print ) = @_; + my $table = Text::ASCIITable->new( { drawRowLine => 1 } ); + $table->setCols( qw( State Notes ) ); + $table->setColWidth( 'Notes', 74 ); + + my $rows_in_table = 0; + foreach my $i ( 0 ... scalar @$statewise - 1 ) { + # $rows_printed is incremented at the end of this foreach + # loop. + if ( $rows_to_print ) { + last if $rows_in_table == $rows_to_print; + } + + my $state = $statewise->[$i]{state}; + + # If user has asked to show specific states then forget about + # hide option. + if ( scalar keys %$show ) { + next + unless exists $show->{lc $state} + or ( length $state > 16 + and exists $show->{lc $statewise->[$i]{statecode}}); + } else { + next + if exists $hide->{lc $state} + # User sees the statecode if length $state > 16 so we + # also match against that. + or ( length $state > 16 + and exists $hide->{lc $statewise->[$i]{statecode}}); + } + + $state = $statewise->[$i]{statecode} + if length $state > 16; + + unless ( length($statewise->[$i]{statenotes}) == 0 ) { + $table->addRow( + $state, + $statewise->[$i]{statenotes}, + ) ; + $rows_in_table++; + } + } + return ( $table, $rows_in_table ); +} + +1; -- cgit 1.4.1-2-gfad0