| 1 | package WebService::OurWorldInData::Search; | ||||||||||||||||||||
| 2 | # ABSTRACT: Queries the Our World in Data Search endpoint and collects the results | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | extends 'WebService::OurWorldInData'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use JSON qw(decode_json); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use PerlX::Maybe qw( maybe provided ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use Types::Standard qw( Int Str Bool Enum ); # ArrayRef HashRef InstanceOf ConsumerOf | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | has q => ( | |||||||||||||||||||
| 13 | is => 'rw', | ||||||||||||||||||||
| 14 | isa => Str, | ||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | has type => ( | |||||||||||||||||||
| 18 | is => 'rw', | ||||||||||||||||||||
| 19 | isa => Enum[qw( charts pages )], | ||||||||||||||||||||
| 20 | default => 'charts', | ||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | has [qw/page hitsPerPage/] => ( | |||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||
| 25 | isa => Int, | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | has [qw/countries topics pageTypes/] => ( | |||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||
| 30 | isa => Str, | ||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | has require_all_countries => ( | |||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||
| 35 | isa => Bool, | ||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 3 | ▶ | sub query { | ||||||||||||||||||
|
Subroutine
query: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||||||||
| 40 | 3 | my ($self, $query_string) = @_; | |||||||||||||||||||
| 41 | 3 | my $url = $self->get_path; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 3 | ▶ | my $query = { | ||||||||||||||||||
|
Condition33%1/3
$query_string || $self->q
MC/DC0%0/2
$query_string || $self->q
$query_string
$self->q
Truth table33%1/3
A || B
A= $query_stringB= $self->q
| |||||||||||||||||||||
| 44 | q => $query_string || $self->q, | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | maybe type => $self->type, | ||||||||||||||||||||
| 47 | maybe page => $self->page, | ||||||||||||||||||||
| 48 | maybe hitsPerPage => $self->hitsPerPage, | ||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||
| 50 | 3 | ▶ | if ($self->type eq 'charts') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 2 | ▶ | $query->{countries} = $self->countries if $self->countries; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 2 | ▶ | $query->{topics} = $self->topics if $self->topics; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||
| 55 | 1 | ▶ | $query->{pageTypes} = $self->pageTypes if $self->pageTypes; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 3 | ▶ | if (exists $query->{countries}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | maybe $query->{requireAllCountries} => $self->require_all_countries; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 3 | return decode_json $self->get_response( $url, $query ); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 3 | ▶ | sub get_path { | ||||||||||||||||||
|
Subroutine
get_path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 66 | 3 | my $self = shift; | |||||||||||||||||||
| 67 | 3 | return join '/', $self->base_url, qw(api search); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | ▶ | sub extract_urls { | ||||||||||||||||||
|
Subroutine
extract_urls: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 71 | 1 | my ($self, $data) = @_; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | return map { $_->{url} } @{$data->{results}}; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 2 | 1; # Perl is my Igor | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 SYNOPSIS | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | my $search = WebService::OurWorldInData::Search->new(); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | my $gdp = $search->query('gdp'); | ||||||||||||||||||||
| 83 | for my $chart ( $gdp->{results}->@* ) { | ||||||||||||||||||||
| 84 | say $chart->{title}; | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | my @urls = $search->extract_urls($gdp); | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head1 DESCRIPTION | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Queries the Our World in Data Search api for charts or pages. | ||||||||||||||||||||
| 92 | The Search object can be created with the following attributes: | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =over 4 | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =item q - query text used when the query method is called with no arguments | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =item type - either charts (default) or pages | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =item page - page number for pagination | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =item hitsPerPage - number of results per page | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =item countries - (charts only) filter results based on countries (separated by ~) | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =item topics - (charts only) topic name for filtering charts | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =item requireAllCountries - boolean (only if countries provided) | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =item pageTypes - (pages only) Comma-separated list of page content types to search | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =back | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Refine search query as described by the | ||||||||||||||||||||
| 115 | L<OWiD Search API|https://docs.owid.io/projects/etl/api/search-api/>. eg. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | my $nato_fr = WebService::OurWorldInData::Search->new( | ||||||||||||||||||||
| 118 | countries => 'Canada~France~Belgium', | ||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||
| 120 | $nato_fr->hitsPerPage(50); | ||||||||||||||||||||
| 121 | $nato_fr->require_all_countries(1); | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head2 extract_urls | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Provided as a utility function, it takes the response as an argument | ||||||||||||||||||||
| 126 | and returns a list of urls extracted from the results. | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =head2 NOTES | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | I haven't deconstructed the response from the Search endpoint. | ||||||||||||||||||||
| 131 | Your results are a list of hashrefs in the data structure. | ||||||||||||||||||||
| 132 | Let me know if you'd like a smoother api. | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head2 TODO | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | Add a C<next_page> function to continue fetching results. | ||||||||||||||||||||
| 137 | This may need to store current page in a SearchResponse object. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =head1 SEE ALSO | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =over 4 | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =item * L<HTTP::Tiny> | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =back | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||