File Coverage

blib/lib/WebService/Pixela/Webhook.pm
Criterion Covered Total %
statement 55 55 100.0
branch 18 20 90.0
condition 8 9 88.8
subroutine 12 12 100.0
pod 5 7 71.4
total 98 103 95.1


line stmt bran cond sub pod time code
1             package WebService::Pixela::Webhook;
2 6     6   212610 use 5.010001;
  6         25  
3 6     6   30 use strict;
  6         15  
  6         146  
4 6     6   32 use warnings;
  6         12  
  6         176  
5 6     6   33 use Carp qw/croak/;
  6         11  
  6         341  
6 6     6   3169 use JSON qw/decode_json/;
  6         48051  
  6         41  
7              
8             our $VERSION = "0.01";
9              
10             sub new {
11 43     43 0 96 my ($class,$pixela_client) = @_;
12 43         126 return bless +{
13             client => $pixela_client,
14             }, $class;
15             }
16              
17              
18             sub client {
19 22     22 0 47 my $self = shift;
20 22         66 return $self->{client};
21             }
22              
23             sub hash {
24 16     16 1 1715 my $self = shift;
25 16 100       41 if (@_){
26 6         12 $self->{hash} = shift;
27 6         15 return $self;
28             }
29 10         37 return $self->{hash};
30             }
31              
32             sub create {
33 6     6 1 1058 my ($self,%args) = @_;
34 6         14 my $params = {};
35              
36             #check graphID
37 6   100     25 $params->{graphID} = $args{graph_id} // $self->client->graph->id;
38 6 100       163 croak 'require graph_id' unless $params->{graphID};
39              
40             #check type
41 5 100       81 croak 'require type' unless $args{type};
42             map {
43 4 100       9 if( $args{type} =~ /^$_$/i){
  8         100  
44 3         18 $params->{type} = lc($args{type});
45             }
46             } (qw/increment decrement/);
47 4 100       104 croak 'invalid type' unless $params->{type};
48              
49 3         8 my $path = 'users/'.$self->client->username.'/webhooks';
50 3         21 my $res = $self->client->request_with_xuser_in_header('POST',$path,$params);
51              
52 3 100       25 my $res_json = $self->client->decode() ? $res : decode_json($res);
53              
54 3 50       8 if($res_json->{isSuccess}){
55 3         7 $self->hash($res_json->{webhookHash});
56             }
57              
58 3         21 return $res;
59             }
60              
61             sub get {
62 2     2 1 24 my $self = shift;
63 2         5 my $client = $self->client;
64              
65 2         7 my $path = 'users/'.$client->username.'/webhooks/';
66 2         14 my $res = $client->request_with_xuser_in_header('GET',$path);
67              
68 2 100       14 return $client->decode() ? $res->{webhooks} : $res;
69             }
70              
71             sub invoke {
72 2     2 1 452 my ($self,$hash) = @_;
73 2         5 my $client = $self->client;
74              
75 2   66     8 $hash //= $self->hash();
76 2 50       7 croak 'require webhookHash' unless $hash;
77              
78 2         5 my $path = 'users/'.$client->username.'/webhooks/'.$hash;
79 2         16 return $client->request_with_content_length_in_header('POST',$path,0);
80             }
81              
82             sub delete {
83 4     4 1 503 my ($self,$hash) = @_;
84 4         32 my $client = $self->client;
85              
86 4   100     18 $hash //= $self->hash();
87 4 100       178 croak 'require webhookHash' unless $hash;
88              
89 2         6 my $path = 'users/'.$client->username.'/webhooks/'.$hash;
90 2         14 return $self->client->request_with_xuser_in_header('DELETE',$path);
91             }
92              
93              
94             1;
95             __END__