File Coverage

blib/lib/WebService/Slack/WebApi.pm
Criterion Covered Total %
statement 30 30 100.0
branch 2 2 100.0
condition 3 3 100.0
subroutine 10 10 100.0
pod n/a
total 45 45 100.0


line stmt bran cond sub pod time code
1             package WebService::Slack::WebApi;
2 5     5   323535 use strict;
  5         54  
  5         148  
3 5     5   28 use warnings;
  5         9  
  5         122  
4 5     5   27 use utf8;
  5         9  
  5         27  
5              
6 5     5   2571 use Class::Load qw/ load_class /;
  5         106206  
  5         468  
7             use Class::Accessor::Lite::Lazy (
8 5         60     new => 1,
9                 rw => [qw/ team_domain token opt ua /],
10                 ro_lazy => [qw/ client api auth channels conversations chat dialog emoji files groups im oauth pins reactions rtm search stars team users dnd bots migration /],
11 5     5   2428 );
  5         14082  
12              
13 5     5   5110 use WebService::Slack::WebApi::Exception;
  5         16  
  5         162  
14 5     5   2593 use WebService::Slack::WebApi::Client;
  5         21  
  5         864  
15              
16             our $VERSION = '0.17';
17              
18             sub _build_client {
19 11     11   13206     my $self = shift;
20 11 100 100     41     if( $self->opt && $self->ua ) {
21 1         22         WebService::Slack::WebApi::Exception::IllegalParameters->throw(
22                         message => 'Illegal parameters. You cannot use both parameters \'opt\' and \'ua\' together',
23                     );
24                 }
25 10         109     return WebService::Slack::WebApi::Client->new(
26                     team_domain => $self->team_domain,
27                     token => $self->token,
28                     opt => $self->opt,
29                     useragent => $self->ua,
30                 );
31             }
32              
33             for my $class_name (qw/ api auth channels conversations chat dialog emoji files groups im oauth pins reactions rtm search stars team users dnd bots migration /) {
34                 my $method = sprintf '%s::_build_%s', __PACKAGE__, $class_name;
35                 my $class = sprintf '%s::%s', __PACKAGE__, ucfirst($class_name);
36              
37 5     5   37     no strict 'refs';
  5         9  
  5         495  
38                 *$method = sub {
39 42     42   75457         load_class $class;
40 42         1551         return $class->new(client => shift->client)
41                 };
42             }
43              
44             1;
45              
46             __END__
47             =pod
48            
49             =head1 NAME
50            
51             WebService::Slack::WebApi - a simple wrapper for Slack Web API
52            
53             =head1 SYNOPSIS
54            
55             use WebService::Slack::WebApi;
56            
57             # By default we use the HTTP client library Furl but you can also use any other
58             # Eg. LWP::UserAgent
59             my $slack = WebService::Slack::WebApi->new( ua => LWP::UserAgent->new() );
60            
61             # the token is required unless using $slack->oauth->access
62             my $slack = WebService::Slack::WebApi->new(token => 'access token');
63            
64             # getting channel's descriptions
65             my $channels = $slack->conversations->list;
66            
67             # posting message to specified channel and getting message description
68             my $posted_message = $slack->chat->post_message(
69             channel => 'channel id', # required
70             text => 'hoge', # required (not required if 'attachments' argument exists)
71             username => 'fuga', # optional
72             # other optional parameters...
73             );
74            
75             =head1 DESCRIPTION
76            
77             WebService::Slack::WebApi is a simple wrapper for Slack Web API (https://api.slack.com/web).
78            
79             =head1 Options
80            
81             You can set some options by giving C<opt> parameter to C<new> method.
82             All values of C<opt> are given to C<Furl#new>.
83            
84             WebService::Slack::WebApi->new(token => 'access token', opt => {});
85            
86             =head2 User Agent
87            
88             By default WebService::Slack::WebApi uses the L<Furl> HTTP client.
89             But if your software is already using some other client,
90             e.g. L<LWP::UserAgent> or L<HTTP::Tiny>,
91             you can also use that. Under the hood WebService::Slack::WebApi uses
92             the HTTP client wrapper L<HTTP::AnyUA>.
93            
94             Use parameter C<ua> to specify the user agent which you have already
95             created.
96            
97             my $ua = LWP::UserAgent->new( timeout => 10 );
98             my $slack = WebService::Slack::WebApi->new( ua => $ua );
99            
100             If you use both parameters C<ua> and C<opt>, WebService::Slack::WebApi
101             will throw an exception. This combination is illegal.
102            
103             =head2 Proxy
104            
105             C<opt> can contain C<env_proxy> as boolean value .
106             If C<env_proxy> is true then proxy settings are loaded from C<$ENV{HTTP_PROXY}> and C<$ENV{NO_PROXY}> by calling C<Furl#env_proxy> method.
107             See also https://metacpan.org/pod/Furl#furl-env_proxy.
108            
109             =head1 METHODS
110            
111             This module provides all methods declared in the API reference (https://api.slack.com/methods).
112            
113             =head2 Basis
114            
115             C<WebService::Slack::WebApi::Namespace::method_name> corresponds to C<namespace.methodName> in Slack Web API.
116             For example C<WebService::Slack::WebApi::Chat::post_message> corresponds to C<chat.postMessage>.
117             You describe as below to call C<Chat::post_message> method.
118            
119             my $result = $slack->chat->post_message;
120            
121             =head2 Return value
122            
123             All methods return HashRef.
124             When you want to know what is contained in HashRef, see the API reference.
125            
126             =head2 The token parameter
127            
128             The API reference shows C<chat.update> method require 4 parameters: C<token>, C<ts>, C<channel> and C<text>.
129             When using this module C<token> parameter is added implicitly except using C<oauth.access> method.
130             So you pass the other 3 parameters to C<Chat::update> method as shown below.
131            
132             my $result = $slack->chat->update(
133             ts => '1401383885.000061', # as Str
134             channel => 'channel id',
135             text => 'hoge',
136             );
137            
138             =head2 Optional parameters
139            
140             Some methods have optional parameters.
141             If a parameter is optional in the API reference, it is also optional in this module.
142            
143             =head2 Not primitive parameters
144            
145             These parameters are not primitive:
146            
147             =over
148            
149             =item C<files.upload.file>: string of path to local file
150            
151             =item C<files.upload.channels>: ArrayRef of channel id string
152            
153             =back
154            
155             =head1 SEE ALSO
156            
157             =over
158            
159             =item https://api.slack.com/web
160            
161             =item https://api.slack.com/methods
162            
163             =back
164            
165             =head1 AUTHOR
166            
167             Mihyaeru/mihyaeru21 E<lt>mihyaeru21@gmail.comE<gt>
168            
169             =head1 LICENSE
170            
171             Copyright (C) Mihyaeru/mihyaeru21
172            
173             Released under the MIT license.
174            
175             See C<LICENSE> file.
176            
177             =cut
178            
179