| 1 | package WebService::Slack::IncomingWebHook; | ||||||||||
| 2 | 3 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Furl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | our $VERSION = "0.02"; | |||||||||
| 12 | |||||||||||
| 13 | 12 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 14 | 12 | my ($class, %args) = @_; | |||||||||
| 15 | 12 | ▶ | Carp::croak('required webhook url') if ! exists $args{webhook_url}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | |||||||||||
| 17 | my $self = bless { | ||||||||||
| 18 | 11 | map { ( $_ => $args{$_} ) } qw( webhook_url channel icon_emoji icon_url username ) | |||||||||
| 19 | } => $class; | ||||||||||
| 20 | |||||||||||
| 21 | 11 | $self->{json} = JSON->new->utf8; | |||||||||
| 22 | 11 | $self->{furl} = Furl->new(agent => "$class.$VERSION"); | |||||||||
| 23 | |||||||||||
| 24 | 11 | return $self; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 5 | ▶ | sub post { | ||||||||
|
Subroutine
post: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 28 | 5 | my ($self, %args) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 5 | my $post_data = $self->_make_post_data(%args); | |||||||||
| 31 | |||||||||||
| 32 | my $res = $self->{furl}->post( | ||||||||||
| 33 | $self->{webhook_url}, | ||||||||||
| 34 | ['Content-Type' => 'application/json'], | ||||||||||
| 35 | 5 | $self->{json}->encode($post_data), | |||||||||
| 36 | ); | ||||||||||
| 37 | 5 | ▶ | if (! $res->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 5 | Carp::carp('post failed: '. $res->body); | |||||||||
| 39 | } | ||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 11 | ▶ | sub _make_post_data { | ||||||||
|
Subroutine
_make_post_data: called CC=3 SCAR=11.0
| |||||||||||
| 43 | 11 | my ($self, %args) = @_; | |||||||||
| 44 | return +{ | ||||||||||
| 45 | ( | ||||||||||
| 46 | map { | ||||||||||
| 47 | 55 | ▶ | exists $args{$_} ? ( $_ => $args{$_} ) : () | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } qw( text pretext color fields attachments ) | ||||||||||
| 49 | ), | ||||||||||
| 50 | # override if parameter specified | ||||||||||
| 51 | ( | ||||||||||
| 52 | map { | ||||||||||
| 53 | 11 | ▶ | ( $_ => exists $args{$_} ? $args{$_} : $self->{$_} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | } qw( channel icon_emoji icon_url username ) | ||||||||||
| 55 | ), | ||||||||||
| 56 | }; | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 3 | 1; | |||||||||
| 60 | __END__ | ||||||||||