OurWorldInData.pm

filter: Statementstmt 66.6% 30 / 45 Branchbran 37.5% 9 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 2 totaltotal 57.5% 46 / 80 CC 13 SCAR 23.6
CC
13
Cov
57%
_report_status_tiny
30.0
get_response
6.5
_report_status_full
6.4
CRAP 10.6
↑ Summary OurWorldInData/Catalog.pm »
1 package WebService::OurWorldInData;
2 # ABSTRACT: Perl library to connect with the Our World in Data API
3 # https://ourworldindata.org
4  
5 8 our $VERSION = '0.06';
6  
7 8 ▶use v5.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 my $DEBUG = 0;
12  
13 has ua => (
14 is => 'ro',
15 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 require HTTP::Tiny;
17 2 require IO::Socket::SSL;
18 2 HTTP::Tiny->new(
19 agent => "WebService-OurWorldInData/$VERSION ",
20 );
21 },
22 8 );
23  
24 has base_url => (
25 is => 'ro',
26 12 ▶ default => sub { 'https://ourworldindata.org' },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 8 );
28  
29 19 ▶sub get_response {
Subroutine get_response: called CC=5 SCAR=18.7
Pod undocumented
30 #TODO Needs to be re-written to handle different UAs gracefully ####
31 19 my ($self, $url, $query) = @_;
32  
33 19 my $res;
34 19 ▶ if ( ref $self->ua eq 'HTTP::Tiny' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->ua eq 'HTTP::Tiny') { } 0 19 19
elsif (ref $self->ua eq 'LWP::UserAgent') { } 19 0 19
35 0 my $params = $self->ua->www_form_urlencode( $query );
36 0 $res = $self->ua->get( join('?', $url, $params) );
37 0 _report_status_tiny($res);
38 }
39 elsif ( ref $self->ua eq 'LWP::UserAgent' ) {
40 19 require URI;
41 19 my $uri = URI->new( $url );
42 19 ▶ $uri->query_form( $query ) if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 12 7 19
43 19 $res = $self->ua->get( $uri->as_string );
44 19 _report_status_full($res);
45 }
46 else {
47 0 carp 'No url_encoding for ', ref $self->ua;
48 0 $res = $self->ua->get( $url );
49 0 _report_status_full($res);
50 }
51  
52 19 ▶ return ref $self->ua eq 'HTTP::Tiny' ? $res->{content} : $res->content;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->ua eq 'HTTP::Tiny' ? : 0 19 19
53 }
54  
55 0 ▶sub _report_status_tiny {
Subroutine _report_status_tiny: not called CC=5 SCAR=34.0
56 0 my $res = shift;
57  
58 0 ▶ if ($res->{success}) { warn $res->{content} if $DEBUG > 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($res->{'success'}) { } 0 0 0
elsif ($res->{'redirects'}) { } 0 0 0
if $DEBUG > 1 0 0 0
59 0 ▶ elsif ($res->{redirects}) { carp 'Redirected: ', $res->headers->location if $DEBUG }
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
60 0 else { carp 'HTTP Error: ', $res->{status}, $res->{reason}; }
61 }
62  
63 19 ▶sub _report_status_full {
Subroutine _report_status_full: called CC=5 SCAR=18.6
64 19 my $res = shift;
65  
66 19 ▶ if ($res->is_success) { warn $res->content if $DEBUG > 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($res->is_success) { } 17 2 19
elsif ($res->is_redirect) { } 0 2 2
if $DEBUG > 1 0 17 17
67 0 ▶ elsif ($res->is_redirect) { carp 'Redirected: ', $res->headers->location if $DEBUG }
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
68 2 else { carp join q{ }, 'HTTP Error:', $res->code, $res->message; }
69 }
70  
71 0 ▶sub post_response {
Subroutine post_response: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my ($self, $url) = @_;
73  
74 0 my $res = $self->ua->post( $url );
75  
76 0 return $res->{content};
77 }
78  
79 8 1; # Perl is my Igor
80  
81 =head1 SYNOPSIS
82  
83 my $owid = WebService::OurWorldInData->new({
84 proxy => '...', # your web proxy
85 });
86  
87 my $search = $owid->search( q => 'star', fl => 'bibcode' );
88  
89 =head1 DESCRIPTION
90  
91 This is a base class for Our World in Data APIs. You will want the modules
92 for each endpoint:
93  
94 =over 4
95  
96 =item * L<WebService::OurWorldInData::Chart>
97  
98 =item * L<WebService::OurWorldInData::Tables>
99  
100 =item * L<WebService::OurWorldInData::Indicators>
101  
102 =item * L<WebService::OurWorldInData::Search>
103  
104 =back
105  
106 B<WARNING>: from the OWID dev page, I<These APIs are under active development>.
107 For that reason, I will start by only providing the JSON responses from their
108 server. As the API design crystalizes, I will add convenience handling classes
109 for the Results so that you can iterate through them for specific bits of data.
110  
111 =head2 Getting Started
112  
113 OWID's L<Technical Documentation|https://docs.owid.io/projects/etl/api/>
114  
115 =head2 Proxies
116  
117 The UA gets the proxy from your environment variable
118 _or_
119 create a HTTP::Tiny object with the {all_proxy => "proxy url"} attribute
120 and pass that to the C<ua> attribute of the API constructor
121  
122 $tiny_ua = HTTP::Tiny->new({all_proxy => "http://proxy.url"});
123 $client = WebService::OurWorldInData->new({ ua => $tiny_ua });
124  
125 =head1 ACKNOWLEDGMENTS
126  
127 I am stealing from Neil Bowers' L<WebService::HackerNews> to learn how he does
128 APIs with L<HTTP::Tiny>. This is a re-write from my first version in Mojo.
129 Any mistakes, of course, are mine.
130  
131 =head1 REPOSITORY
132  
133 L<https://github.com/duffee/perl-OurWorldInData>
134  
135 =head1 AUTHOR
136  
137 Boyd Duffee E<lt>duffee@cpan.orgE<gt>
138  
139 =head1 LICENSE
140  
141 MIT License
142  
143 Copyright (c) 2025 Boyd Duffee
144  
145 Permission is hereby granted, free of charge, to any person obtaining a copy
146 of this software and associated documentation files (the "Software"), to deal
147 in the Software without restriction, including without limitation the rights
148 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
149 copies of the Software, and to permit persons to whom the Software is
150 furnished to do so, subject to the following conditions:
151  
152 The above copyright notice and this permission notice shall be included in all
153 copies or substantial portions of the Software.
154  
155 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
156 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
157 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
158 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
159 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
160 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
161 SOFTWARE.
162  
163 =cut
↑ Summary OurWorldInData/Catalog.pm »