File Coverage

blib/lib/Astro/ADS/Role/ResultMapper.pm
Criterion Covered Total %
statement 43 47 91.4
branch 4 8 50.0
condition 2 6 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 58 70 82.8


line stmt bran cond sub pod time code
1             package Astro::ADS::Role::ResultMapper;
2             $Astro::ADS::Role::ResultMapper::VERSION = '1.92';
3 4     4   29154 use Moo::Role;
  4         12  
  4         31  
4 4     4   5011 use strictures 2;
  4         16702  
  4         234  
5              
6 4     4   4060 use Astro::ADS::Paper;
  4         20  
  4         215  
7 4     4   2559 use Astro::ADS::QTree;
  4         42  
  4         217  
8 4     4   2433 use Astro::ADS::Result;
  4         27  
  4         2083  
9              
10             sub parse_response {
11 6     6 1 19 my ($self, $json) = @_;
12              
13 6 50 33     48 unless ($json && exists $json->{responseHeader}) {
14 0         0 warn 'No response to parse';
15 0         0 return;
16             }
17             # re-map wanted hash keys
18 6         13 my $result_params;
19 6         13 @{$result_params}{ qw<q rows fl> } = @{$json->{responseHeader}{params}}{ qw<q rows fl> };
  6         63  
  6         27  
20 6         11 $result_params->{status} = @{$json->{responseHeader}}{ status };
  6         39  
21 6         16 @{$result_params}{ qw<numFound start> } = @{$json->{response}}{ qw<numFound start> };
  6         18  
  6         18  
22 6 50       105 $result_params->{numFoundExact} = $json->{response}{numFoundExact} ? 1 : 0;
23              
24 6         86 my @papers;
25 6         20 for my $paper ( @{$json->{response}->{docs}} ) {
  6         23  
26 60         20279 push @papers, Astro::ADS::Paper->new( $paper );
27             }
28 6 50       229 $result_params->{docs} = \@papers if @papers;
29              
30 6         101 return Astro::ADS::Result->new( $result_params );
31             }
32              
33             sub parse_qtree_response {
34 1     1 1 3 my ($self, $response) = @_;
35              
36 1         6 my $json = $response->json;
37 1 50 33     87 unless ($json && exists $json->{responseHeader}) {
38 0         0 warn 'No response to parse';
39 0         0 return;
40             }
41             # re-map wanted hash keys
42 1         3 my $result_params;
43 1         3 @{$result_params}{ qw<qtime status> } = @{$json->{responseHeader}}{ qw<QTime status> };
  1         9  
  1         1261  
44 1         7 $result_params->{qtree} = $json->{qtree};
45              
46 1         8 $result_params->{asset} = $response->content->asset;
47              
48 1         27 return Astro::ADS::QTree->new( $result_params );
49             }
50              
51             1;
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             Astro::ADS::Role::ResultMapper - Maps the JSON structure returned from an ADS search query
60             to an Astro::ADS::Result object
61              
62             =head1 VERSION
63              
64             version 1.92
65              
66             =head1 SYNOPSIS
67              
68             use Moo;
69             extends 'Astro::ADS';
70             with 'Astro::ADS::Role::ResultMapper';
71              
72             ...
73             return $self->parse_response( $json );
74              
75             =head1 DESCRIPTION
76              
77             =head2 parse_response
78              
79             Takes the Mojo Response JSON and maps it to the Result object parameters,
80             returning a Result object with Papers in the B<docs> attribute.
81              
82             =head2 parse_qtree_response
83              
84             Takes the Mojo Response and maps it to the QTree object parameters,
85             allocating the JSON fields to attributes and the C<content> to the C<asset>
86             attribute to allow use of the Mojo::Asset::move_to function.
87              
88             This interface is subject to change up to v2.0 to discover the best way
89             to use the returned value.
90              
91             =head1 COPYRIGHT AND LICENSE
92              
93             This software is Copyright (c) 2025 by Boyd Duffee.
94              
95             This is free software, licensed under:
96              
97             The MIT (X11) License
98              
99             =cut