File Coverage

blib/lib/WWW/ELISA.pm
Criterion Covered Total %
statement 27 52 51.9
branch n/a
condition n/a
subroutine 9 15 60.0
pod 2 2 100.0
total 38 69 55.0


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