File Coverage

blib/lib/WebService/Slack/WebApi/Client.pm
Criterion Covered Total %
statement 47 49 95.9
branch 9 12 75.0
condition 2 2 100.0
subroutine 12 12 100.0
pod 0 2 0.0
total 70 77 90.9


line stmt bran cond sub pod time code
1             package WebService::Slack::WebApi::Client;
2 5     5   34 use strict;
  5         25  
  5         165  
3 5     5   28 use warnings;
  5         9  
  5         121  
4 5     5   24 use utf8;
  5         10  
  5         26  
5              
6 5     5   2931 use HTTP::AnyUA;
  5         109153  
  5         165  
7 5     5   2122 use JSON;
  5         30046  
  5         35  
8 5     5   615 use WebService::Slack::WebApi::Exception;
  5         12  
  5         195  
9              
10             use Class::Accessor::Lite::Lazy (
11 5         138     new => 1,
12                 rw => [qw/ team_domain token opt useragent /],
13                 ro_lazy => [qw/ ua /],
14 5     5   27 );
  5         12  
15              
16             sub _build_ua {
17 6     6   128     my $self = shift;
18 6         24     my $ua;
19             # Before introducing the parameter 'ua' to WebService::Slack::WebApi
20             # we used Furl. So let's keep ourselves backward compatible!
21 6 100       28     if( $self->useragent ) {
22 3         33         $ua = HTTP::AnyUA->new( ua => $self->useragent );
23                 } else {
24             # Attn. Using expression form of eval because otherwise
25             # the "use" would be executed before arriving to eval.
26 3 50   2   262         eval 'use Furl; 1;' or do {
  2     1   1109  
  2         46035  
  2         37  
  1         10  
  1         2  
  1         13  
27 0         0             my $msg = 'Illegal parameters. Unable to use package Furl.'
28                                 . ' If no \'ua\' is defined, we use Furl by default';
29 0         0             WebService::Slack::WebApi::Exception::IllegalParameters->throw(
30                             message => $msg,
31                         );
32                     };
33 3   100     11         my %opt = %{ $self->opt // +{} };
  3         16  
34 3         47         my $env_proxy = delete $opt{env_proxy};
35 3         14         my $furl = Furl->new(%opt);
36 3 100       128         $furl->env_proxy if $env_proxy;
37 3         40         $ua = HTTP::AnyUA->new( ua => $furl );
38                 }
39 6         334     return $ua;
40             }
41              
42             sub base_url {
43 108     108 0 662     my $self = shift;
44 108 50       237     my $team_domain = $self->team_domain ? $self->team_domain . '.' : '';
45 108         824     return sprintf 'https://%sslack.com/api', $team_domain;
46             }
47              
48             sub request {
49 108     108 0 776     my ($self, $path, $params) = @_;
50              
51                 my $response = $self->ua->post_form(
52                     $self->base_url . $path,
53                     [
54                         $self->token ? (token => $self->token) : (),
55 108 50       304             %{ $params },
  108         1337  
56                     ],
57                 );
58 108 100       34161     return decode_json $response->{content} if $response->{success};
59              
60 1         23     WebService::Slack::WebApi::Exception::FailureResponse->throw(
61                     message => 'request failed.',
62                     response => $response,
63                 );
64             }
65              
66             1;
67              
68