File Coverage

blib/lib/Plack/Auth/SSO/ResponseParser/ORCID.pm
Criterion Covered Total %
statement 54 58 93.1
branch 12 22 54.5
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 75 91 82.4


line stmt bran cond sub pod time code
1             package Plack::Auth::SSO::ResponseParser::ORCID;
2              
3 2     2   94596 use strict;
  2         11  
  2         48  
4 2     2   441 use utf8;
  2         14  
  2         10  
5 2     2   392 use Data::Util qw(:check);
  2         546  
  2         264  
6 2     2   11 use JSON;
  2         3  
  2         11  
7 2     2   562 use Moo;
  2         9226  
  2         11  
8 2     2   2268 use Clone qw();
  2         3836  
  2         886  
9              
10             our $VERSION = "0.0137";
11              
12             with "Plack::Auth::SSO::ResponseParser";
13              
14             has json => (
15                 is => "ro",
16                 lazy => 1,
17                 builder => "_build_json",
18                 init_arg => undef
19             );
20              
21             sub _build_json {
22 1     1   41     JSON->new();
23             }
24              
25             sub dig {
26              
27 11     11 0 21     my( $hash, @keys ) = @_;
28              
29 11         21     while( my $key = shift(@keys) ){
30 23 50       40         return unless exists $hash->{$key};
31 23 100       64         return $hash->{$key} if scalar(@keys) == 0;
32 12         16         $hash = $hash->{$key};
33 12 50       30         return unless is_hash_ref($hash);
34                 }
35              
36 0         0     return;
37             }
38              
39             sub parse {
40              
41 1     1 0 1563     my ( $self, $obj, $obj2 ) = @_;
42              
43 1 50       5     if ( is_string( $obj ) ) {
44              
45 1         17         $obj = $self->json()->decode( $obj );
46              
47                 }
48                 else {
49              
50 0         0         $obj = Clone::clone( $obj );
51              
52                 }
53              
54 1 50       4     if ( is_string( $obj2 ) ) {
55              
56 1         17         $obj2 = $self->json()->decode( $obj2 );
57              
58                 }
59                 else {
60              
61 0         0         $obj2 = Clone::clone( $obj2 );
62              
63                 }
64              
65 1         81     my $uid = delete $obj->{orcid};
66 1         3     my $name = delete $obj->{name};
67              
68 1         3     my $info = +{ name => $name };
69 1         4     $info->{first_name} = dig( $obj2, "name","given-names","value" );
70 1         3     $info->{last_name} = dig( $obj2, "name", "family-name", "value" );
71 1 50       3     $info->{other_names} = [ grep { defined($_) } map { dig( $_, "content" ); } @{ dig( $obj2, "other-names", "other-name" ) || [] } ];
  1         4  
  1         2  
  1         2  
72 1         3     $info->{description} = dig( $obj2, "biography", "content" );
73 1 50       2     $info->{location} = [ grep { defined($_) } map { dig($_,"country","value") } @{ dig( $obj2, "addresses", "address" ) || [] } ]->[0];
  1         4  
  1         2  
  1         3  
74 1 50       11     $info->{email} = [ map { $_->{email} } grep { $_->{verified} && $_->{primary} } @{ dig( $obj2, "emails", "email" ) || [] } ]->[0];
  1 50       22  
  1         60  
  1         2  
75 1 50       3     $info->{urls} = [ map { +{ $_->{"url-name"} => dig($_,"url","value") } } @{ dig( $obj2, "researcher-urls", "researcher-url" ) || [] } ];
  1         2  
  1         3  
76                 $info->{external_identifiers} = [ map { +{
77                     type => $_->{"external-id-type"},
78 0         0         value => $_->{"external-id-value"},
79                     url => dig($_,"external-id-url","value")
80 1 50       3     } } @{ dig( $obj2, "external-identifiers", "external-identifier" ) || [] } ];
  1         3  
81              
82                 +{
83 1         14         uid => $uid,
84                     info => $info,
85                     extra => { %$obj2, %$obj }
86                 };
87              
88             }
89              
90             1;
91