ADS/Metrics.pm

filter: Statementstmt 91.0% 61 / 67 Branchbran 50.0% 3 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 86.6% 84 / 97 CC 5 SCAR 4.5
CC
5
Cov
86%
fetch
3.2
details
2.3
batch
2.2
CRAP 1.6
« ADS/Paper.pm ↑ Summary ADS/QTree.pm »
1 package Astro::ADS::Metrics;
2 1 $Astro::ADS::Metrics::VERSION = '1.92';
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'Astro::ADS';
5 #with 'Astro::ADS::Role::ResultMapper';
6  
7 1 ▶use Astro::ADS::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mojo::Base -strict; # do we want -signatures
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Mojo::File qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mojo::JSON qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Mojo::Util qw( quote url_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Types::Standard qw( Int Str Enum ArrayRef HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 has bibcode => (
21 is => 'rw',
22 isa => Str,
23 );
24 has bibcodes => (
25 is => 'rw',
26 isa => ArrayRef[Str],
27 1 ▶ default => sub { return [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 );
29 1 has types => (
30 is => 'rw',
31 isa => Enum[qw( basic citations indicators histograms timeseries )],
32 );
33 1 has histograms => (
34 is => 'rw',
35 isa => Enum[qw( publications reads downloads citations )],
36 );
37  
38 1 ▶sub details {
Subroutine details: called CC=2 SCAR=8.1
Pod documented
39 1 my ($self, @bibcodes) = @_;
40 1 my $url = $self->base_url->clone->path('metrics/detail');
41  
42 1 my $response = $self->post_response( $url, {bibcodes => \@bibcodes} );
43 1 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 1 1
44 0 carp $response->message;
45 0 my $error_obj = {
46 message => $response->message,
47 bibcodes => \@bibcodes,
48 url => $url->to_string,
49 };
50 0 return Astro::ADS::Result->new( {error => $error_obj} );
51 }
52  
53 1 return $response->json;
54 }
55  
56 1 ▶sub fetch {
Subroutine fetch: called CC=3 SCAR=11.6
Pod documented
57 1 my ($self, $bibcode) = @_;
58  
59 1 ▶ $bibcode ||= $self->bibcode()
Branch: 1 branch
BranchTrueFalseTotal
$bibcode ||= $self->bibcode or die "No bibcode given to fetch" 1 0 1
Condition33%1/3
$bibcode ||= $self->bibcode
l!l&&r!l&&!r
010
MC/DC0%0/2
$bibcode ||= $self->bibcode
$bibcode $self->bibcode
Truth table33%1/3
A ||= B
A= $bibcodeB= $self->bibcode
ABresult
1X1
011
000
60 or die 'No bibcode given to fetch';
61 1 my $path = join '/', 'metrics', url_escape($bibcode);
62 1 my $url = $self->base_url->clone->path($path);
63 1 my $response = $self->get_response( $url );
64 1 return $response->json;
65 }
66  
67 2 ▶sub batch {
Subroutine batch: called CC=2 SCAR=7.8
Pod documented
68 2 my ($self, $bibcodes, $options) = @_;
69 2 my $url = $self->base_url->clone->path('metrics');
70  
71 my $terms = {
72 bibcodes => $bibcodes,
73 maybe types => $options->{types},
74 maybe histograms => $options->{histograms},
75 2 };
76  
77 2 my $response = $self->post_response( $url, $terms );
78 2 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 2 2
79 0 carp $response->message;
80 0 my $error_obj = {
81 message => $response->message,
82 bibcodes => $bibcodes,
83 url => $url->to_string,
84 key => $self->token,
85 };
86 0 return Astro::ADS::Result->new( {error => $error_obj} );
87 }
88  
89 2 return $response->json;
90 }
91  
92 1 1;
93  
94 =pod
95  
96 =encoding UTF-8
97  
98 =head1 NAME
99  
100 Astro::ADS::Metrics - Queries the ADS Metrics endpoint and collects the results
101  
102 =head1 VERSION
103  
104 version 1.92
105  
106 =head1 SYNOPSIS
107  
108 my $metrics = Astro::ADS::Metrics->new({
109 bibcodes => ['...'], # list of bibcodes
110 { types => ['basic'] } # types of metrics to return
111 });
112  
113 my $json_result = $metrics->batch();
114  
115 my $single_bibcode = $metrics->fetch('2019MNRAS.487.3523C');
116  
117 my $detailed_stats = $metrics->details( @bibcodes );
118  
119  
120 =head1 DESCRIPTION
121  
122 Fetch Metrics for papers in the Harvard ADS
123  
124 Currently only C<fetch> checks the Metrics object for
125 how to make the calls.
126  
127 The json structure is documented on the ADS API website.
128 I haven't used the ResultMapper to make it easier to access.
129 You'll have to work that out yourself. Let me know if there's a better way.
130  
131 =head1 Methods
132  
133 =head2 fetch
134  
135 Fetch a single bibcode. No options taken.
136 Returns the json response as a hash reference.
137  
138 =head2 batch
139  
140 Takes an array ref of bibcodes to find metrics for.
141 An optional hash ref can limit the types of metrics
142 returned and if the type is histogram, which histograms
143 to return. As in
144  
145 { types => ['histograms'], histograms => ['citations'] }
146  
147 Returns the json response as a hash reference.
148  
149 =head2 details
150  
151 Queries the metrics/details endpoint with a list of bibcodes.
152 Takes no options.
153 Returns the json response as a hash reference.
154  
155 =head2 Notes
156  
157 This module's client methods are liable to change,
158 but deprecation warnings will be issued if they do.
159 See the docs.
160  
161 =head1 TODO
162  
163 The Metrics object should store the arguments from the latest
164 request, so that repeated calls can be brief.
165  
166 Consider using the ResultMapper to contain results (if that makes sense)
167 or maybe to warn if there are any C<skipped bibcodes> in the response.
168  
169 =head1 See Also
170  
171 =over 4
172  
173 =item * L<Astro::ADS>
174  
175 =item * L<ADS API|https://ui.adsabs.harvard.edu/help/api/>
176  
177 =item * L<Metrics API|https://ui.adsabs.harvard.edu/help/api/api-docs.html#tag--metrics>
178  
179 =back
180  
181 =head1 COPYRIGHT AND LICENSE
182  
183 This software is Copyright (c) 2025 by Boyd Duffee.
184  
185 This is free software, licensed under:
186  
187 The MIT (X11) License
188  
189 =cut
« ADS/Paper.pm ↑ Summary ADS/QTree.pm »