lib/Web/SIVA.pm

filter: Statementstmt 73.4% 69 / 94 Branchbran 55.5% 10 / 18 Conditioncond 73.3% 11 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 71.7% 104 / 145 SCAR 34.6
CC
21
Cov
71%
day
39.4
new
2.1
BEGIN
1.0
CRAP 31.9
« blib/lib/Web/SIVA.pm ↑ 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.5');
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 sub new {
22 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.6
Pod documented
23 1 ▶ my $province = shift || croak "Necesito una provincia";
Condition33%1/3
shift() || croak('Necesito una provincia')
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || croak('Necesito una provincia')
shift() croak('Necesito una provincia')
Truth table33%1/3
A || B
A= shift()B= croak('Necesito una provincia')
ABresult
1X1
011
000
24
25 1 return bless { _province => $province}, $class;
26  
27 }
28  
29 sub day {
30 4 ▶ my $self = shift;
Subroutine day: called CC=20 SCAR=36.7
Pod documented
31 4 my ($dia, $mes, $year ) = @_;
32 4 my $year_digits = substr($year,2,2);
33 4 my $provincia = $self->{'_province'};
34 4 ▶ if ( ! $provincias{$provincia} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Web::SIVA::provincias{$provincia}) 0 4 4
35 0 croak "$provincia is not one of the 8 provinces";
36 }
37 4 my $date = sprintf("%02d%02d%02d",$year_digits,$mes,$dia);
38 4 my $fecha = sprintf("%04d-%02d-%02d", $year, $mes, $dia );
39 4 my @datos;
40
41 4 ▶ 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) { } 2 2 4
Condition88%8/9
$year < 2004 or $year == 2004 and $mes == 1 and $dia < 11
l!l&&r!l&&!r
112
$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 2 my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.txt";
43 2 my $content = get( $url );
44 2 ▶ if ( $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 2 2
45 0 my @tables = split(/\s+\n\s+\n\s+\n/, $content);
46 0 shift @tables; # unneeded first row
47 0 for my $t (@tables) {
48 0 my @lines = split("\n", $t );
49 0 my $this_metadata = { date => $fecha."T00:00" };
50 0 my @metadatos;
51 0 push @metadatos, ( $lines[0] =~ /Provincia:\s+(\w+)\s+Estacion:\s+(.+)/ );
52 0 push @metadatos, ( $lines[1] =~ /Municipio:\s+(\w+)\s+Direccion:\s+(.+)/ );
53 0 for my $k (qw(provincia estacion municipio direccion)) {
54 0 $this_metadata->{$k} = shift @metadatos;
55 }
56 0 ▶ for (my $l = 5; $l <= $#lines-4; $l++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= $#lines - 4) 0 0 0
57 0 my %these_medidas = %{$this_metadata};
58 0 my @columnas = split(/\s+/, $lines[$l]);
59 0 my $fecha_hora = shift @columnas;
60 0 my ($hora) = ($fecha_hora =~ /(\d+:\d+)/);
61 0 ▶ if ( !$hora ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hora) 0 0 0
62 0 carp "Problemas con el formato en $l $lines[$l] $fecha";
63 0 next;
64 }
65 0 $these_medidas{'date'} =~ s/00:00/$hora/;
66 0 for my $c (qw(SO2 PART NO2 CO)) {
67 0 $these_medidas{$c} = shift @columnas;
68 }
69 0 push @datos, \%these_medidas;
70 }
71 }
72 }
73 } else {
74 2 my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.htm";
75
76 2 my $content = get( $url );
77
78
79 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
80 1 my $dom = Mojo::DOM->new( $content );
81
82 1 my @tables = $dom->find('table')->each;
83
84 1 shift @tables; #Primera tabla con leyenda
85
86 1 ▶ while ( @tables ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tables) 4 1 5
87 4 my $metadatos = shift @tables;
88 4 ▶ next if !@tables;
Branch: 1 branch
BranchTrueFalseTotal
unless @tables 0 4 4
89 4 my $medidas = shift @tables;
90
91 4 my @metadatos = ( $metadatos =~ /<b>.([A-Z][^<]+)/g);
92 4 my $this_metadata = { date => $fecha };
93 4 for my $k (qw(provincia municipio estacion direccion)) {
94 16 $this_metadata->{$k} = shift @metadatos;
95 }
96
97 4 my @filas = $medidas->find('tr')->each;
98
99 4 shift @filas; #Cabecera
100 4 pop @filas;
101 4 for my $f (@filas) {
102 576 my @columnas = $f->find('td')->map('text')->each;
103 576 my %these_medidas = %{$this_metadata};
104 576 my $fecha_hora = shift @columnas;
105 576 my ($hora) = ($fecha_hora =~ /(\d+:\d+)/);
106 576 ▶ if ( !$hora ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hora) 0 576 576
107 0 carp "Problemas con el formato en $f $fecha";
108 }
109 576 $these_medidas{'date'} =~ s/00:00/$hora/;
110 576 for my $c (qw(SO2 PART NO2 CO O3)) {
111 2880 $these_medidas{$c} = shift @columnas;
112 }
113 576 push @datos, \%these_medidas;
114 }
115 }
116 }
117 }
118 4 return \@datos;
119 }
120  
121 1 "We want air"; # Magic true value required at end of module
122 __END__
« blib/lib/Web/SIVA.pm ↑ Summary