AzimuthAero/RouteMap.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« AzimuthAero/Mock.pm ↑ Summary AzimuthAero/Utils.pm »
1 package WWW::AzimuthAero::RouteMap;
2 $WWW::AzimuthAero::RouteMap::VERSION = '0.2';
3  
4 # ABSTRACT: Route map representation
5  
6  
7 1 ▶use utf8; # important cause of L<WWW::AzimuthAero::RouteMap/neighbor_airports>
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Util qw/uniq first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use WWW::AzimuthAero::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Graph;
12 use Data::Dumper;
13  
14  
15 sub new {
16 my ( $self, $rm_raw ) = @_;
17  
18 confess "Wrong raw route map structure, not a hash"
19 unless ( ref($rm_raw) eq 'HASH' );
20  
21 bless { raw => $rm_raw }, $self;
22 }
23  
24  
25 sub raw {
26 return shift->{raw};
27 }
28  
29  
30 sub all_cities {
31 my ($self) = @_;
32 my @res;
33  
34 for my $v ( values %{ $self->raw } ) {
35 push @res, { NAME => $v->{NAME}, IATA => $v->{IATA} };
36 }
37  
38 return sort { lc( $a->{NAME} ) cmp lc( $b->{NAME} ) } @res;
39 }
40  
41  
42 sub get {
43 my ( $self, $what, $by, $val ) = @_;
44 my $city = first { $_->{$by} eq $val } $self->all_cities;
45 return $city->{$what};
46 }
47  
48 sub get_iata_by_azo {
49 my ( $self, $azo_code ) = @_;
50 return $self->raw->{$azo_code}{IATA};
51 }
52  
53  
54 sub route_map_iata {
55 my ($self) = @_;
56 my $res = {};
57 while ( my ( $azo_code, $data ) = each( %{ $self->raw } ) ) {
58 $res->{ $self->get_iata_by_azo($azo_code) } = [
59 sort { lc($a) cmp lc($b) }
60 map { $self->get_iata_by_azo($_) }
61 keys %{ $self->raw->{$azo_code}{ROUTES} }
62 ];
63 }
64 return $res;
65 }
66  
67  
68 sub neighbor_airports {
69 return {
70 'Ростов-на-Дону' => [qw/Краснодар/], # KRR
71 'Москва' => [qw/Калуга/], # KLG
72 'Санкт-Петербург' => [qw/Псков/] # PKV
73 };
74 }
75  
76  
77 sub get_neighbor_airports_iata {
78 my ( $self, $city_iata ) = @_;
79  
80 my $city = first { $_->{IATA} eq $city_iata } $self->all_cities;
81 return
82 map { $self->get( 'IATA', 'NAME', $_ ) }
83 @{ $self->neighbor_airports->{ $city->{NAME} } };
84  
85 }
86  
87  
88 sub transfer_routes {
89 my ( $self, %params ) = @_;
90  
91 my $raw = $self->route_map_iata();
92  
93 # warn "IATA map : ".Dumper $raw;
94  
95 my $g = Graph::Undirected->new;
96  
97 while ( my ( $from, $destinations ) = each(%$raw) ) {
98 for my $to (@$destinations) {
99 $g->add_edge( $from, $to ); # add_weighted_edge if price
100 }
101 }
102  
103 # print $g; # OK
104  
105 # внутри могут быть уже маршруты с пересадками, внимательнее!
106 return $self->_all_paths_btw_vertexes_w_l2(
107 graph => $g,
108 v1 => $params{from},
109 v2 => $params{to}
110 );
111  
112 }
113  
114 sub _all_paths_btw_vertexes_w_l2 {
115 my ( $sef, %params ) = @_;
116  
117 my $g = $params{graph};
118 my @res;
119  
120 #my ( $v1, $v2, $max_path_length ) = @_;
121 for my $neighbour_of_v1 ( $g->neighbours( $params{v1} ) ) {
122 next if $params{v2} eq $neighbour_of_v1; # direct route
123 for
124 my $neighbour_of_neighbour_of_v1 ( $g->neighbours($neighbour_of_v1) )
125 {
126 next
127 if $params{v1} eq
128 $neighbour_of_neighbour_of_v1; # ignore backlink to start
129 push @res, [ $params{v1}, $neighbour_of_v1, $params{v2} ]
130 if $params{v2} eq $neighbour_of_neighbour_of_v1;
131 }
132 }
133  
134 return [ uniq @res ];
135 }
136  
137 1;
138  
139 __END__
140  
141 =pod
142  
143 =encoding UTF-8
144  
145 =head1 NAME
146  
147 WWW::AzimuthAero::RouteMap - Route map representation
148  
149 =head1 VERSION
150  
151 version 0.2
152  
153 =head1 SYNOPSIS
154  
155 my $az = WWW::AzimuthAero->new();
156 $az->get( from => 'ROV', to => 'LED', date => '14.06.2019' );
157 $az->get_lowest_fares( from => 'ROV', to => 'LED', max_date => '14.08.2019' )->print;
158  
159 =head1 DESCRIPTION
160  
161 https://azimuth.aero/ru/about/flight-map
162  
163 =head1 new
164  
165 my $rm = WWW::AzimuthAero::RouteMap->new($route_map_raw_hash);
166  
167 =head1 new
168  
169 Return hash with original route map parsed from site
170  
171 =head1 all_cities
172  
173 Return sorted list of city names in route map
174  
175 print $rm->all_cities()
176 print join(',' map { "\'". $_ ."\'" } $rm->all_cities() ); # for json array
177  
178 =head1 get
179  
180 Universal accessor function for route map, wrapper under L<WWW::AzimuthAero::RouteMap/all_cities>
181  
182 $rm->get( $which_property, $by_what_property, $what_property_val )
183  
184 Examples:
185  
186 $rm->get('IATA', 'NAME', 'Ростов-на-Дону')
187 $rm->get('IATA', 'AZO', 'РОВ')
188  
189 =head1 route_map_iata
190  
191 Return hash with IATA route map
192  
193 perl -Ilib -MWWW::AzimuthAero -MData::Dumper -e 'my $x = WWW::AzimuthAero->new->route_map->route_map_iata; warn Dumper $x;'
194  
195 Amount of cities
196  
197 my $x = WWW::AzimuthAero->new->route_map->route_map_iata; print scalar values %$x;
198  
199 Amount of all routes
200  
201 perl -Ilib -MWWW::AzimuthAero -e 'my $x = WWW::AzimuthAero->new->route_map->route_map_iata; my $i = 0; $i+= scalar @$_ for values %$x; print $i;'
202  
203 =head1 neighbor_airports
204  
205 Return hash of airports that are no more than 4 hours by train from each other
206  
207 For now it's manually hardcoded
208  
209 {
210 'Ростов-на-Дону' => [qw/Краснодар/],
211 'Москва' => [qw/Калуга/],
212 'Санкт-Петербург' => [qw/Псков/]
213 };
214  
215 Cities are set by name, not IATA code, for convenience
216  
217 When you set new city please check it's availability at L<WWW::AzimuthAero::RouteMap/all_cities>
218  
219 TO-DO: check correctness with Yandex Maps API and RZD API
220  
221 =head1 get_neighbor_airports_iata
222  
223 Return list of IATA codes of neighbor airports based on L<WWW::AzimuthAero::RouteMap/neighbor_airports>
224  
225 $rm->get_neighbor_airports_iata('LED') # ( 'PKV' )
226  
227 =head1 transfer_routes
228  
229 Convert route map to L<Graph> object and return ARRAYref of routes with one transfer maximum
230  
231 perl -Ilib -MData::Dumper -MWWW::AzimuthAero -e 'my $x = WWW::AzimuthAero->new->route_map->transfer_routes; warn Dumper $x;'
232  
233 Params:
234  
235 # graph processing options :
236 # $params{max_edges} - 2 by default, hardcoded for now
237 # $params{check_neighbors}
238  
239 # source and destination cities :
240 # $params{from} +
241 # $params{to} +
242  
243 # schedule processing :
244 # $params{min}
245 # $params{max}
246 # $params{max_delay_days}
247  
248 =head1 AUTHOR
249  
250 Pavel Serikov <pavelsr@cpan.org>
251  
252 =head1 COPYRIGHT AND LICENSE
253  
254 This software is copyright (c) 2019 by Pavel Serikov.
255  
256 This is free software; you can redistribute it and/or modify it under
257 the same terms as the Perl 5 programming language system itself.
258  
259 =cut
« AzimuthAero/Mock.pm ↑ Summary AzimuthAero/Utils.pm »