File Coverage

blib/lib/WWW/ELISA.pm
Criterion Covered Total %
statement 27 55 49.0
branch n/a
condition 0 4 0.0
subroutine 9 16 56.2
pod 2 4 50.0
total 38 79 48.1


line stmt bran cond sub pod time code
1             package WWW::ELISA;
2              
3             our $VERSION = '0.07';
4              
5 1     1   74931 use strict;
  1         11  
  1         32  
6 1     1   6 use warnings;
  1         2  
  1         25  
7 1     1   5 use Carp;
  1         2  
  1         70  
8 1     1   6 use Digest::MD5 qw( md5_hex );
  1         1  
  1         62  
9 1     1   465 use HTTP::Request;
  1         20547  
  1         41  
10 1     1   799 use JSON::XS;
  1         5651  
  1         82  
11 1     1   692 use LWP::UserAgent;
  1         27544  
  1         54  
12 1     1   13 use Try::Tiny;
  1         2  
  1         596  
13              
14             sub new {
15 2     2 1 1088 my $class = shift;
16 2         9 my %args = (endpoint => 'https://elisa.hbz-nrw.de:8091/api/rest', @_,);
17              
18 2         13 return bless {%args}, $class;
19             }
20              
21             # leave it for backwards compatibility
22             sub push {
23 0     0 1   my ($self, $notepad) = @_;
24              
25 0           $self->create_notepad($notepad);
26             }
27              
28             sub create_notepad {
29 0     0 0   my ($self, $notepad) = @_;
30              
31             my $data = {
32             userID => $notepad->{userID},
33             token => $self->_authenticate(),
34             notepadName => $notepad->{notepadName},
35             titleList => $notepad->{titleList},
36 0   0       available_only => $notepad->{available_only} // "false",
37             };
38              
39 0           my $response = $self->_do_request("/createNotepad", $data);
40             }
41              
42             sub create_basket {
43 0     0 0   my ($self, $notepad) = @_;
44              
45             my $data = {
46             userID => $notepad->{userID},
47             token => $self->_authenticate(),
48             notepadName => $notepad->{notepadName},
49             titleList => $notepad->{titleList},
50 0   0       available_only => $notepad->{available_only} // "false",
51             };
52              
53 0           my $response = $self->_do_request("/createBasket", $data);
54             }
55              
56             sub _authenticate {
57 0     0     my $self = shift;
58              
59 0           my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
60             = localtime(time);
61              
62 0           my $timestamp = sprintf(
63             "%04d-%02d-%02dT%02d:%02d:%02dZ",
64             $year + 1900,
65             $mon + 1, $mday, $hour, $min, $sec
66             );
67 0           my $timestamp2 = sprintf(
68             "%04d%02d%02dT%02d%02d%02dZ",
69             $year + 1900,
70             $mon + 1, $mday, $hour, $min, $sec
71             );
72              
73             my $auth = {
74             callerID => $self->{callerID},
75             timestamp => $timestamp,
76 0           hash => md5_hex($self->{callerID} . $timestamp2 . $self->{secret}),
77             };
78              
79 0           my $res = $self->_do_request("/authenticate", $auth);
80 0           return $res->{token};
81             }
82              
83             sub _do_request {
84 0     0     my ($self, $path, $content) = @_;
85              
86 0           my $endpoint = $self->{endpoint};
87              
88             try {
89 0     0     my $ua = LWP::UserAgent->new;
90 0           $ua->timeout(20);
91 0           $ua->env_proxy;
92 0           $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
93              
94 0           my $req = HTTP::Request->new('POST', $endpoint . $path);
95 0           $req->header('Content-Type' => 'application/json');
96 0           $req->content(encode_json($content));
97              
98 0           my $res = $ua->request($req);
99              
100 0           return decode_json($res->decoded_content);
101             }
102             catch {
103 0     0     print STDERR "Error: " . $_;
104             }
105 0           }
106              
107             1;
108              
109             __END__