| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package EZID; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 28801 | use Modern::Perl; | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 721 | use Encode; | 
|  | 1 |  |  |  |  | 8451 |  | 
|  | 1 |  |  |  |  | 81 |  | 
| 6 | 1 |  |  | 1 |  | 526 | use HTTP::Request::Common; | 
|  | 1 |  |  |  |  | 29278 |  | 
|  | 1 |  |  |  |  | 70 |  | 
| 7 | 1 |  |  | 1 |  | 1808 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 34484 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 8 | 1 |  |  | 1 |  | 7 | use URI::Escape; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 633 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new { | 
| 13 | 1 |  |  | 1 | 0 | 11 | my ($class, $args) = @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 1 |  | 50 |  |  | 3 | $args //= {}; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 1 | 50 |  |  |  | 5 | return unless (ref $args eq 'HASH'); | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  |  |  | 5 | my $self = { | 
| 20 |  |  |  |  |  |  | _username => $args->{username}, | 
| 21 |  |  |  |  |  |  | _password => $args->{password}, | 
| 22 |  |  |  |  |  |  | }; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 1 |  |  |  |  | 3 | return bless $self, $class; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub _parse { | 
| 28 | 0 |  |  | 0 |  | 0 | my ($data) = @_; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 0 |  |  |  |  | 0 | return map { | 
| 31 | 0 |  |  |  |  | 0 | map { | 
| 32 | 0 |  |  |  |  | 0 | my $a = $_; | 
| 33 | 0 |  |  |  |  | 0 | $a =~ s/%([0-9A-F]{2})/pack("C", hex($1))/egi; | 
|  | 0 |  |  |  |  | 0 |  | 
| 34 | 0 |  |  |  |  | 0 | $a | 
| 35 |  |  |  |  |  |  | } split(/: /, $_, 2) | 
| 36 |  |  |  |  |  |  | } split(/\n/, $data) | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub get { | 
| 40 | 4 |  |  | 4 | 0 | 1375 | my ($self, $identifier) = @_; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 4 | 100 |  |  |  | 14 | return unless $identifier; | 
| 43 | 3 |  |  |  |  | 6 | my $response; | 
| 44 |  |  |  |  |  |  | my %metadata; | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 3 |  |  |  |  | 21 | my $ua = LWP::UserAgent->new; | 
| 47 | 3 |  |  |  |  | 5166 | my $r = $ua->get("http://ezid.cdlib.org/id/$identifier"); | 
| 48 | 3 | 50 |  |  |  | 1552113 | if ($r->is_success) { | 
| 49 | 0 |  |  |  |  | 0 | $response = { _parse($r->decoded_content) }; | 
| 50 |  |  |  |  |  |  | } else { | 
| 51 | 3 |  |  |  |  | 54 | $self->{_error_msg} = $r->decoded_content; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 3 |  |  |  |  | 3379 | return $response; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub _escape { | 
| 58 | 0 |  |  | 0 |  | 0 | (my $s = $_[0]) =~ s/([%:\r\n])/uri_escape($1)/eg; | 
|  | 0 |  |  |  |  | 0 |  | 
| 59 | 0 |  |  |  |  | 0 | return $s; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub create { | 
| 63 | 2 |  |  | 2 | 0 | 879 | my ($self, $identifier, $metadata) = @_; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 2 |  | 50 |  |  | 17 | $metadata //= {}; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 |  |  |  |  | 0 | my $content = encode("UTF-8", join("\n", | 
| 68 | 2 |  |  |  |  | 14 | map { escape($_) . ": " . escape($metadata->{$_}) } keys %$metadata)); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 2 |  |  |  |  | 276 | my $ua = LWP::UserAgent->new; | 
| 71 | 2 |  |  |  |  | 447 | $ua->credentials("ezid.cdlib.org:443", "EZID", $self->{_username}, | 
| 72 |  |  |  |  |  |  | $self->{_password}); | 
| 73 | 2 |  |  |  |  | 40 | 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 |  |  |  |  | 4532 | my $response; | 
| 78 | 2 | 50 |  |  |  | 9 | if ($r->is_success) { | 
| 79 | 0 |  |  |  |  | 0 | $response = { _parse($r->decoded_content) }; | 
| 80 |  |  |  |  |  |  | } else { | 
| 81 | 2 |  |  |  |  | 24 | $self->{_error_msg} = $r->decoded_content; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 2 |  |  |  |  | 927 | return $response; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub error_msg { | 
| 88 | 2 |  |  | 2 | 0 | 1144 | my ($self) = @_; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 2 |  |  |  |  | 21 | return $self->{_error_msg}; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | 1; | 
| 94 |  |  |  |  |  |  | __END__ |