blib/lib/EZID.pm

filter: Statementstmt 76.3% 42 / 55 Branchbran 62.5% 5 / 8 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 4 totaltotal 69.0% 58 / 84 CC 7 SCAR 8.0
CC
7
Cov
73%
create
3.1
new
3.1
get
3.0
CRAP 2.2
↑ Summary
1 package EZID;
2  
3 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.02';
11  
12 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod undocumented
13 1 my ($class, $args) = @_;
14  
15 1 ▶ $args //= {};
Condition50%1/2
$args //= {}
l!l
10
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
16  
17 1 ▶ return unless (ref $args eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 1 1
18  
19 my $self = {
20 _username => $args->{username},
21 _password => $args->{password},
22 1 };
23  
24 1 return bless $self, $class;
25 }
26  
27 0 ▶sub _parse {
Subroutine _parse: not called CC=1 SCAR=6.9
28 0 my ($data) = @_;
29  
30 return map {
31 0 map {
32 0 my $a = $_;
33 0 $a =~ s/%([0-9A-F]{2})/pack("C", hex($1))/egi;
34 0 $a
35 } split(/: /, $_, 2)
36 } split(/\n/, $data)
37 }
38  
39 4 ▶sub get {
Subroutine get: called CC=3 SCAR=11.1
Pod undocumented
40 4 my ($self, $identifier) = @_;
41  
42 4 ▶ return unless $identifier;
Branch: 1 branch
BranchTrueFalseTotal
unless $identifier 1 3 4
43 3 my $response;
44 my %metadata;
45  
46 3 my $ua = LWP::UserAgent->new;
47 3 my $r = $ua->get("http://ezid.cdlib.org/id/$identifier");
48 3 ▶ if ($r->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_success) { } 0 3 3
49 0 $response = { _parse($r->decoded_content) };
50 } else {
51 3 $self->{_error_msg} = $r->decoded_content;
52 }
53  
54 3 return $response;
55 }
56  
57 0 ▶sub _escape {
Subroutine _escape: not called CC=1 SCAR=6.9
58 0 (my $s = $_[0]) =~ s/([%:\r\n])/uri_escape($1)/eg;
59 0 return $s;
60 }
61  
62 2 ▶sub create {
Subroutine create: called CC=3 SCAR=11.4
Pod undocumented
63 2 my ($self, $identifier, $metadata) = @_;
64  
65 2 ▶ $metadata //= {};
Condition50%1/2
$metadata //= {}
l!l
02
MC/DC0%0/1
$metadata //= {}
$metadata
Truth table50%1/2
A
A= $metadata
Aresult
11
00
66  
67 my $content = encode("UTF-8", join("\n",
68 0 map { escape($_) . ": " . escape($metadata->{$_}) } keys %$metadata));
69  
70 2 my $ua = LWP::UserAgent->new;
71 $ua->credentials("ezid.cdlib.org:443", "EZID", $self->{_username},
72 2 $self->{_password});
73 2 my $r = $ua->request(PUT "https://ezid.cdlib.org/id/$identifier",
74 'Content-Type' => "text/plain; charset=UTF-8",
75 'Content' => $content);
76  
77 2 my $response;
78 2 ▶ if ($r->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_success) { } 0 2 2
79 0 $response = { _parse($r->decoded_content) };
80 } else {
81 2 $self->{_error_msg} = $r->decoded_content;
82 }
83  
84 2 return $response;
85 }
86  
87 2 ▶sub error_msg {
Subroutine error_msg: called CC=1 SCAR=0.0
Pod undocumented
88 2 my ($self) = @_;
89  
90 2 return $self->{_error_msg};
91 }
92  
93 1 1;
94 __END__
↑ Summary