OurWorldInData/Chart.pm

filter: Statementstmt 94.2% 65 / 69 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 44.4% 4 / 9 totaltotal 88.5% 85 / 96 CC 4 SCAR 6.1
CC
4
Cov
92%
parse_data
4.6
BEGIN
1.0
BEGIN
1.0
CRAP 1.8
« OurWorldInData/Indicators.pm ↑ Summary OurWorldInData/Error.pm »
1 package WebService::OurWorldInData::Chart;
2 # ABSTRACT: Queries the Our World in Data Chart endpoint and collects the results
3  
4 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 extends 'WebService::OurWorldInData';
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use PerlX::Maybe qw( maybe provided );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Types::Standard qw( Str Bool Enum ); # Int ArrayRef HashRef InstanceOf ConsumerOf
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 has chart => (
13 is => 'rw',
14 isa => Str,
15 required => 1,
16 );
17  
18 4 has short_names => (
19 is => 'rw',
20 isa => Bool,
21 default => 0,
22 );
23  
24 4 has csv_type => (
25 is => 'rw',
26 isa => Enum[qw( full filtered )],
27 default => 'full',
28 );
29  
30 4 has [qw/time country/] => (
31 is => 'rw',
32 isa => Str,
33 );
34  
35  
36 5 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
Pod documented
37 5 my $self = shift;
38 5 my $url = $self->get_path . '.csv';
39  
40 5 my $query = {
41 maybe time => $self->time,
42 maybe country => $self->country,
43 provided $self->csv_type eq 'filtered',
44 csvType => $self->csv_type,
45 provided $self->short_names,
46 useColumnShortNames => 'true',
47 };
48  
49 5 return $self->get_response( $url, $query );
50 }
51  
52 12 ▶sub get_path {
Subroutine get_path: called CC=1 SCAR=0.0
Pod undocumented
53 12 my $self = shift;
54 12 return join '/', $self->base_url, 'grapher', $self->chart;
55 }
56  
57 1 ▶sub metadata {
Subroutine metadata: called CC=1 SCAR=0.0
Pod documented
58 1 my ($self) = @_;
59  
60 1 my $url = $self->get_path . '.metadata.json';
61  
62 1 my $response = $self->get_response( $url );
63 1 my $json = decode_json( $response );
64 1 return $json;
65 }
66  
67 1 ▶sub zip {
Subroutine zip: called CC=1 SCAR=0.0
Pod documented
68 1 my ($self) = @_;
69  
70 1 my $url = $self->get_path . '.zip';
71  
72 1 my $response = $self->get_response( $url );
73 1 return $response;
74 }
75  
76 1 ▶sub readme {
Subroutine readme: called CC=1 SCAR=0.0
Pod undocumented
77 1 my ($self) = @_;
78  
79 1 my $url = $self->get_path . '.readme.md';
80  
81 1 my $response = $self->get_response( $url );
82 1 return $response;
83 }
84  
85 1 ▶sub config {
Subroutine config: called CC=1 SCAR=0.0
Pod undocumented
86 1 my ($self) = @_;
87  
88 1 my $url = $self->get_path . '.config.json';
89  
90 1 my $response = $self->get_response( $url );
91 1 my $json = decode_json( $response );
92 1 return $json;
93 }
94  
95 1 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
Pod undocumented
96 1 my ($self) = @_;
97  
98 1 my $url = $self->get_path . '.values.json';
99  
100 1 my $response = $self->get_response( $url );
101 1 my $json = decode_json( $response );
102 1 return $json;
103 }
104  
105 1 ▶sub search_result {
Subroutine search_result: called CC=1 SCAR=0.0
Pod undocumented
106 1 my ($self) = @_;
107  
108 1 my $url = $self->get_path . '.search-result.json';
109  
110 1 my $response = $self->get_response( $url );
111 1 my $json = decode_json( $response );
112 1 return $json;
113 }
114  
115 2 ▶sub parse_data {
Subroutine parse_data: called CC=4 SCAR=15.2
Pod documented
116 2 my ($self, $body) = @_;
117  
118 2 eval { require Text::CSV };
119 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
120 0 carp "Install the Text::CSV module to use parse_data()\n", @_;
121 0 return;
122 }
123 2 my $csv = Text::CSV->new;
124 2 my @rows = ();
125 2 for my $line (split /\n/, $body) {
126 10 ▶ unless ( $csv->parse($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csv->parse($line)) 0 10 10
127 0 carp "Error parsing CSV data (", $csv->error_diag(),
128 ") suggest you save as a file and parse the file instead.";
129 0 return;
130 }
131 10 push @rows, [ $csv->fields ];
132 }
133  
134 2 return \@rows;
135 }
136  
137 4 1; # Perl is my Igor
138  
139 =head1 SYNOPSIS
140  
141 my $chart = WebService::OurWorldInData::Chart->new({
142 chart => 'sea-surface-temperature-anomaly', # dataset name
143 });
144  
145 my $result = $chart->data(); # get the csv data
146 my $rows = $chart->parse_data($result);
147  
148 =head1 DESCRIPTION
149  
150 Queries the Our World in Data Chart api which provides data and metadata
151 in CSV format. The Chart object can be created with the following attributes:
152  
153 =over 4
154  
155 =item short_names - a boolean flag to affect the results
156  
157 =item csv_type - either full (default) or filtered
158  
159 =item country - filter results based on country code
160  
161 =item time - filter results on years, ranges declared with ".."
162  
163 =back
164  
165  
166 as described by the OWiD API.
167  
168 my $chile = WebService::OurWorldInData::Chart->new(
169 chart => 'life-expectancy',
170 csv_type => 'filtered',
171 country => '~CHL',
172 time => '1998..2023' );
173  
174 =head1 Methods
175  
176 =head2 data
177  
178 Queries the endpoint for the csv file
179  
180 my $result = $chart->data(); # get the csv data
181 my $rows = $chart->parse_data($result);
182  
183 or save the data to a file
184  
185 open my $fh, '>', 'data.csv';
186 print $fh $result;
187 close $fh
188  
189 =head2 parse_data
190  
191 Takes the output from the C<data> method and uses Text::CSV
192 to read each line, returning an arrayref of rows.
193 You will need to have the Text::CSV module installed.
194  
195 If the parsing throws an error, it bails warning you to save
196 the file and parse it in full.
197  
198 =head2 metadata
199  
200 Queries the endpoint for *.metadata.json
201  
202 my $json = $chart->metadata(); # get the metadata
203 my $column = (keys $result->{columns}->%*)[0]; # grab one of the really long keys
204 print $json->{chart}{title},
205 $json->{columns}{$column}{timespan};
206  
207 =head2 zip
208  
209 Queries the endpoint for the zip file
210  
211 my $result = $chart->zip;
212  
213 my $filename = $dataset . 'zip';
214 open my $fh, '>:raw', $filename; # write out binary
215 print $fh $result;
216 close $fh;
217  
218 my $ae = Archive::Extract->new( archive => $filename );
219 $ae->extract or warn "Error extracting $filename: ", $ae->error;
220 my $files = $ae->files;
221  
222 =head2 Notes
223  
224  
225 =head1 SEE ALSO
226  
227 =over 4
228  
229 =item * L<HTTP::Tiny>
230  
231 =back
232  
233 =cut
« OurWorldInData/Indicators.pm ↑ Summary OurWorldInData/Error.pm »