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   207881 use 5.010001;
  6         26  
3 6     6   27 use strict;
  6         13  
  6         117  
4 6     6   35 use warnings;
  6         10  
  6         210  
5 6     6   32 use Carp qw/croak/;
  6         10  
  6         316  
6 6     6   2890 use JSON qw/decode_json/;
  6         44441  
  6         33  
7              
8             our $VERSION = "0.021";
9              
10             sub new {
11 44     44 0 96 my ($class,$pixela_client) = @_;
12 44         125 return bless +{
13             client => $pixela_client,
14             }, $class;
15             }
16              
17              
18             sub client {
19 22     22 0 51 my $self = shift;
20 22         62 return $self->{client};
21             }
22              
23             sub hash {
24 16     16 1 1591 my $self = shift;
25 16 100       37 if (@_){
26 6         13 $self->{hash} = shift;
27 6         16 return $self;
28             }
29 10         34 return $self->{hash};
30             }
31              
32             sub create {
33 6     6 1 1111 my ($self,%args) = @_;
34 6         13 my $params = {};
35              
36             #check graphID
37 6   100     23 $params->{graphID} = $args{graph_id} // $self->client->graph->id;
38 6 100       182 croak 'require graph_id' unless $params->{graphID};
39              
40             #check type
41 5 100       84 croak 'require type' unless $args{type};
42             map {
43 4 100       7 if( $args{type} =~ /^$_$/i){
  8         105  
44 3         17 $params->{type} = lc($args{type});
45             }
46             } (qw/increment decrement/);
47 4 100       86 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       27 my $res_json = $self->client->decode() ? $res : decode_json($res);
53              
54 3 50       9 if($res_json->{isSuccess}){
55 3         7 $self->hash($res_json->{webhookHash});
56             }
57              
58 3         23 return $res;
59             }
60              
61             sub get {
62 2     2 1 24 my $self = shift;
63 2         6 my $client = $self->client;
64              
65 2         5 my $path = 'users/'.$client->username.'/webhooks/';
66 2         14 my $res = $client->request_with_xuser_in_header('GET',$path);
67              
68 2 100       16 return $client->decode() ? $res->{webhooks} : $res;
69             }
70              
71             sub invoke {
72 2     2 1 449 my ($self,$hash) = @_;
73 2         5 my $client = $self->client;
74              
75 2   66     8 $hash //= $self->hash();
76 2 50       6 croak 'require webhookHash' unless $hash;
77              
78 2         7 my $path = 'users/'.$client->username.'/webhooks/'.$hash;
79 2         15 return $client->request_with_content_length_in_header('POST',$path,0);
80             }
81              
82             sub delete {
83 4     4 1 795 my ($self,$hash) = @_;
84 4         33 my $client = $self->client;
85              
86 4   100     16 $hash //= $self->hash();
87 4 100       175 croak 'require webhookHash' unless $hash;
88              
89 2         7 my $path = 'users/'.$client->username.'/webhooks/'.$hash;
90 2         13 return $self->client->request_with_xuser_in_header('DELETE',$path);
91             }
92              
93              
94             1;
95             __END__