lib/Web/SIVA.pm

filter: Statementstmt 59.4% 69 / 116 Branchbran 33.3% 10 / 30 Conditioncond 78.5% 11 / 14 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 58.7% 104 / 177 CC 27 SCAR 45.8
CC
27
Cov
56%
day
127.1
new
2.0
BEGIN
1.0
CRAP 97.5
↑ Summary
1 package Web::SIVA;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use version; our $VERSION = qv('0.0.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our @provincias = qw(al ca ma gr se co hu ja);
13  
14 1 our %provincias = map { $_ => 1 } @provincias;
15  
16 1 our @meses = qw(ene feb mar abr may jun jul ago sep oct nov dic);
17  
18 1 our $base_url = "http://www.juntadeandalucia.es/medioambiente/atmosfera/informes_siva/";
19  
20 # Module implementation here
21 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
22 1 my $class = shift;
23 1 ▶ my $province = shift || croak "Necesito una provincia";
Condition50%1/2
shift() || croak('Necesito una provincia')
l!l
10
MC/DC0%0/1
shift() || croak('Necesito una provincia')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
24
25 1 return bless { _province => $province}, $class;
26  
27 }
28  
29 6 ▶sub day {
Subroutine day: called CC=26 SCAR=48.4
Pod documented
30 6 my $self = shift;
31 6 my ($dia, $mes, $year ) = @_;
32 6 my $year_digits = substr($year,2,2);
33 6 my $provincia = $self->{'_province'};
34 6 ▶ if ( ! $provincias{$provincia} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Web::SIVA::provincias{$provincia}) 0 6 6
35 0 croak "$provincia is not one of the 8 provinces";
36 }
37 6 my $date = sprintf("%02d%02d%02d",$year_digits,$mes,$dia);
38 6 my $fecha = sprintf("%04d-%02d-%02d", $year, $mes, $dia );
39 6 my @datos;
40
41 6 ▶ if ( ($year < 2004) || ( $year == 2004 && $mes == 1 && $dia < 11 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year < 2004 or $year == 2004 and $mes == 1 and $dia < 11) { } 4 2 6
Condition88%8/9
$year < 2004 or $year == 2004 and $mes == 1 and $dia < 11
l!l&&r!l&&!r
312
$year == 2004 and $mes == 1 and $dia < 11
!ll&&!rl&&r
111
$year == 2004 and $mes == 1
!ll&&!rl&&r
102
MC/DC75%3/4
$year < 2004 or $year == 2004 and $mes == 1 and $dia < 11
$year < 2004 $year == 2004 $mes == 1 $dia < 11
Truth table80%4/5
A or B and C and D
A= $year < 2004B= $year == 2004C= $mes == 1D= $dia < 11
ABCDresult
1XXX1
01111
00XX0
010X0
01100
42 4 my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.txt";
43 4 my $content = get( $url );
44 4 ▶ if ( $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 4 4
45 0 my @tables;
46 0 ▶ if ( $content =~ /Ambiental/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /Ambiental/) { } 0 0 0
47 0 @tables = ($content =~ /Ambiental\s+(.+?)\s+Nota/gs);
48 } else {
49 0 @tables = split(/\s+\n\s+\n\s+\n/, $content);
50 }
51 0 shift @tables; # unneeded first row
52 0 for my $t (@tables) {
53 0 my @lines = grep( /\S+/, split("\n", $t ) ); # Only non-empty
54 0 ▶ next if $lines[$#lines] =~ /Fecha/; # No data
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$#lines] =~ /Fecha/ 0 0 0
55 0 ▶ if ( $lines[$#lines] =~ /unidades/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$#lines] =~ /unidades/) 0 0 0
56 0 pop @lines;
57 0 pop @lines;
58 }
59 0 my $this_metadata = { date => $fecha."T00:00" };
60 0 my @metadatos;
61 0 push @metadatos, ( $lines[0] =~ /Provincia\s*:\s+(\w+)\s+Estacion\s*:\s+(.+)/ );
62 0 push @metadatos, ( $lines[1] =~ /Municipio\s*:\s+(\w+)\s+Direccion\s*:\s+(.+)/ );
63 0 for my $k (qw(provincia estacion municipio direccion)) {
64 0 $this_metadata->{$k} = shift @metadatos;
65 }
66 0 my (@cabeceras) = split( /\s+/, $lines[2]);
67 0 shift @cabeceras; #Date goes first
68 0 ▶ for (my $l = 3; $l <= $#lines; $l++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= $#lines) 0 0 0
69 0 my %these_medidas = %{$this_metadata};
70 0 my @columnas;
71 0 ▶ if ( $lines[$l] =~ /:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$l] =~ /:/) { } 0 0 0
72 0 @columnas = split( /\t/, $lines[$l]);
73 0 my $fecha_hora = shift @columnas;
74 0 my ($hora) = ($fecha_hora =~ /(\d+:\d+)/);
75 0 ▶ if ( !$hora ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hora) 0 0 0
76 0 carp "Problemas con el formato en $l $lines[$l] $fecha";
77 0 next;
78 }
79 0 $these_medidas{'date'} =~ s/00:00/$hora/;
80 } else { #Different format
81 0 my ($fecha_hora, $resto) = ($lines[$l] =~ /(\S+ \d+)\s{3}(.+)/);
82 0 ▶ if ( !$resto ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resto) 0 0 0
83 0 carp "Problemas con formato en $l => $lines[$l]";
84 }
85 0 @columnas= split(/\s{7}/, $resto);
86 0 my ($this_date, $hour) = split(/\s+/, $fecha_hora);
87 0 my ($this_day,$mon,$year) = split("/", $this_date);
88 0 $these_medidas{'date'} = sprintf("%04d-%02d-%02dT%02d:00", $year+1900,$mon,$this_day,$hour);
89 }
90 0 for my $c ( @cabeceras ) {
91 0 $these_medidas{$c} = shift @columnas;
92 0 ▶ next if !$these_medidas{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $these_medidas{$c} 0 0 0
93 0 $these_medidas{$c} =~ s/\.//;
94 0 $these_medidas{$c} =~ s/,/./;
95 0 $these_medidas{$c} = 0 + $these_medidas{$c};
96 }
97 0 push @datos, \%these_medidas;
98 }
99 }
100 }
101 } else {
102 2 my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.htm";
103
104 2 my $content = get( $url );
105
106 2 ▶ if ( $content and $content =~ m{$year</title} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content and $content =~ m[$year</title]) 1 1 2
Condition66%2/3
$content and $content =~ m[$year</title]
!ll&&!rl&&r
101
MC/DC50%1/2
$content and $content =~ m[$year</title]
$content $content =~ m[$year</title]
Truth table66%2/3
A and B
A= $contentB= $content =~ m[$year</title]
ABresult
0X0
100
111
107 1 my $dom = Mojo::DOM->new( $content );
108
109 1 my @tables = $dom->find('table')->each;
110
111 1 shift @tables; #Primera tabla con leyenda
112
113 1 ▶ while ( @tables ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tables) 4 1 5
114 4 my $metadatos = shift @tables;
115 4 ▶ next if !@tables;
Branch: 1 branch
BranchTrueFalseTotal
unless @tables 0 4 4
116 4 my $medidas = shift @tables;
117
118 4 my @metadatos = ( $metadatos =~ /<b>.([A-Z][^<]+)/g);
119 4 my $this_metadata = { date => $fecha };
120 4 for my $k (qw(provincia municipio estacion direccion)) {
121 16 $this_metadata->{$k} = shift @metadatos;
122 }
123
124 4 my @filas = $medidas->find('tr')->each;
125
126 4 shift @filas; #Cabecera
127 4 pop @filas;
128 4 for my $f (@filas) {
129 576 my @columnas = $f->find('td')->map('text')->each;
130 576 my %these_medidas = %{$this_metadata};
131 576 my $fecha_hora = shift @columnas;
132 576 my ($hora) = ($fecha_hora =~ /(\d+:\d+)/);
133 576 ▶ if ( !$hora ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hora) 0 576 576
134 0 carp "Problemas con el formato en $f $fecha";
135 }
136 576 $these_medidas{'date'} =~ s/00:00/$hora/;
137 576 for my $c (qw(SO2 PART NO2 CO O3)) {
138 2880 $these_medidas{$c} = shift @columnas;
139 }
140 576 push @datos, \%these_medidas;
141 }
142 }
143 }
144 }
145 6 return \@datos;
146 }
147  
148 1 "We want air"; # Magic true value required at end of module
149 __END__
↑ Summary