OurWorldInData/Indicators.pm

filter: Statementstmt 100% 38 / 38 Branchbran 87.5% 7 / 8 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 90.5% 67 / 74 CC 9 SCAR 15.4
CC
9
Cov
93%
query
8.1
health
2.0
BEGIN
1.0
CRAP 4.7
« OurWorldInData/Search.pm ↑ Summary OurWorldInData/Chart.pm »
1 package WebService::OurWorldInData::Indicators;
2 # ABSTRACT: Queries the Our World in Data Indicators endpoint and collects the results
3  
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 extends 'WebService::OurWorldInData';
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PerlX::Maybe qw( maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Types::Standard qw( Str Int Num ); # Bool Enum ArrayRef HashRef InstanceOf ConsumerOf
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has base_url => (
13 is => 'ro',
14 1 ▶ default => sub { 'https://search.owid.io' },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 );
16  
17 1 has q => (
18 is => 'rw',
19 isa => Str,
20 );
21  
22 1 has limit => (
23 is => 'rw',
24 isa => Int,
25 );
26  
27 1 has min_popularity => (
28 is => 'rw',
29 isa => Num, # score 0-1 to filter results
30 );
31  
32  
33 6 ▶sub query {
Subroutine query: called CC=8 SCAR=20.9
Pod documented
34 6 my ($self, $q) = @_;
35 6 my $url = join '/', $self->base_url, 'indicators';
36  
37 6 ▶ $q //= $self->q;
Condition66%2/3
$q //= $self->q
l!l&&r!l&&!r
501
MC/DC50%1/2
$q //= $self->q
$q $self->q
Truth table66%2/3
A //= B
A= $qB= $self->q
ABresult
1X1
011
000
38 6 ▶ unless ( defined $q ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $q) 1 5 6
39 1 warn 'query method requires an argument to search on';
40 1 return;
41 }
42  
43 5 ▶ my $query = ref $q eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $q eq 'HASH' ? : 2 3 5
44 ? $q
45 : {
46 query => $q,
47 maybe limit => $self->limit,
48 maybe min_popularity => $self->min_popularity,
49 };
50 5 ▶ unless ( ( exists $query->{query} && $query->{query} =~ /\w/)
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $query->{"query"} and $query->{"query"} =~ /\w/ or exists $query->{"q"} and $query->{"q"} =~ /\w/) 1 4 5
Condition77%7/9
exists $query->{"query"} and $query->{"query"} =~ /\w/ or exists $query->{"q"} and $query->{"q"} =~ /\w/
l!l&&r!l&&!r
311
exists $query->{"query"} and $query->{"query"} =~ /\w/
!ll&&!rl&&r
203
exists $query->{"q"} and $query->{"q"} =~ /\w/
!ll&&!rl&&r
101
MC/DC50%2/4
exists $query->{"query"} and $query->{"query"} =~ /\w/ or exists $query->{"q"} and $query->{"q"} =~ /\w/
exists $query->{"query"} $query->{"query"} =~ /\w/ exists $query->{"q"} $query->{"q"} =~ /\w/
Truth table42%3/7
A and B or C and D
A= exists $query->{"query"}B= $query->{"query"} =~ /\w/C= exists $query->{"q"}D= $query->{"q"} =~ /\w/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
51 || ( exists $query->{q} && $query->{q} =~ /\w/) ) {
52 1 warn 'query missing "q" or "query" parameter';
53 1 return;
54 }
55  
56 4 return decode_json $self->get_response( $url, $query );
57 }
58  
59 1 ▶sub health {
Subroutine health: called CC=2 SCAR=7.0
Pod documented
60 1 my $self = shift;
61 1 my $url = join '/', $self->base_url, 'health';
62 1 my $msg = decode_json $self->get_response( $url );
63  
64 1 ▶ warn 'OWID Health check failed' unless $msg->{status} eq 'ok';
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->{"status"} eq "ok" 0 1 1
65 1 return $msg;
66 }
67  
68 1 1; # Perl is my Igor
69  
70 =head1 SYNOPSIS
71  
72 my $ind = WebService::OurWorldInData::Indicators->new();
73  
74 my $result = $ind->query('gdp');
75  
76 =head1 DESCRIPTION
77  
78 Queries the Our World in Data Indicators api which provides data and metadata
79 in CSV format. The Indicators object can be created with the following attributes:
80  
81 =over 4
82  
83 =item limit - the number of results to return
84  
85 =item min_popularity - filter results based on popularity score, value between 0 and 1
86  
87 =back
88  
89 as described by the OWiD API.
90  
91 my $ind = WebService::OurWorldInData::Indicators->new();
92  
93 =head1 Methods
94  
95 =head2 query
96  
97 Search for indicators using semantic similarity.
98  
99 my $json_response = $ind->query('gdp');
100  
101 =head2 health
102  
103 Runs a health check on the endpoint and returns a hashref with the status.
104 It will warn if the status is not 'ok'.
105  
106 =head2 Notes
107  
108 Results returned in the JSON structure provided by OWID. No Perl object handling.
109  
110 =head1 SEE ALSO
111  
112 =over 4
113  
114 =item * L<HTTP::Tiny>
115  
116 =item * L<Indicators API|https://docs.owid.io/projects/etl/api/semantic-search-api>
117  
118 =back
119  
120 =cut
« OurWorldInData/Search.pm ↑ Summary OurWorldInData/Chart.pm »