File Coverage

blib/lib/Astro/ADS.pm
Criterion Covered Total %
statement 57 74 77.0
branch 13 30 43.3
condition n/a
subroutine 12 14 85.7
pod 0 2 0.0
total 82 120 68.3


line stmt bran cond sub pod time code
1             package Astro::ADS;
2             $Astro::ADS::VERSION = '1.92';
3 5     5   3501 use Moo;
  5         15  
  5         44  
4              
5 5     5   2444 use Carp;
  5         11  
  5         437  
6 5     5   34 use Data::Dumper::Concise;
  5         54  
  5         380  
7 5     5   3126 use Feature::Compat::Try;
  5         2247  
  5         38  
8 5     5   1105 use Mojo::Base -strict;
  5         12  
  5         47  
9 5     5   792 use Mojo::URL;
  5         11  
  5         39  
10 5     5   2833 use Mojo::UserAgent;
  5         1246956  
  5         47  
11 5     5   277 use Types::Standard qw( StrMatch );
  5         10  
  5         62  
12              
13             # suppress warning for native perl 5.36 try/catch
14 5     5   9006 no if $] >= 5.018, 'warnings', 'experimental';
  5         31  
  5         7688  
15              
16             my $DEBUG = 0;
17              
18             has ua => ( is => 'lazy' );
19             has proxy => ( is => 'lazy' );
20             has token => ( is => 'lazy', isa => StrMatch[qr/\A\w{20,50}\z/] );
21              
22             has base_url => (
23             is => 'ro',
24             default => sub { Mojo::URL->new('https://api.adsabs.harvard.edu/v1/') },
25             );
26              
27             sub _build_ua {
28 0     0   0 my ($self) = @_;
29 0         0 return Mojo::UserAgent->new;
30             }
31              
32             sub _build_proxy {
33 0     0   0 my ($self, $proxy) = @_;
34 0         0 $self->ua->proxy->https( $proxy );
35 0         0 return $proxy;
36             }
37              
38             sub _build_token {
39 7     7   940 my ($self) = @_;
40              
41 7 100       143 return $ENV{ADS_DEV_KEY} if $ENV{ADS_DEV_KEY};
42              
43             # consider using File::HomeDir
44 1         34 my $dev_key_file = Mojo::File->new($ENV{HOME} . '/.ads/dev_key');
45 1 50       27 if (-e $dev_key_file) {
46 0         0 my $key = $dev_key_file->slurp;
47 0         0 chomp $key; # remove this line to create a Bad Request
48 0         0 return $key;
49             }
50              
51 1         557 croak "You need to provide an API token.\nSee https://metacpan.org/pod/Astro::ADS#Getting-Started\n";
52             }
53              
54             sub get_response {
55 8     8 0 28 my ($self, $url) = @_;
56              
57 8         284 my $tx = $self->ua->build_tx( GET => $url );
58 8         1940 $tx->req->headers->authorization( 'Bearer ' . $self->token );
59 8 50       490 warn $tx->req->to_string if $DEBUG;
60            
61 8         23 try { $tx = $self->ua->start($tx) }
  8         172  
62             catch ($error) {
63 0         0 carp "Got this error: ", $error;
64             }
65              
66 8         173713 my $res;
67 8         24 try { $res = $tx->result } # call to result dies on connection error
  8         59  
68             catch ($error) {
69 0         0 carp "Connection error: ", $error;
70 0         0 return;
71             }
72 8 50       294 if ($res->is_success) { warn $res->body if $DEBUG > 1 }
  7 100       135  
    50          
    0          
73 1         50 elsif ($res->is_error) { carp 'HTTP Error: ', $res->message }
74 0 0       0 elsif ($res->code == 301) { carp 'Redirected: ', $res->headers->location if $DEBUG }
75              
76 8         343 return $res;
77             }
78              
79             sub post_response {
80 4     4 0 12 my ($self, $url, $content) = @_;
81              
82             # this will work until we need to do form submission
83             # hashrefs are sent as JSON
84 4 100       146 my $tx = ref $content eq 'HASH'
85             ? $self->ua->build_tx( POST => $url, json => $content )
86             : $self->ua->build_tx( POST => $url, $content );
87 4         963 $tx->req->headers->authorization( 'Bearer ' . $self->token );
88 4 50       173 carp "Request sent to $url\n\n", $tx->req->to_string if $DEBUG;
89            
90 4         8 try { $tx = $self->ua->start($tx) }
  4         58  
91             catch ($error) {
92 0         0 carp "Got this error: ", $error;
93             }
94              
95 4         37619 my $res;
96 4         9 try { $res = $tx->result } # call to result dies on connection error
  4         16  
97             catch ($error) {
98 0         0 carp "Connection error: ", $error;
99 0         0 return;
100             }
101 4 50       84 if ($res->is_success) { warn $res->body if $DEBUG > 1 }
  4 50       57  
    0          
    0          
102 0         0 elsif ($res->is_error) { carp 'HTTP Error: ', $res->message }
103 0 0       0 elsif ($res->code == 301) { carp 'Redirected: ', $res->headers->location if $DEBUG }
104              
105 4         16 return $res;
106             }
107              
108             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