Opendata/GTFS/Feed.pm

filter: Statementstmt 94.3% 166 / 176 Branchbran 57.1% 24 / 42 Conditioncond 50.0% 6 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 38 / 38 Podpod 0.0% 0 / 4 totaltotal 84.5% 236 / 279 CC 25 SCAR 19.1
CC
25
Cov
85%
parse_file
17.5
__ANON__
14.2
BUILD
4.0
CRAP 6.8
↑ Summary Opendata/GTFS/Type/Calendar.pm »
1 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Opendata::GTFS::Feed;
6  
7 # ABSTRACT: Parse General Transit Feeds (GTFS)
8 3 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 3 our $VERSION = '0.0202';
10  
11 3 ▶use Opendata::GTFS::Feed::Elk;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Archive::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Lingua::EN::Inflect;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use File::BOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Opendata::GTFS::Type::Agency;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Opendata::GTFS::Type::Calendar;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Opendata::GTFS::Type::CalendarDate;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Opendata::GTFS::Type::FareAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Opendata::GTFS::Type::FareRule;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Opendata::GTFS::Type::Frequency;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Opendata::GTFS::Type::Route;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Opendata::GTFS::Type::Shape;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Opendata::GTFS::Type::Stop;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Opendata::GTFS::Type::StopTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Opendata::GTFS::Type::Transfer;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Opendata::GTFS::Type::Trip;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Types::Opendata::GTFS -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 ▶use List::UtilsBy qw/zip_by/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use MooseX::AttributeDocumented;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Types::Path::Tiny qw/AbsPath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use Types::URI qw/Uri/;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use Types::Standard qw/ArrayRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 3 has file => (
41 is => 'ro',
42 isa => AbsPath,
43 required => 0,
44 coerce => 1,
45 documentation_order => 1,
46 documentation => q{If file is given, the feed in the file will be parsed.},
47 );
48 3 has directory => (
49 is => 'ro',
50 isa => AbsPath,
51 required => 0,
52 coerce => 1,
53 documentation_order => 3,
54 documentation => q{If only directory is given, it is expected to find a fully extracted feed in that directory. If C<directory> is given together with either file or url, the feed will be extracted into that directory (and remain there).},
55 );
56 3 has url => (
57 is => 'ro',
58 isa => Uri,
59 required => 0,
60 coerce => 1,
61 documentation_order => 1,
62 documentation => q{If url is given, the feed at the url will be fetched and parsed.},
63 );
64  
65 3 my @attributes = (
66 Agency, 1 => 'agency.txt',
67 Stop, 1 => 'stops.txt',
68 Route, 1 => 'routes.txt',
69 Trip, 1 => 'trips.txt',
70 StopTime, 1 => 'stop_times.txt',
71 Calendar, 1 => 'calendar.txt',
72 CalendarDate, 0 => 'calendar_dates.txt',
73 FareAttribute, 0 => 'fare_attributes.txt',
74 FareRule, 0 => 'fare_rules.txt',
75 Shape, 0 => 'shapes.txt',
76 Frequency, 0 => 'frequencies.txt',
77 Transfer, 0 => 'transfers.txt',
78 );
79  
80 118 ▶sub type_to_singular {
Subroutine type_to_singular: called CC=1 SCAR=0.0
Pod undocumented
81 118 my $type = shift;
82  
83 118 my $name = $type->name;
84 118 $name =~ s{(?<=[a-z])([A-Z])}{_$1}g;
85 118 return lc $name;
86 }
87 59 ▶sub type_to_plural {
Subroutine type_to_plural: called CC=1 SCAR=0.0
Pod undocumented
88 59 my $type = shift;
89  
90 59 my @names = split /_/ => type_to_singular($type);
91 59 $names[-1] = Lingua::EN::Inflect::PL($names[-1]);
92 59 return join '_' => @names;
93 }
94  
95 3 ▶for (my $i = 0; $i < $#attributes; $i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#attributes) 36 3 39
96 36 my $type = $attributes[$i];
97 36 my $attribute = type_to_plural($type);
98 36 my $singular = type_to_singular($type);
99  
100 has $attribute => (
101 is => 'ro',
102 isa => ArrayRef[ $type ],
103 traits => ['Array'],
104 24 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 36 init_arg => undef,
106 handles => {
107 "add_$singular" => 'push',
108 "all_$attribute" => 'elements',
109 "count_$attribute" => 'count',
110 },
111 documentation_default => '[]',
112 );
113 }
114  
115 2 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=10 SCAR=26.5
116 2 my $orig = shift;
117 2 my $self = shift;
118 2 my @args = @_;
119  
120 2 my %args = @args;
121 2 ▶ if(!exists $args{'directory'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"directory"}) 0 2 2
122 0 $args{'directory'} = File::Temp->newdir;
123 }
124 2 $args{'directory'} = path($args{'directory'})->absolute;
125 2 ▶ if(exists $args{'file'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'file'}) { } 0 2 2
elsif (exists $args{'url'}) { } 1 1 2
126 0 ▶ if(path($args{'file'})->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if (path($args{'file'})->exists) { } 0 0 0
127 0 $args{'file'} = path($args{'file'})->absolute;
128 0 my $x = Archive::Extract->new(archive => $args{'file'}->stringify);
129 0 ▶ $x->extract(to => $args{'directory'}->stringify) or die $x->error;
Branch: 1 branch
BranchTrueFalseTotal
$x->extract("to", $args{"directory"}->stringify) or die $x->error 0 0 0
130 }
131 else {
132 0 die sprintf 'Supplied filepath (%s) does not exist.', $args{'file'};
133 }
134 }
135 elsif(exists $args{'url'}) {
136 1 ▶ eval "use HTTP::Tiny";
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 ▶ die "Passing 'url' to Opendata::GTFS::Feed->new requires HTTP::Tiny" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
138  
139 1 my $response = HTTP::Tiny->new->get($args{'url'});
140  
141 1 ▶ die sprintf "Can't download %s: %s", $args{'url'}, join (' - ' => $response->{'status'}, $response->{'reason'}) if !$response->{'success'};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 1 1
142  
143 1 my $filename = $args{'url'};
144 1 $filename =~ s{/?\?.*}{};
145 1 $filename =~ s{.*/([^/]*)$}{$1};
146 1 ▶ $filename .= '.zip' if index ($filename, '.') == -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($filename, ".") == -1 0 1 1
147  
148 1 $args{'directory'}->child($filename)->spew($response->{'content'});
149  
150 1 my $x = Archive::Extract->new(archive => $args{'directory'}->child($filename)->stringify);
151 1 ▶ $x->extract(to => $args{'directory'}->stringify) or die $x->error;
Branch: 1 branch
BranchTrueFalseTotal
$x->extract("to", $args{"directory"}->stringify) or die $x->error 1 0 1
152 }
153 2 $self->$orig(%args);
154 3 };
155  
156 2 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
157 2 my $self = shift;
158  
159 FILE:
160 2 ▶ for (my $i = 0; $i < $#attributes; $i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#attributes) 24 2 26
161 24 my $type = $attributes[$i];
162 24 my $is_required = $attributes[$i + 1];
163 24 my $filename = $attributes[$i + 2];
164  
165 24 ▶ if(!$self->directory->child($filename)->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->directory->child($filename)->exists) 1 23 24
166 1 ▶ next FILE if !$is_required;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_required 1 0 1
167 }
168 23 $self->parse_file($type, $filename);
169  
170 23 my $plural = type_to_plural($type);
171 23 my $method = "count_$plural";
172 }
173 }
174  
175 23 ▶sub parse_file {
Subroutine parse_file: called CC=13 SCAR=28.6
Pod undocumented
176 23 my $self = shift;
177 23 my $type = shift;
178 23 my $filename = shift;
179  
180 23 my $method = sprintf 'add_%s', type_to_singular($type);
181 23 my $class = sprintf 'Opendata::GTFS::Type::%s', $type->name;
182  
183 23 my $csv = Text::CSV->new( { binary => 1 } );
184 23 my $fh;
185 23 File::BOM::open_bom($fh, $self->directory->child($filename), ':utf8');
186  
187 23 my $column_names = $csv->getline($fh);
188 23 ▶ if(!defined $column_names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $column_names) 0 23 23
189 0 die sprintf "Can't read the first line of the file. Check %s for errors.", $self->directory->child($filename);
190 }
191 23 my @column_names = @{ $column_names };
192  
193 # Google's example feed (https://developers.google.com/transit/gtfs/examples/gtfs-feed / https://developers.google.com/transit/gtfs/examples/sample-feed.zip)
194 # has a (reported) bug. This fixes that.
195 23 ▶ if($type->name eq 'StopTime' && any { $_ eq 'drop_off_time' } @column_names) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if ($type->name eq "StopTime" and &any(sub { $_ eq "drop_off_time"; } , @column_names)) 0 23 23
Condition66%2/3
$type->name eq "StopTime" and &any(sub { $_ eq "drop_off_time"; } , @column_names)
!ll&&!rl&&r
2120
MC/DC0%0/2
$type->name eq "StopTime" and &any(sub { $_ eq "drop_off_time"; } , @column_names)
$type->name eq "StopTime" &any(sub { $_ eq "drop_off_time"; } , @column_names)
Truth table66%2/3
A and B
A= $type->name eq "StopTime"B= &any(sub { $_ eq "drop_off_time"; } , @column_names)
ABresult
0X0
100
111
196 0 my $index = first_index { $_ eq 'drop_off_time'} @column_names;
197  
198 0 ▶ $column_names[ $index ] = 'drop_off_type' if $index >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= 0 0 0 0
199 }
200  
201 LINE:
202 23 while(1) {
203 153 my $line = $csv->getline($fh);
204 153 ▶ last LINE if $csv->eof && !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if $csv->eof and not defined $line 1 152 153
Condition100%3/3
$csv->eof and not defined $line
!ll&&!rl&&r
130221
MC/DC100%2/2
$csv->eof and not defined $line
$csv->eof not defined $line
Truth table100%3/3
A and not(B)
A= $csv->eofB= not defined $line
ABresult
0X0
100
111
205 152 ▶ next LINE if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 152 152
206 152 ▶ next LINE if scalar @{ $line } == 1 && (!defined $line->[0] || $line->[0] eq ''); # skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$line;} == 1 and !defined($line->[0]) || $line->[0] eq "" 0 152 152
Condition16%1/6
scalar @{$line;} == 1 and !defined($line->[0]) || $line->[0] eq ""
!ll&&!rl&&r
15200
not defined $line->[0] or $line->[0] eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
scalar @{$line;} == 1 and !defined($line->[0]) || $line->[0] eq ""
scalar @{$line;} == 1 not defined $line->[0] $line->[0] eq ""
Truth table25%1/4
A and not(B) or C
A= scalar @{$line;} == 1B= not defined $line->[0]C= $line->[0] eq ""
ABCresult
0XX0
1000
11X1
1011
207  
208 152 ▶ my @args = zip_by { @_ } \@column_names, $line;
Subroutine __ANON__: called CC=1 SCAR=0.0
209 152 $self->$method($class->new(@args));
210  
211 152 ▶ last LINE if $csv->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $csv->eof 22 130 152
212 }
213  
214 23 ▶ close $fh or die sprintf "Can't close %s", $self->directory->child($filename);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die sprintf("Can't close %s", $self->directory->child($filename)) 23 0 23
215 }
216  
217 3 __PACKAGE__->meta->make_immutable;
218  
219 3 1;
220  
221 __END__
↑ Summary Opendata/GTFS/Type/Calendar.pm »