File Coverage

blib/lib/Astro/ADS/Result.pm
Criterion Covered Total %
statement 38 43 88.3
branch 5 6 83.3
condition 5 6 83.3
subroutine 11 12 91.6
pod 2 2 100.0
total 61 69 88.4


line stmt bran cond sub pod time code
1             package Astro::ADS::Result;
2             $Astro::ADS::Result::VERSION = '1.92';
3 5     5   60 use Moo;
  5         18  
  5         52  
4              
5 5     5   2377 use Carp;
  5         13  
  5         463  
6 5     5   661 use Data::Dumper::Concise;
  5         14601  
  5         559  
7 5     5   840 use Mojo::Base -strict; # do we want -signatures
  5         13153  
  5         53  
8 5     5   2278 use Mojo::DOM;
  5         257795  
  5         248  
9 5     5   520 use Mojo::File qw( path );
  5         26610  
  5         371  
10 5     5   582 use Mojo::URL;
  5         7574  
  5         55  
11 5     5   255 use Mojo::Util qw( quote );
  5         12  
  5         423  
12 5     5   591 use PerlX::Maybe;
  5         3588  
  5         46  
13 5     5   851 use Types::Standard qw( Int Str ArrayRef HashRef Bool ); # InstanceOf ConsumerOf
  5         191938  
  5         54  
14              
15             has [qw/q fq fl sort/] => (
16             is => 'rw',
17             isa => Str,
18             );
19             has numFound => (
20             is => 'rw',
21             isa => Int->where( '$_ >= 0' ),
22             );
23             has numFoundExact => (
24             is => 'rw',
25             isa => Bool,
26             );
27             has [qw/start rows/] => (
28             is => 'rw',
29             isa => Int->where( '$_ >= 0' ),
30             );
31             has error => (
32             is => 'rw',
33             isa => HashRef[]
34             );
35             has docs => (
36             is => 'rw',
37             isa => ArrayRef[],
38             #isa => ArrayRef[ InstanceOf ['Astro::ADS::Paper'] ],
39             default => sub { return [] },
40             );
41              
42             # if the query failed, the Result has an error
43             # so warn the user if they try to access other returned attributes
44             before [qw/numFound numFoundExact start rows docs/] => sub {
45             my ($self) = @_;
46             if ($self->error ) {
47             carp 'Empty Result object: ', $self->error->{message};
48             }
49             };
50              
51             sub next {
52 4     4 1 15510 my ($self, $num) = @_;
53 4         151 my $next_start = $self->start + $self->rows;
54              
55 4 50 100     217 if ( $next_start > $self->numFound ) {
    100          
56 0         0 carp "No more results for ", $self->q, "\n";
57 0         0 return;
58             }
59             elsif ( $num && !($num > 0) ) {
60 2         113 carp sprintf('Bad value for number of rows: %s. Defaulting to %d', $num, $self->rows);
61 2         518 $num = 0;
62             }
63              
64 4   66     181 my $next_search_terms = {
65             q => $self->q,
66             start => $next_start,
67             maybe fq => $self->fq,
68             maybe fl => $self->fl,
69             maybe rows => ($num || $self->rows),
70             maybe sort => $self->sort,
71             };
72             delete $next_search_terms->{rows}
73 4 100       206 if $next_search_terms->{rows} == 10; # don't send default
74              
75 4         16 return $next_search_terms;
76             }
77              
78             sub get_papers {
79 0     0 1   my $self = shift;
80 0           return @{$self->docs};
  0            
81             }
82              
83             #To Be Decided:
84             #This is a hold over from v1.0 which gets the summary for each document.
85             #Should we ditch it?
86             #
87             #sub summary {
88             # my $self = shift;
89             # for my $paper ( $self->get_papers ) {
90             # $paper->summary;
91             # }
92             #}
93             #
94             #sub sizeof {
95             # my $self = shift;
96             # return $self->numFound; # or should it be $self->rows ?
97             #}
98              
99             1;
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             Astro::ADS::Result - A class for the results of a Search
108              
109             =head1 VERSION
110              
111             version 1.92
112              
113             =head1 SYNOPSIS
114              
115             my $search = Astro::ADS::Search->new(...);
116              
117             my $result = $search->query();
118             my @papers = $result->get_papers();
119              
120             my $next_q = $result->next();
121             $result = $ads->query( $next_q );
122              
123             =head1 DESCRIPTION
124              
125             The Result class holds the
126             L<response|https://ui.adsabs.harvard.edu/help/api/api-docs.html#get-/search/query>
127             from an ADS search query. It will create attributes for all the fields specified
128             in the C<fl> parameter of the search OR it will hold the error returned by the
129             L<UserAgent|Astro::ADS>. If an error was returned, any calls to attribute methods
130             will raise a polite warning that no fields will be available for that object.
131              
132             By default, a successful search returns up to 10 rows of results. If more exist,
133             the user iterates through them using the L</"next"> method to generate a search
134             query updated to start where the previous search left off.
135              
136             =head1 Methods
137              
138             =head2 get_papers
139              
140             This method gets a list of L<Astro::ADS::Paper>s from the last query executed.
141              
142             =head2 next
143              
144             Creates a new search query term hashref, suitable for fetching the next N papers
145             from the ADS. If not given as an argument, the default is 10 rows.
146             It returns C<undef> if you have already reached the end of the available results.
147              
148             This takes the values from the response header and updates the start position,
149             collects the original query C<q> and other params and returns the hashref,
150             ready for the next C<<$search->query>>.
151              
152             If given an argument, it takes that as the number of rows to fetch.
153              
154             =head1 See Also
155              
156             =over 4
157              
158             =item * L<Astro::ADS>
159              
160             =item * L<Astro::ADS::Search>
161              
162             =item * L<ADS API|https://ui.adsabs.harvard.edu/help/api/>
163              
164             =item * L<Available fields for Results|https://ui.adsabs.harvard.edu/help/search/comprehensive-solr-term-list>
165              
166             =back
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is Copyright (c) 2025 by Boyd Duffee.
171              
172             This is free software, licensed under:
173              
174             The MIT (X11) License
175              
176             =cut