AzimuthAero/Utils.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« AzimuthAero/RouteMap.pm ↑ Summary AzimuthAero.pm »
1 package WWW::AzimuthAero::Utils;
2 $WWW::AzimuthAero::Utils::VERSION = '0.2';
3  
4 # ABSTRACT: functions that can be used outside WWW::AzimuthAero::* packages
5  
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use JavaScript::V8;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use DateTime;
16 use DateTime::Format::Strptime;
17  
18 require Exporter;
19 our @ISA = qw(Exporter);
20  
21 our @EXPORT_OK = qw(
22 get_next_dow_date
23 get_next_dow_date_dmy
24 get_dates_from_dows
25 get_dates_from_range
26 filter_dates
27 sort_dates
28 extract_js_glob_var
29 iata_pairwise
30 fix_html_string
31 );
32  
33 # TO-DO: to_dt and from_dt methods
34  
35 our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] );
36  
37  
38 sub get_next_dow_date {
39 my ( $date, $dow, $pattern ) = @_;
40  
41 $pattern = '%d.%m.%Y' unless defined $pattern;
42  
43 unless ( ref($date) eq 'DateTime' ) {
44 $date = DateTime::Format::Strptime->new( pattern => $pattern )
45 ->parse_datetime($date);
46 }
47  
48 my $days_diff = $dow - $date->day_of_week;
49 my $dur = DateTime::Duration->new( days => $days_diff );
50 return $date if ( $dur->is_zero );
51 return $date->add_duration($dur) if ( $dur->is_positive );
52 return $date->add( days => 7 - $dur->days ) if ( $dur->is_negative );
53 }
54  
55 sub get_next_dow_date_dmy {
56 my ( $date, $dow, $pattern ) = @_;
57 get_next_dow_date( $date, $dow, $pattern )->dmy('.');
58 }
59  
60  
61 sub get_dates_from_dows {
62 my (%params) = @_;
63  
64 #warn "get_dates() params : ".Dumper \%params;
65 confess "max date is not defined" unless defined $params{max};
66 confess "days of week (days variable) are not defined"
67 unless defined $params{days};
68  
69 my @wdays = split( '', $params{days} );
70  
71 #say "wdays: ".Dumper \@wdays;
72  
73 # TO-DO: $dt_max: 4 month further
74 my $dt_max = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' )
75 ->parse_datetime( $params{max} );
76  
77 my $dt_min =
78 ( defined $params{min} )
79 ? ( DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' )
80 ->parse_datetime( $params{min} ) )
81 : ( DateTime->now->truncate( to => 'day' ) );
82  
83 # $dt_min = DateTime->now if ( $dt_min <= DateTime->now );
84  
85 my @res = ();
86 for my $dow (@wdays) {
87 my $next_target_dt = get_next_dow_date( $dt_min, $dow );
88 while ( $next_target_dt <= $dt_max ) {
89  
90 # push @res, $next_target_dt; # strange behaviour if push datetime
91 push @res, $next_target_dt->dmy('.');
92 $next_target_dt =
93 $next_target_dt + DateTime::Duration->new( weeks => 1 );
94 }
95 }
96  
97 return sort_dates(@res);
98 }
99  
100 # This method used for filtering dates when no available_to property
101  
102 sub get_dates_from_range {
103 my (%params) = @_;
104  
105 # confess "min date is not defined" unless defined $params{min};
106 # confess "max date is not defined" unless defined $params{max};
107  
108 my $dt_max =
109 ( defined $params{max} )
110 ? ( DateTime::Format::Strptime->new( pattern => '%d.%m.%Y' )
111 ->parse_datetime( $params{max} ) )
112 : ( DateTime->now->truncate( to => 'day' )->add( months => 2 ) );
113  
114 # my $dt_max = DateTime::Format::Strptime->new( pattern => '%d.%m.%Y' )
115 #->parse_datetime( $params{max} );
116  
117 # DateTime->now->truncate include max_day
118 my $dt_min =
119 ( defined $params{min} )
120 ? ( DateTime::Format::Strptime->new( pattern => '%d.%m.%Y' )
121 ->parse_datetime( $params{min} ) )
122 : ( DateTime->now->truncate( to => 'day' ) );
123  
124 my $next_dt = $dt_min; ### TO-DO: round to midnight, '%Y-%m-%d 00:00:00'
125 my @res;
126  
127 while ( DateTime->compare( $dt_max, $next_dt ) >= 0 ) {
128 push @res, $next_dt->dmy('.');
129 $next_dt = $next_dt->add( days => 1 );
130 }
131  
132 return @res;
133  
134 }
135  
136  
137 sub sort_dates {
138 my @dates = @_;
139 my $pattern = '%d.%m.%Y';
140  
141 return map { $_->dmy('.') }
142 sort { DateTime->compare( $a, $b ) }
143 map {
144 DateTime::Format::Strptime->new( pattern => $pattern )
145 ->parse_datetime($_)
146 } @dates;
147 }
148  
149  
150 sub filter_dates {
151 my ( $dates, %params ) = @_;
152 my $pattern = '%d.%m.%Y';
153  
154 confess "max date is not defined" unless defined $params{max};
155 $params{max} = DateTime::Format::Strptime->new( pattern => $pattern )
156 ->parse_datetime( $params{max} );
157 $params{min} =
158 ( defined $params{min} )
159 ? ( DateTime::Format::Strptime->new( pattern => $pattern )
160 ->parse_datetime( $params{min} ) )
161 : ( DateTime->now() );
162  
163 return map { $_->dmy('.') }
164 grep { $_ >= $params{min} && $_ <= $params{max} }
165 map {
166 DateTime::Format::Strptime->new( pattern => $pattern )
167 ->parse_datetime($_)
168 } @$dates;
169 }
170  
171  
172 sub extract_js_glob_var {
173 my ( $code_str, $var_name ) = @_;
174 my $res;
175 my $context = JavaScript::V8::Context->new();
176 $context->eval($code_str);
177 $context->bind( console_log => sub { $res = $_[0] } );
178 $context->eval( 'console_log(' . $var_name . ')' );
179 undef $context;
180 return $res;
181 }
182  
183  
184 sub fix_html_string {
185 my ($html_str) = @_;
186 my $str = $html_str;
187 $str =~ s/[\r\n\t]//g;
188 $str =~ s/^\s+//;
189 $str =~ s/\s+$//;
190  
191 # hack for deleting &nbsp; at flight_num
192 $str =~ s/([A-Z1-9]).(\d{3})/$1 $2/;
193  
194 return $str;
195 }
196  
197  
198 sub iata_pairwise {
199 my ($AoA) = @_;
200 my @res;
201 for my $x (@$AoA) {
202 push @res, { from => $x->[0], via => $x->[1], to => $x->[2] };
203 }
204 return @res;
205 }
206  
207 1;
208  
209 __END__
210  
211 =pod
212  
213 =encoding UTF-8
214  
215 =head1 NAME
216  
217 WWW::AzimuthAero::Utils - functions that can be used outside WWW::AzimuthAero::* packages
218  
219 =head1 VERSION
220  
221 version 0.2
222  
223 =head1 DESCRIPTION
224  
225 Some useful date manipulation and filtering functions
226  
227 =head1 get_next_dow_date
228  
229 Get next dow-date following by specified date
230  
231 By default all input and output dates in '%d.%m.%Y' format, but you can easily specified needed one or deturn DateTime object
232  
233 get_next_dow_date( '7.06.2019', 7 )->dmy('.'); # '9.06.2019'
234 get_next_dow_date( '7.06.2019', 3 )->dmy('.'); # '12.06.2019'
235  
236 =head1 get_dates_from_dows
237  
238 Get particular dates, based on min_date, max_date and days_of_week
239  
240 min_date and max_date are in '%Y-%m-%d' format by default
241  
242 get_dates_from_dows( min => '2019-06-01', max => '2019-10-26', 'days' => '16' );
243  
244 Return sorted array
245  
246 TO-DO: carefully check for max day without min day (result may not include it)
247  
248 =head1 filter_dates
249  
250 Sort dates ascending
251  
252 =head1 filter_dates
253  
254 Filter dates by max and min dates
255  
256 filter_dates( \@dates, max => '15.06.2019' );
257 filter_dates( \@dates, max => '15.06.2019', min => '07.06.2019' );
258  
259 =head1 extract_js_glob_var
260  
261 Extract global variable value from JavaScript code
262  
263 =head1 fix_html_string
264  
265 remove newline symbols, leading and trailing whitespaces
266  
267 =head1 pairwise
268  
269 Transform
270  
271 [ [ 'ROV', 'MOW', 'LED' ], [ 'ROV', 'KRR', 'LED' ] ]
272  
273 to
274  
275 [
276 { from => 'ROV', via => 'MOW', to => 'LED' },
277 { from => 'ROV', via => 'KRR', to => 'LED' }
278 ]
279  
280 =head1 AUTHOR
281  
282 Pavel Serikov <pavelsr@cpan.org>
283  
284 =head1 COPYRIGHT AND LICENSE
285  
286 This software is copyright (c) 2019 by Pavel Serikov.
287  
288 This is free software; you can redistribute it and/or modify it under
289 the same terms as the Perl 5 programming language system itself.
290  
291 =cut
« AzimuthAero/RouteMap.pm ↑ Summary AzimuthAero.pm »