ADS.pm

filter: Statementstmt 79.2% 65 / 82 Branchbran 43.3% 13 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 2 totaltotal 70.5% 91 / 129 CC 16 SCAR 19.1
CC
16
Cov
70%
post_response
13.9
get_response
9.7
_build_token
3.3
CRAP 6.8
« ADS/Search.pm ↑ Summary ADS/Role/ResultMapper.pm »
1 package Astro::ADS;
2 5 $Astro::ADS::VERSION = '1.92';
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Types::Standard qw( StrMatch );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # suppress warning for native perl 5.36 try/catch
14 5 ▶no if $] >= 5.018, 'warnings', 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 my $DEBUG = 0;
17  
18 5 has ua => ( is => 'lazy' );
19 5 has proxy => ( is => 'lazy' );
20 5 has token => ( is => 'lazy', isa => StrMatch[qr/\A\w{20,50}\z/] );
21  
22 has base_url => (
23 is => 'ro',
24 7 ▶ default => sub { Mojo::URL->new('https://api.adsabs.harvard.edu/v1/') },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 5 );
26  
27 0 ▶sub _build_ua {
Subroutine _build_ua: not called CC=1 SCAR=6.9
28 0 my ($self) = @_;
29 0 return Mojo::UserAgent->new;
30 }
31  
32 0 ▶sub _build_proxy {
Subroutine _build_proxy: not called CC=1 SCAR=6.9
33 0 my ($self, $proxy) = @_;
34 0 $self->ua->proxy->https( $proxy );
35 0 return $proxy;
36 }
37  
38 7 ▶sub _build_token {
Subroutine _build_token: called CC=3 SCAR=12.0
39 7 my ($self) = @_;
40  
41 7 ▶ return $ENV{ADS_DEV_KEY} if $ENV{ADS_DEV_KEY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"ADS_DEV_KEY"} 6 1 7
42  
43 # consider using File::HomeDir
44 1 my $dev_key_file = Mojo::File->new($ENV{HOME} . '/.ads/dev_key');
45 1 ▶ if (-e $dev_key_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dev_key_file) 0 1 1
46 0 my $key = $dev_key_file->slurp;
47 0 chomp $key; # remove this line to create a Bad Request
48 0 return $key;
49 }
50  
51 1 croak "You need to provide an API token.\nSee https://metacpan.org/pod/Astro::ADS#Getting-Started\n";
52 }
53  
54 8 ▶sub get_response {
Subroutine get_response: called CC=7 SCAR=22.7
Pod undocumented
55 8 my ($self, $url) = @_;
56  
57 8 my $tx = $self->ua->build_tx( GET => $url );
58 8 $tx->req->headers->authorization( 'Bearer ' . $self->token );
59 8 ▶ warn $tx->req->to_string if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 8 8
60
61 8 try { $tx = $self->ua->start($tx) }
62 catch ($error) {
63 0 carp "Got this error: ", $error;
64 }
65  
66 8 my $res;
67 8 try { $res = $tx->result } # call to result dies on connection error
68 catch ($error) {
69 0 carp "Connection error: ", $error;
70 0 return;
71 }
72 8 ▶ if ($res->is_success) { warn $res->body if $DEBUG > 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($res->is_success) { } 7 1 8
elsif ($res->is_error) { } 1 0 1
elsif ($res->code == 301) { } 0 0 0
if $DEBUG > 1 0 7 7
73 1 elsif ($res->is_error) { carp 'HTTP Error: ', $res->message }
74 0 ▶ elsif ($res->code == 301) { carp 'Redirected: ', $res->headers->location if $DEBUG }
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
75  
76 8 return $res;
77 }
78  
79 4 ▶sub post_response {
Subroutine post_response: called CC=8 SCAR=26.3
Pod undocumented
80 4 my ($self, $url, $content) = @_;
81  
82 # this will work until we need to do form submission
83 # hashrefs are sent as JSON
84 4 ▶ my $tx = ref $content eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $content eq 'HASH' ? : 3 1 4
85 ? $self->ua->build_tx( POST => $url, json => $content )
86 : $self->ua->build_tx( POST => $url, $content );
87 4 $tx->req->headers->authorization( 'Bearer ' . $self->token );
88 4 ▶ carp "Request sent to $url\n\n", $tx->req->to_string if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 4 4
89
90 4 try { $tx = $self->ua->start($tx) }
91 catch ($error) {
92 0 carp "Got this error: ", $error;
93 }
94  
95 4 my $res;
96 4 try { $res = $tx->result } # call to result dies on connection error
97 catch ($error) {
98 0 carp "Connection error: ", $error;
99 0 return;
100 }
101 4 ▶ if ($res->is_success) { warn $res->body if $DEBUG > 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($res->is_success) { } 4 0 4
elsif ($res->is_error) { } 0 0 0
elsif ($res->code == 301) { } 0 0 0
if $DEBUG > 1 0 4 4
102 0 elsif ($res->is_error) { carp 'HTTP Error: ', $res->message }
103 0 ▶ elsif ($res->code == 301) { carp 'Redirected: ', $res->headers->location if $DEBUG }
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
104  
105 4 return $res;
106 }
107  
108 5 1; # Perl is my Igor
109  
110 =pod
111  
112 =encoding UTF-8
113  
114 =head1 NAME
115  
116 Astro::ADS - Perl library to connect with the Harvard Astrophysical Data Service
117  
118 =head1 VERSION
119  
120 version 1.92
121  
122 =head1 SYNOPSIS
123  
124 my $client = Astro::ADS->new({
125 proxy => '...', # your web proxy
126 });
127  
128 my $search = $client->search( q => 'star', fl => 'bibcode' );
129  
130 =head1 DESCRIPTION
131  
132 The Harvard Astrophysics Data System (ADS) is a digital library
133 portal for researchers in astronomy and physics, maintaining
134 large bibliographic collections. Through the ADS, you can search
135 abstracts and full-text of major astronomy and physics publications.
136  
137 Astro::ADS is the base class for accessing the ADS API using Perl
138 and will grow as more services are added.
139  
140 It handles methods common to all services such as setting the UserAgent and
141 including your API key in all request headers.
142  
143 =head2 Getting Started
144  
145 If you don't have one already, you will need to register an
146 L<ADS account|https://ui.adsabs.harvard.edu/user/account/register> .
147 Generate an L<API token|https://ui.adsabs.harvard.edu/user/settings/token> and
148 put it in an environment variable named C<ADS_DEV_KEY> or in a file under your
149 home directory, B<~/.ads/dev_key>.
150  
151 Find more help on the L<Quick Start|https://ui.adsabs.harvard.edu/help/api/> page.
152  
153 =head2 Terms and Conditions
154  
155 B<NOTE: the ADS does not hold the copyright for the abstracts and articles, and their use is free for personal use only>
156  
157 Use of this module does not imply the granting of any rights to
158 the publications found through this API. Please refer to the
159 L<ADS Terms and Conditions of Use|http://adsabs.github.io/help/terms/>
160  
161 To acknowledge the ADS in a publication, refer to the text at the
162 bottom of L<About ADS|https://ui.adsabs.harvard.edu/about/>, which suggests:
163 I<“This research has made use of the Astrophysics Data System,
164 funded by NASA under Cooperative Agreement 80NSSC21M0056.”>
165  
166 To acknowledge use of this module, it will be sufficient to mention
167 I<Perl's Astro::ADS is available at https://metacpan.org/pod/Astro::ADS>
168  
169 =head1 COPYRIGHT AND LICENSE
170  
171 This software is Copyright (c) 2025 by Boyd Duffee.
172  
173 This is free software, licensed under:
174  
175 The MIT (X11) License
176  
177 =cut
« ADS/Search.pm ↑ Summary ADS/Role/ResultMapper.pm »