File Coverage

blib/lib/WebService/Jandi/WebHook.pm
Criterion Covered Total %
statement 14 23 60.8
branch 1 6 16.6
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 22 37 59.4


line stmt bran cond sub pod time code
1             package WebService::Jandi::WebHook;
2             $WebService::Jandi::WebHook::VERSION = 'v0.0.1';
3 2     2   107543 use strict;
  2         7  
  2         78  
4 2     2   17 use warnings;
  2         6  
  2         81  
5              
6 2     2   1171 use HTTP::Tiny;
  2         100057  
  2         137  
7 2     2   1104 use JSON qw/encode_json/;
  2         18327  
  2         19  
8              
9             =encoding utf8
10              
11             =head1 NAME
12              
13             WebService::Jandi::WebHook - Perl interface to Jandi Service Incoming Webhook
14              
15             =head1 SYNOPSIS
16              
17             my $jandi = WebService::Jandi::WebHook->new('https://wh.jandi.com/connect-api/webhook/md5sum');
18             my $msg = {
19             body => '[[PizzaHouse]](http://url_to_text) You have a new Pizza order.',
20             connectColor => '#FAC11B',
21             connectInfo => [
22             {
23             title => 'Topping',
24             description => 'Pepperoni',
25             },
26             {
27             title => 'Location',
28             description => 'Empire State Building, 5th Ave, New York',
29             imageUrl => 'http://url_to_text'
30             }
31             ]
32             };
33              
34             my $res = $jandi->request($msg); # HTTP::Tiny response
35              
36             or
37              
38             my $res = $jandi->request('Hello, world');
39             die "Failed!\n" unless $res->{success};
40              
41             =head1 METHODS
42              
43             =head2 new($webhook_url)
44              
45             =cut
46              
47             sub new {
48 1     1 1 81 my ( $class, $webhook_url ) = @_;
49 1 50       5 return unless $webhook_url;
50              
51 0           my $self = {
52             url => $webhook_url,
53             http => HTTP::Tiny->new(
54             default_headers => {
55             agent => 'WebService::Jandi::WebHook - Perl interface to Jandi Service Incoming Webhook',
56             accept => 'application/vnd.tosslab.jandi-v2+json',
57             'content-type' => 'application/json',
58             }
59             ),
60             };
61              
62 0           bless $self, $class;
63 0           return $self;
64             }
65              
66             =head2 request($message)
67              
68             my $res = $self->request($message);
69              
70             C<$message> is a simple string or hashref.
71              
72             C<$res> is L C<$response>.
73              
74             Hashref format.
75              
76             {
77             body => '[[PizzaHouse]](http://url_to_text) You have a new Pizza order.',
78             connectColor => '#FAC11B',
79             connectInfo => [
80             {
81             title => 'Topping',
82             description => 'Pepperoni',
83             },
84             {
85             title => 'Location',
86             description => 'Empire State Building, 5th Ave, New York',
87             imageUrl => 'http://url_to_text'
88             }
89             ]
90             }
91              
92             C and simple string support markdown link format.
93              
94             [text](url)
95              
96             =cut
97              
98             sub request {
99 0     0 1   my ( $self, $message ) = @_;
100 0 0         return unless $message;
101             ## TODO: $message validation
102              
103 0 0         $message = { body => $message } unless ref $message;
104 0           my $json = encode_json($message);
105             my $res = $self->{http}->request(
106             'POST',
107             $self->{url},
108 0           { content => $json }
109             );
110              
111 0           return $res;
112             }
113              
114             =head1 COPYRIGHT and LICENSE
115              
116             The MIT License (MIT)
117              
118             Copyright (c) 2017 Hyungsuk Hong
119              
120             =cut
121              
122             1;