File Coverage

blib/lib/Astro/ADS/Metrics.pm
Criterion Covered Total %
statement 53 59 89.8
branch 3 6 50.0
condition 1 3 33.3
subroutine 15 15 100.0
pod 3 3 100.0
total 75 86 87.2


line stmt bran cond sub pod time code
1             package Astro::ADS::Metrics;
2             $Astro::ADS::Metrics::VERSION = '1.92';
3 1     1   262647 use Moo;
  1         10448  
  1         7  
4             extends 'Astro::ADS';
5             #with 'Astro::ADS::Role::ResultMapper';
6              
7 1     1   3030 use Astro::ADS::Result;
  1         4  
  1         31  
8              
9 1     1   8 use Carp;
  1         1  
  1         78  
10 1     1   5 use Data::Dumper::Concise;
  1         1  
  1         50  
11 1     1   4 use Mojo::Base -strict; # do we want -signatures
  1         1  
  1         10  
12 1     1   238 use Mojo::DOM;
  1         2  
  1         35  
13 1     1   3 use Mojo::File qw( path );
  1         2  
  1         55  
14 1     1   494 use Mojo::JSON qw( decode_json encode_json );
  1         26724  
  1         81  
15 1     1   8 use Mojo::URL;
  1         1  
  1         42  
16 1     1   35 use Mojo::Util qw( quote url_escape );
  1         2  
  1         46  
17 1     1   5 use PerlX::Maybe;
  1         1  
  1         10  
18 1     1   32 use Types::Standard qw( Int Str Enum ArrayRef HashRef );
  1         2  
  1         10  
19              
20             has bibcode => (
21             is => 'rw',
22             isa => Str,
23             );
24             has bibcodes => (
25             is => 'rw',
26             isa => ArrayRef[Str],
27             default => sub { return [] },
28             );
29             has types => (
30             is => 'rw',
31             isa => Enum[qw( basic citations indicators histograms timeseries )],
32             );
33             has histograms => (
34             is => 'rw',
35             isa => Enum[qw( publications reads downloads citations )],
36             );
37              
38             sub details {
39 1     1 1 31254 my ($self, @bibcodes) = @_;
40 1         12 my $url = $self->base_url->clone->path('metrics/detail');
41              
42 1         207 my $response = $self->post_response( $url, {bibcodes => \@bibcodes} );
43 1 50       6 if ( $response->is_error ) {
44 0         0 carp $response->message;
45 0         0 my $error_obj = {
46             message => $response->message,
47             bibcodes => \@bibcodes,
48             url => $url->to_string,
49             };
50 0         0 return Astro::ADS::Result->new( {error => $error_obj} );
51             }
52              
53 1         13 return $response->json;
54             }
55              
56             sub fetch {
57 1     1 1 9037 my ($self, $bibcode) = @_;
58              
59 1 50 33     54 $bibcode ||= $self->bibcode()
60             or die 'No bibcode given to fetch';
61 1         14 my $path = join '/', 'metrics', url_escape($bibcode);
62 1         22 my $url = $self->base_url->clone->path($path);
63 1         207 my $response = $self->get_response( $url );
64 1         5 return $response->json;
65             }
66              
67             sub batch {
68 2     2 1 8608 my ($self, $bibcodes, $options) = @_;
69 2         15 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         316 };
76              
77 2         15 my $response = $self->post_response( $url, $terms );
78 2 50       4 if ( $response->is_error ) {
79 0         0 carp $response->message;
80 0         0 my $error_obj = {
81             message => $response->message,
82             bibcodes => $bibcodes,
83             url => $url->to_string,
84             key => $self->token,
85             };
86 0         0 return Astro::ADS::Result->new( {error => $error_obj} );
87             }
88              
89 2         46 return $response->json;
90             }
91              
92             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