blib/lib/WWW/ORCID/API.pm

filter: Statementstmt 42.0% 53 / 126 Branchbran 15.9% 7 / 44 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 39.1% 9 / 23 Podpod 0.0% 0 / 7 totaltotal 32.8% 72 / 219 SCAR 55.8
CC
29
Cov
35%
_url
30.0
_headers
20.0
client
20.0
CRAP 264.1
↑ Summary blib/lib/WWW/ORCID/MemberAPI.pm »
1 package WWW::ORCID::API;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = 0.02_01;
7  
8 4 ▶use Class::Load qw(try_load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 with 'WWW::ORCID::Base';
16  
17 4 requires 'ops';
18 4 requires '_build_api_url';
19  
20 4 has sandbox => (is => 'ro',);
21 4 has client_id => (is => 'ro', required => 1);
22 4 has client_secret => (is => 'ro', required => 1);
23 4 has api_url => (is => 'lazy',);
24 4 has oauth_url => (is => 'lazy');
25 4 has read_public_token => (is => 'lazy');
26 4 has transport => (is => 'lazy',);
27 4 has last_error => (
28 is => 'rwp',
29 init_arg => undef,
30 clearer => '_clear_last_error',
31 trigger => 1
32 );
33 4 has _t => (is => 'lazy',);
34  
35 sub _build_oauth_url {
36 0 ▶ $_[0]->sandbox
Subroutine _build_oauth_url: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->sandbox ? : 0 0 0
37 ? 'https://sandbox.orcid.org/oauth'
38 : 'https://orcid.org/oauth';
39 }
40  
41 sub _build_read_public_token {
42 0 ▶ $_[0]->access_token(
Subroutine _build_read_public_token: not called CC=1 SCAR=6.9
43 grant_type => 'client_credentials',
44 scope => '/read-public'
45 );
46 }
47  
48 sub access_token {
49 0 ▶ my $self = shift;
Subroutine access_token: not called CC=3 SCAR=24.8
Pod undocumented
50 0 $self->_clear_last_error;
51 0 ▶ my $opts = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
52 0 $opts->{client_id} = $self->client_id;
53 0 $opts->{client_secret} = $self->client_secret;
54 0 my $url = join('/', $self->oauth_url, 'token');
55 0 my $headers = {'Accept' => 'application/json'};
56 0 my $res = $self->_t->post_form($url, $opts, $headers);
57  
58 0 ▶ if ($res->[0] eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] eq 200) 0 0 0
59 0 return decode_json($res->[2]);
60 }
61 0 $self->_set_last_error($res);
62 0 return;
63 }
64  
65 sub authorize_url {
66 0 ▶ my $self = shift;
Subroutine authorize_url: not called CC=2 SCAR=17.9
Pod undocumented
67 0 ▶ my $opts = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
68 0 $opts->{client_id} = $self->client_id;
69 0 $self->_param_url(join('/', $self->oauth_url, 'authorize'), $opts);
70 }
71  
72 sub record_url {
73 0 ▶ my ($self, $orcid) = @_;
Subroutine record_url: not called CC=2 SCAR=17.9
Pod undocumented
74 0 ▶ $self->sandbox
Branch: 1 branch
BranchTrueFalseTotal
$self->sandbox ? : 0 0 0
75 ? "http://sandbox.orcid.org/$orcid"
76 : "http://orcid.org/$orcid";
77 }
78  
79 sub _build_transport {
80 0 ▶ 'LWP';
Subroutine _build_transport: not called CC=1 SCAR=6.9
81 }
82  
83 sub _build__t {
84 0 ▶ my ($self) = @_;
Subroutine _build__t: not called CC=2 SCAR=17.9
85 0 my $transport = $self->transport;
86 0 my $transport_class = "WWW::ORCID::Transport::${transport}";
87 0 ▶ try_load_class($transport_class)
Branch: 1 branch
BranchTrueFalseTotal
try_load_class($transport_class) or croak("Could not load ${transport_class}: $!") 0 0 0
88 or croak("Could not load $transport_class: $!");
89 0 $transport_class->new;
90 }
91  
92 sub _trigger_last_error {
93 0 ▶ my ($self, $res) = @_;
Subroutine _trigger_last_error: not called CC=2 SCAR=17.9
94 0 ▶ $self->log->errorf("%s", $res) if $self->log->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->log->is_error 0 0 0
95 }
96  
97 sub _url {
98 0 ▶ my ($host, $path, $opts) = @_;
Subroutine _url: not called CC=5 SCAR=34.0
99 0 ▶ $path = join('/', @$path) if ref $path;
Branch: 1 branch
BranchTrueFalseTotal
if ref $path 0 0 0
100 0 $path =~ s|_summary$|/summary|;
101 0 $path =~ s|_|-|g;
102 0 ▶ if (defined(my $orcid = $opts->{orcid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $orcid = $opts->{"orcid"})) 0 0 0
103 0 $path = "$orcid/$path";
104 }
105 0 ▶ if (defined(my $put_code = $opts->{put_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $put_code = $opts->{"put_code"})) 0 0 0
106 0 ▶ $put_code = join(',', @$put_code) if ref $put_code;
Branch: 1 branch
BranchTrueFalseTotal
if ref $put_code 0 0 0
107 0 $path = "$path/$put_code";
108 }
109 0 join('/', $host, $path);
110 }
111  
112 sub _headers {
113 0 ▶ my ($opts, $add_accept, $add_content_type) = @_;
Subroutine _headers: not called CC=4 SCAR=30.0
114 0 my $token = $opts->{token};
115 0 ▶ $token = $token->{access_token} if ref $token;
Branch: 1 branch
BranchTrueFalseTotal
if ref $token 0 0 0
116 0 my $headers = {'Authorization' => "Bearer $token",};
117 0 ▶ if ($add_accept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_accept) 0 0 0
118 0 $headers->{'Accept'} = 'application/vnd.orcid+json';
119 }
120 0 ▶ if ($add_content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_content_type) 0 0 0
121 0 $headers->{'Content-Type'} = 'application/vnd.orcid+json';
122 }
123 0 $headers;
124 }
125  
126 sub _clean {
127 0 ▶ my ($opts) = @_;
Subroutine _clean: not called CC=2 SCAR=17.9
128 0 delete $opts->{$_} for qw(orcid token put_code);
129 0 $opts;
130 }
131  
132 sub client {
133 0 ▶ my $self = shift;
Subroutine client: not called CC=4 SCAR=30.0
Pod undocumented
134 0 $self->_clear_last_error;
135 0 ▶ my $opts = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
136 0 ▶ $opts->{token} ||= $self->read_public_token;
Condition0%0/3
$opts->{"token"} ||= $self->read_public_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"token"} ||= $self->read_public_token
$opts->{"token"} $self->read_public_token
Truth table0%0/3
A ||= B
A= $opts->{"token"}B= $self->read_public_token
ABresult
1X1
011
000
137 0 my $url = join('/', $self->api_url, 'client', $self->client_id);
138 0 my $res = $self->_t->get($url, undef, _headers($opts, 1, 0));
139 0 ▶ if ($res->[0] eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] eq 200) 0 0 0
140 0 return decode_json($res->[2]);
141 }
142 0 $self->_set_last_error($res);
143 0 return;
144 }
145  
146 sub search {
147 0 ▶ shift->get('search', @_);
Subroutine search: not called CC=1 SCAR=6.9
Pod undocumented
148 }
149  
150 sub get {
151 0 ▶ my $self = shift;
Subroutine get: not called CC=4 SCAR=30.0
Pod undocumented
152 0 $self->_clear_last_error;
153 0 my $path = shift;
154 0 ▶ my $opts = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
155 0 ▶ $opts->{token} ||= $self->read_public_token;
Condition0%0/3
$opts->{"token"} ||= $self->read_public_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"token"} ||= $self->read_public_token
$opts->{"token"} $self->read_public_token
Truth table0%0/3
A ||= B
A= $opts->{"token"}B= $self->read_public_token
ABresult
1X1
011
000
156 0 my $url = _url($self->api_url, $path, $opts);
157 0 my $headers = _headers($opts, 1, 0);
158 0 my $res = $self->_t->get($url, _clean($opts), $headers);
159  
160 0 ▶ if ($res->[0] eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] eq 200) 0 0 0
161 0 return decode_json($res->[2]);
162 }
163 0 $self->_set_last_error($res);
164 0 return;
165 }
166  
167 sub install_helper_methods {
168 2 ▶ my $class = $_[0];
Subroutine install_helper_methods: called CC=8 SCAR=21.0
Pod undocumented
169 2 my $ops = $class->ops;
170 2 for my $op (sort keys %$ops) {
171 51 my $spec = $ops->{$op};
172 51 my $sym = $op;
173 51 $sym =~ s|[-/]|_|g;
174  
175 51 ▶ if ($spec->{get} || $spec->{get_pc} || $spec->{get_pc_bulk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"get"} or $spec->{"get_pc"} or $spec->{"get_pc_bulk"}) 20 0 20
Condition50%3/6
$spec->{"get"} or $spec->{"get_pc"} or $spec->{"get_pc_bulk"}
l!l&&r!l&&!r
5100
$spec->{"get"} or $spec->{"get_pc"}
l!l&&r!l&&!r
31200
MC/DC0%0/3
$spec->{"get"} or $spec->{"get_pc"} or $spec->{"get_pc_bulk"}
$spec->{"get"} $spec->{"get_pc"} $spec->{"get_pc_bulk"}
Truth table50%2/4
A or B or C
A= $spec->{"get"}B= $spec->{"get_pc"}C= $spec->{"get_pc_bulk"}
ABCresult
1XX1
01X1
0011
0000
176 51 quote_sub("${class}::${sym}", qq|shift->get('${op}', \@_)|);
177 }
178  
179 51 ▶ if ($spec->{add}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"add"}) 11 40 51
180 11 quote_sub("${class}::add_${sym}", qq|shift->add('${op}', \@_)|);
181 }
182  
183 51 ▶ if ($spec->{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"update"}) 11 40 51
184 11 quote_sub("${class}::update_${sym}",
185 qq|shift->update('${op}', \@_)|);
186 }
187  
188 51 ▶ if ($spec->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"delete"}) 12 39 51
189 12 quote_sub("${class}::delete_${sym}",
190 qq|shift->delete('${op}', \@_)|);
191 }
192 }
193  
194 2 1;
195 }
196  
197 4 1;
↑ Summary blib/lib/WWW/ORCID/MemberAPI.pm »