blib/lib/EZID.pm untested

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