| 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
MC/DC50%1/2
$q //= $self->q
$q
$self->q
Truth table66%2/3
A //= B
A= $qB= $self->q
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 6 | ▶ | unless ( defined $q ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition77%7/9
exists $query->{"query"} and $query->{"query"} =~ /\w/ or exists $query->{"q"} and $query->{"q"} =~ /\w/
exists $query->{"query"} and $query->{"query"} =~ /\w/
exists $query->{"q"} and $query->{"q"} =~ /\w/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||