File Coverage

lib/WebService/Intercom.pm
Criterion Covered Total %
statement 330 843 39.1
branch 0 554 0.0
condition 0 209 0.0
subroutine 89 104 85.5
pod n/a
total 419 1710 24.5


line stmt bran cond sub pod time code
1 2     2   3758 use Moops -strict;
  2         117236  
  2         18  
2              
3             # ABSTRACT: interfaces with Intercom.io
4             # PODNAME: WebService::Intercom
5              
6             =pod
7              
8             =head1 NAME
9              
10             WebService::Intercom - interact with the Intercom.io API
11              
12             =head1 SYNOPSIS
13              
14             my $intercom = WebService::Intercom->new(app_id => '[APP ID]',
15             api_key => '[API KEY]');
16              
17             # Create a user
18             my $user = $intercom->user_create_or_update(
19             email => 'test@example.com',
20             name => 'test user');
21            
22             # Retrieve an existing user.
23             my $existing_user = $intercom->user_get(email => 'test2@example.com');
24              
25             # Add a tag to a user
26             $user->tag('test tag');
27             $intercom->tag_create_or_update(name => "test tag");
28             $intercom->tag_items(name => "test tag", users => [{ email => 'test@example.com'}]);
29             $user->untag('test tag');
30              
31             # Change the user's name
32             $user->name = 'new name';
33             $user->save();
34              
35             # Delete the user
36             $user->delete();
37             $intercom->user_delete(email => 'test@example.com');
38              
39             # Add a note
40             $user->add_note(body => "This is a test note");
41             $intercom->note_create(email => 'test@example.com',
42             body => "This is a test note");
43            
44             # Add an event
45             $user->add_event(event_name => 'test event');
46             $intercom->event_create(email => 'test@example.com',
47             event_name => 'test event',
48             metadata => {
49             "article" => {"url" => "https://example.org/",
50             "value" => "link text"},
51             });
52              
53             =head1 DESCRIPTION
54              
55             Provides a nice API for Intercom.io rather than making raw requests.
56              
57             =head1 IMPLEMENTATION PHILOSOPHY
58              
59             This module attempts to stick as close to the API as possible.
60              
61             Documentation for the v2 API:
62              
63             L<http://doc.intercom.io/api/>
64              
65             For examples see the test cases, most functionality is well exercised
66             via tests.
67              
68             =cut
69              
70              
71 2     2   158482 use WebService::Intercom::Exception;
  2         6  
  2         124  
72 2     2   1021 use WebService::Intercom::Types;
  2         6  
  2         26  
73 2     2   2259 use WebService::Intercom::User;
  2         7  
  2         100  
74 2     2   776 use WebService::Intercom::Tag;
  2         5  
  2         104  
75 2     2   1026 use WebService::Intercom::Note;
  2         100  
  2         96  
76 2     2   881 use WebService::Intercom::Message;
  2         4  
  2         115  
77 2     2   914 use WebService::Intercom::Admin;
  2         6  
  2         163  
78              
79 2     2   3349 class WebService::Intercom types WebService::Intercom::Types {
  2     2   96  
  2     2   14  
  2         3  
  2         125  
  2         9  
  2         4  
  2         16  
  2         580  
  2         4  
  2         11  
  2         109  
  2         3  
  2         161  
  2         9  
  2         2  
  2         151  
  2         55  
  2         9  
  2         3  
  2         18  
  2         11524  
  2         4  
  2         15  
  2         1209  
  2         4  
  2         17  
  2         227  
  2         3  
  2         17  
  2         137  
  2         4  
  2         14  
  2         462  
  2         2  
  2         16  
  2         1843  
  2         4  
  2         36  
  2         6734  
  2         5  
  2         93  
  2         10  
  2         2  
  2         112  
  2         8  
  2         6  
  2         91  
  2         7  
  2         3  
  2         280  
  2         22  
  2         7132  
80 2     2   2673 use LWP::UserAgent;
  2         90000  
  2         87  
81 2     2   19 use HTTP::Response;
  2         4  
  2         51  
82 2     2   1264 use HTTP::Request::Common qw(DELETE POST GET);
  2         4764  
  2         165  
83 2     2   1307 use JSON::XS;
  2         10041  
  2         133  
84 2     2   942 use MIME::Base64;
  2         1183  
  2         129  
85 2     2   12 use Kavorka qw( multi method );
  2         3  
  2         22  
86              
87              
88 2         34 has 'ua' => (is => 'ro', default => sub { return LWP::UserAgent->new(keep_alive => 10, agent => "WebService::Intercom/1.0") } );
  0         0  
89 2         1323 has 'app_id' => (is => 'ro');
90 2         614 has 'api_key' => (is => 'ro');
91              
92 2         519 has 'api_base' => (is => 'ro', isa => Str, default => 'https://api.intercom.io');
93              
94             =head1 FUNCTIONS
95              
96             =head2 user_get
97              
98             Retrieves an existing user.
99              
100             $intercom->user_get(Maybe[Str] :$user_id?,
101             Maybe[Str] :$email?,
102             Maybe[Str] :$id?);
103              
104             Only one of user_id, email or id are required to retrieve a user.
105              
106             Returns a L<WebService::Intercom::User>.
107              
108             =cut
109            
110            
111             method user_get(Maybe[Str] :$user_id?,
112             Maybe[Str] :$email?,
113 2 0 0 2   15361 Maybe[Str] :$id?) {
  2 0 0 2   3  
  2 0   2   205  
  2 0   2   9  
  2 0   2   4  
  2 0   0   489  
  2 0       10  
  2 0       4  
  2 0       202  
  2 0       9  
  2 0       2  
  2         253  
  2         11  
  2         2  
  2         508  
  2         776  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
114              
115 0 0 0       if (!(defined($user_id) xor defined($email) xor defined($id))) {
      0        
116 0           WebService::Intercom::Exception->throw({ message => "One and only one of user_id, email or id must be defined"});
117             }
118              
119 0           my $request;
120 0 0         if (defined($id)) {
    0          
    0          
121 0           $request = GET($self->api_base . '/users/' . $id);
122             } elsif (defined($user_id)) {
123 0           $request = GET($self->api_base . '/users?user_id=' . URI::Escape::uri_escape($user_id));
124             } elsif (defined($email)) {
125 0           $request = GET($self->api_base . '/users?email=' . URI::Escape::uri_escape($email));
126             }
127              
128 0           $self->_request($request);
129             }
130              
131             =head2 user_create_or_update
132              
133             Creates or updates a user.
134              
135             # When you have an existing WebService::Intercom::User
136             $intercom->user_create_or_update(WebService::Intercom::User $user);
137              
138             or
139              
140             $intercom->user_create_or_update(Maybe[Str] :$user_id?,
141             Maybe[Str] :$email?,
142             Maybe[Str] :$id?,
143             Maybe[Int] :$signed_up_at?,
144             Str :$name?,
145             Maybe[IPAddressType] :$last_seen_ip?,
146             CustomAttributesType :$custom_attributes?,
147             Maybe[Str] :$last_seen_user_agent?,
148             HashRef :$companies?,
149             Maybe[Int] :$last_request_at?,
150             Maybe[Bool] :$unsubscribed_from_emails?,
151             Maybe[Bool] :$update_last_request_at?,
152             Maybe[Bool] :$new_session?);
153            
154             Returns a L<WebService::Intercom::User> that represents the new or updated user.
155              
156             =cut
157              
158            
159            
160 2 0   2   5637 multi method user_create_or_update(WebService::Intercom::User $user) {
  2 0   2   4  
  2 0   0   429  
  2 0       9  
  2 0       3  
  2 0       222  
  2 0       532  
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
161 0           return $self->user_create_or_update(user_id => $user->user_id,
162             email => $user->email,
163             id => $user->id,
164             name => $user->name,
165             last_seen_ip => $user->last_seen_ip,
166             custom_attributes => $user->custom_attributes,
167             last_seen_user_agent => $user->user_agent_data,
168             last_request_at => $user->last_request_at,
169             unsubscribed_from_emails => $user->unsubscribed_from_emails,
170             );
171             }
172              
173             multi method user_create_or_update(Maybe[Str] :$user_id?,
174             Maybe[Str] :$email?,
175             Maybe[Str] :$id?,
176             Maybe[Int] :$signed_up_at?,
177             Maybe[Str] :$name?,
178             Maybe[IPAddressType] :$last_seen_ip?,
179             CustomAttributesType :$custom_attributes?,
180             Maybe[Str] :$last_seen_user_agent?,
181             HashRef :$companies?,
182             Maybe[Int] :$last_request_at?,
183             Maybe[Bool] :$unsubscribed_from_emails?,
184             Maybe[Bool] :$update_last_request_at?,
185 2 0 0 2   38571 Maybe[Bool] :$new_session?) {
  2 0 0 2   4  
  2 0   2   341  
  2 0   2   10  
  2 0   2   2  
  2 0   2   540  
  2 0   2   12  
  2 0   2   3  
  2 0   2   319  
  2 0   2   53  
  2 0   2   4  
  2 0   2   239  
  2 0   2   9  
  2 0   2   2  
  2 0   2   245  
  2 0   0   12  
  2 0       4  
  2 0       272  
  2 0       11  
  2 0       3  
  2 0       306  
  2 0       10  
  2 0       4  
  2 0       364  
  2 0       9  
  2 0       3  
  2 0       707  
  2 0       10  
  2 0       3  
  2 0       253  
  2 0       9  
  2 0       3  
  2 0       293  
  2 0       12  
  2 0       3  
  2 0       251  
  2 0       9  
  2 0       3  
  2 0       247  
  2 0       8  
  2 0       4  
  2 0       282  
  2 0       10  
  2 0       5  
  2 0       602  
  2 0       366  
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
186              
187 0           my $json_content = {
188             signed_up_at => $signed_up_at,
189             name => $name,
190             last_seen_ip => $last_seen_ip,
191             custom_attributes => $custom_attributes,
192             last_seen_user_agent => $last_seen_user_agent,
193             companies => $companies,
194             last_request_at => $last_request_at,
195             unsubscribed_from_emails => $unsubscribed_from_emails,
196             update_last_request_at => $update_last_request_at,
197             new_session => $new_session
198             };
199            
200 0 0         if (defined($user_id)) {
201 0           $json_content->{user_id} = $user_id;
202             }
203 0 0         if (defined($email)) {
204 0           $json_content->{email} = $email;
205             }
206 0 0         if (defined($id)) {
207 0           $json_content->{id} = $id;
208             }
209              
210 0           my $json = JSON::XS::encode_json($json_content);
211 0           my $request = POST($self->api_base . '/users',
212             'Content-Type' => 'application/json',
213             Content => $json
214             );
215              
216 0           $self->_request($request);
217             }
218              
219              
220             =head2 user_delete
221              
222             Deletes a user
223              
224             # When you have an existing WebService::Intercom::User
225             $intercom->user_delete(WebService::Intercom::User $user);
226              
227             or
228              
229             $intercom->user_delete(Maybe[Str] :$user_id?,
230             Maybe[Str] :$email?,
231             Maybe[Str] :$id?);
232              
233             Only one of user_id, email or id is required.
234            
235             Returns a L<WebService::Intercom::User> that represents the deleted user.
236              
237             =cut
238              
239            
240 2 0   2   5670 multi method user_delete(WebService::Intercom::User $user) {
  2 0   2   3  
  2 0   0   488  
  2 0       13  
  2 0       4  
  2 0       301  
  2 0       356  
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
241 0           return $self->user_delete(user_id => $user->user_id,
242             id => $user->id,
243             email => $user->email);
244             }
245            
246             multi method user_delete(Str :$user_id?,
247             Str :$id?,
248 2 0 0 2   9644 Str :$email?) {
  2 0 0 2   5  
  2 0   2   378  
  2 0   2   11  
  2 0   2   4  
  2 0   0   540  
  2 0       11  
  2 0       3  
  2 0       297  
  2 0       10  
  2 0       3  
  2 0       237  
  2 0       11  
  2 0       6  
  2 0       522  
  2 0       306  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
249 0           my $request;
250 0 0         if (defined($id)) {
    0          
    0          
251 0           $request = DELETE($self->api_base . '/users/' . $id);
252             } elsif (defined($user_id)) {
253 0           $request = DELETE($self->api_base . '/users?user_id=' . URI::Escape::uri_escape($user_id));
254             } elsif (defined($email)) {
255 0           $request = DELETE($self->api_base . '/users?email=' . URI::Escape::uri_escape($email));
256             }
257              
258 0           $self->_request($request);
259             }
260              
261             =head2 tag_create_or_update
262              
263             Creates or updates a tag.
264              
265             # When you have an existing WebService::Intercom::User
266             $intercom->tag_create_or_update(WebService::Intercom::Tag $tag);
267              
268             or
269              
270             $intercom->tag_create_or_update(Str :$name,
271             Maybe[Str] :$id?);
272              
273             Returns a L<WebService::Intercom::Tag> that represents the tag.
274              
275             =cut
276              
277 2 0   2   5381 multi method tag_create_or_update(WebService::Intercom::Tag $tag) {
  2 0   2   4  
  2 0   0   468  
  2 0       13  
  2 0       3  
  2 0       230  
  2 0       296  
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
278 0           return $self->tag_create_or_update(name => $tag->name,
279             id => $tag->id);
280             }
281            
282             multi method tag_create_or_update(Str :$name,
283 2 0 0 2   8476 Maybe[Str] :$id?) {
  2 0 0 2   5  
  2 0   2   327  
  2 0   2   9  
  2 0   0   2  
  2 0       467  
  2 0       11  
  2 0       3  
  2 0       457  
  2 0       10  
  2 0       3  
  2 0       410  
  2         283  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
284 0           my $json_content = {
285             name => $name,
286             };
287 0 0         if (defined($id)) {
288 0           $json_content->{id} = $id;
289             }
290            
291 0           my $request = POST($self->api_base . '/tags',
292             'Content-Type' => 'application/json',
293             Content => JSON::XS::encode_json($json_content)
294             );
295              
296 0           return $self->_request($request);
297             }
298              
299              
300             =head2 tag_items
301              
302             Applies or removes a tag to users or companies
303              
304             # When you have an existing WebService::Intercom::User
305             $intercom->tag_items(Str :$name,
306             ArrayRef[TagUserIdentifierType] :$users?,
307             ArrayRef[TagCompanyIdentifierType] :$companies?);
308              
309             =cut
310              
311            
312             method tag_items(Str :$name,
313             ArrayRef[TagUserIdentifierType] :$users?,
314 2 0 0 2   19322 ArrayRef[TagCompanyIdentifierType] :$companies?) {
  2 0 0 2   4  
  2 0 0 2   218  
  2 0 0 2   10  
  2 0 0 2   3  
  2 0 0 0   457  
  2 0 0     10  
  2 0 0     3  
  2 0 0     290  
  2 0 0     10  
  2 0 0     3  
  2 0 0     956  
  2 0 0     10  
  2 0 0     3  
  2 0 0     1157  
  2 0 0     325  
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
315            
316 0           my $json_content = {
317             name => $name,
318             };
319              
320 0 0 0       if (!(defined($users) xor defined($companies))) {
321 0           WebService::Intercom::Exception->throw({ message => "Either users or companies must be defined as a parameter, not both and not neither"});
322             }
323              
324 0 0         if (defined($users)) {
    0          
325 0           $json_content->{users} = $users;
326             } elsif (defined($companies)) {
327 0           $json_content->{companies} = $companies;
328             }
329            
330            
331 0           my $request = POST($self->api_base . '/tags',
332             'Content-Type' => 'application/json',
333             Content => JSON::XS::encode_json($json_content)
334             );
335              
336 0           return $self->_request($request);
337             }
338              
339              
340             =head2 tag_delete
341              
342             Deletes a tag
343              
344             # When you have an existing WebService::Intercom::User
345             $intercom->tag_delete(WebService::Intercom::Tag $tag);
346              
347             or
348              
349             $intercom->tag_delete(Str :$id);
350              
351             Returns undef
352              
353             =cut
354              
355            
356 2 0 0 2   5708 multi method tag_delete(Str :$id) {
  2 0 0 2   4  
  2 0   2   371  
  2 0   0   10  
  2 0       4  
  2 0       438  
  2 0       8  
  2 0       4  
  2         230  
  2         247  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
357 0           $self->_request(DELETE($self->api_base . '/tags/' . $id), no_content => 1);
358             }
359              
360 2 0   2   5438 multi method tag_delete(WebService::Intercom::Tag $tag) {
  2 0   2   4  
  2 0   0   439  
  2 0       10  
  2 0       3  
  2 0       272  
  2 0       297  
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
361 0           return $self->tag_delete(id => $tag->id);
362             }
363              
364              
365             =head2 note_create
366              
367             Creates a note for a user
368              
369             # When you have an existing WebService::Intercom::User
370             $intercom->note_create(Maybe[Str] :$user_id?,
371             Maybe[Str] :$email?,
372             Maybe[Str] :$id?,
373             Maybe[Str] :$admin_id?,
374             Str :$body);
375              
376             Returns a L<WebService::Intercom::Note> that represents the note.
377              
378             =cut
379              
380            
381              
382             method note_create(Maybe[Str] :$user_id?,
383             Maybe[Str] :$email?,
384             Maybe[Str] :$id?,
385             Maybe[Str] :$admin_id?,
386 2 0 0 2   16334 Str :$body) {
  2 0 0 2   4  
  2 0   2   190  
  2 0   2   10  
  2 0   2   4  
  2 0   2   503  
  2 0   2   9  
  2 0   0   13  
  2 0       215  
  2 0       9  
  2 0       2  
  2 0       308  
  2 0       54  
  2 0       3  
  2 0       210  
  2 0       10  
  2 0       7  
  2         198  
  2         9  
  2         3  
  2         620  
  2         293  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
387 0 0 0       if (!(defined($user_id) xor defined($email) xor defined($id))) {
      0        
388 0           WebService::Intercom::Exception->throw({ message => "One and only one of user_id, email or id must be defined"});
389             }
390              
391 0           my $json_content = {
392             body => $body,
393             };
394              
395 0 0         if (defined($user_id)) {
    0          
    0          
396 0           $json_content->{user}->{user_id} = $user_id;
397             } elsif (defined($email)) {
398 0           $json_content->{user}->{email} = $email;
399             } elsif (defined($id)) {
400 0           $json_content->{user}->{id} = $id;
401             }
402              
403 0           my $request = POST($self->api_base . '/notes',
404             'Content-Type' => 'application/json',
405             Content => JSON::XS::encode_json($json_content)
406             );
407              
408              
409 0           return $self->_request($request);
410             }
411              
412             =head2 event_create
413              
414             Creates an event for a user
415              
416             # When you have an existing WebService::Intercom::User
417             $intercom->event_create(Maybe[Str] :$user_id?,
418             Maybe[Str] :$email?,
419             EventNameType :$event_name,
420             Maybe[Int] :$created_at?,
421             Maybe[EventMetadataType] :$metadata?);
422              
423             Returns undef.
424              
425             =cut
426            
427             method event_create(Maybe[Str] :$user_id?,
428             Maybe[Str] :$email?,
429             EventNameType :$event_name,
430             Maybe[Int] :$created_at?,
431 2 0 0 2   19697 Maybe[EventMetadataType] :$metadata? where { !defined($_) || scalar(keys %$_) <= 5 }) {
  2 0 0 2   4  
  2 0 0 2   243  
  2 0 0 2   9  
  2 0 0 2   3  
  2 0 0 2   519  
  2 0 0 2   11  
  2 0 0 0   4  
  2 0 0     262  
  2 0 0     15  
  2 0 0     3  
  2 0 0     312  
  2 0 0     11  
  2 0       3  
  2 0       278  
  2 0       9  
  2 0       3  
  2 0       257  
  2 0       12  
  2 0       2  
  2 0       1989  
  2 0       307  
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
432              
433 0 0 0       if (!(defined($user_id) || defined($email))) {
434 0           WebService::Intercom::Exception->throw({ message => "One of user_id or email must be defined"});
435             }
436              
437 0           my $json_content = {
438             event_name => $event_name,
439             created_at => $created_at,
440             };
441              
442 0 0         if (defined($user_id)) {
    0          
443 0           $json_content->{user_id} = $user_id;
444             } elsif (defined($email)) {
445 0           $json_content->{email} = $email;
446             }
447              
448 0 0         if (defined($metadata)) {
449 0           $json_content->{metadata} = $metadata;
450             }
451            
452              
453 0           my $request = POST($self->api_base . '/events',
454             'Content-Type' => 'application/json',
455             Content => JSON::XS::encode_json($json_content)
456             );
457              
458 0           return $self->_request($request, no_content => 1);
459             }
460              
461              
462             =head2 create_message
463              
464             Create a message, can be user or admin initiated.
465              
466             # When you have an existing WebService::Intercom::User
467             $intercom->create_message(MessagePersonType :$from,
468             Maybe[MessagePersonType] :$to,
469             Str :$body,
470             Maybe[Str] :$subject?,
471             Maybe[StrMatch[qr/^(plain|personal)$/]] :$template,
472             StrMatch[qr/^(inapp|email)$/] :$message_type);
473              
474             Returns a L<WebService::Intercom::Message>.
475              
476             =cut
477            
478             method create_message(MessagePersonType :$from,
479             Maybe[MessagePersonType] :$to,
480             Str :$body,
481             Maybe[Str] :$subject?,
482             Maybe[StrMatch[qr/^(plain|personal)$/]] :$template,
483 2 0 0 2   32274 StrMatch[qr/^(inapp|email)$/] :$message_type) {
  2 0 0 2   5  
  2 0 0 2   285  
  2 0 0 2   11  
  2 0 0 2   3  
  2 0 0 2   512  
  2 0 0 2   10  
  2 0 0 2   4  
  2 0 0 0   729  
  2 0 0     11  
  2 0 0     3  
  2 0 0     818  
  2 0 0     11  
  2 0 0     2  
  2 0 0     245  
  2 0 0     9  
  2 0 0     3  
  2 0 0     196  
  2 0       9  
  2 0       3  
  2 0       376  
  2 0       9  
  2 0       3  
  2 0       653  
  2 0       260  
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
484              
485 0 0 0       if (defined($message_type) && $message_type eq 'email') {
486 0 0         defined($subject) || WebService::Intercom::Exception->throw({ message => "Subject is required for email message"});
487             }
488            
489 0 0         my $json_content = {
    0          
    0          
    0          
490             from => $from,
491             (defined($to) ? (to => $to) : ()),
492             (defined($subject) ? (subject => $subject) : ()),
493             (defined($template) ? (template => $template) : ()),
494             (defined($message_type) ? (message_type => $message_type) : ()),
495             body => $body,
496             };
497              
498 0           my $request = POST($self->api_base . '/messages',
499             'Content-Type' => 'application/json',
500             Content => JSON::XS::encode_json($json_content)
501             );
502              
503 0           return $self->_request($request);
504             }
505              
506              
507 2 0   2   2300 method get_admins() {
  2 0   0   3  
  2         297  
  2         259  
  0            
  0            
  0            
508 0           my $request = GET($self->api_base . '/admins/',
509             'Content-Type' => 'application/json');
510            
511 0           return $self->_request($request);
512             }
513              
514              
515            
516 2 0 0 2   7458 method _request(HTTP::Request $request, Bool :$no_content?) {
  2 0 0 2   4  
  2 0   2   304  
  2 0   2   10  
  2 0   0   4  
  2 0       145  
  2 0       9  
  2 0       2  
  2 0       400  
  2         10  
  2         4  
  2         1821  
  2         216  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
517 0           $request->header('Accept', 'application/json');
518 0           $request->header('Authorization' => "Basic " . MIME::Base64::encode_base64($self->app_id . ":" . $self->api_key, ''));
519              
520 0           my $response = $self->ua->request($request);
521              
522 0 0         if ($response->is_success()) {
523 0 0         if (!$no_content) {
524 0           my $data;
525 0           eval {
526 0           $data = JSON::XS::decode_json($response->content());
527             };
528 0 0         if ($@) {
529 0           WebService::Intercom::Exception->throw({ message => "Failed to decode JSON result for request: " . $@ . "\n" . $request->as_string() . "\nResult was: " . $response->as_string()});
530             }
531 0 0         if ($data->{type} =~ /^(user|tag|note|user_message|admin_message)$/) {
    0          
    0          
532 0           my $class_name = "WebService::Intercom::" . ucfirst($1);
533 0           $class_name =~ s/(?:user_message|admin_message)$/Message/ig;
534 0           my $r;
535 0           eval {
536 0           $r = $class_name->new({
537             %$data,
538             intercom => $self,
539             });
540             };
541 0 0         if ($@) {
542 0           WebService::Intercom::Exception->throw({ message => "Failed to decode JSON result to object: " . $@ . "\n" . $request->as_string() . "\nResult was: " . $response->as_string()});
543             }
544 0           return $r;
545             } elsif ($data->{type} eq 'admin.list') {
546 0           my @admins = map { WebService::Intercom::Admin->new($_) } @{$data->{admins}};
  0            
  0            
547 0           return \@admins;
548             } elsif ($data->{type} eq 'error.list') {
549 0           WebService::Intercom::Exception->throw(
550             request_id => $data->{request_id},
551             message => $data->{errors}->[0]->{message},
552             code => $data->{errors}->[0]->{code}
553             );
554             } else {
555 0           WebService::Intercom::Exception->throw({ message => "Unknown object type returned: $data->{type}"});
556             }
557             }
558 0           return;
559             } else {
560             # Failed request but we still got some json content
561 0 0         if ($response->header('Content-Type') =~ /json/) {
562 0           my $data;
563 0           eval {
564 0           $data = JSON::XS::decode_json($response->content());
565             };
566 0 0         if ($@) {
567 0           WebService::Intercom::Exception->throw({ message => "Failed to decode JSON result for request " . $request->as_string() . "\nResult was: " . $response->as_string()});
568             }
569             WebService::Intercom::Exception->throw(
570 0           request_id => $data->{request_id},
571             message => $data->{errors}->[0]->{message},
572             code => $data->{errors}->[0]->{code}
573             );
574             }
575             }
576 0           WebService::Intercom::Exception->throw({ message => "Got a bad response from request:\n" . $request->as_string() . "\nResult was: " . $response->as_string()});
577             }
578             }
579              
580            
581             1;
582              
583             __END__
584              
585              
586             =head1 SEE ALSO
587              
588             See L<Moops> and L<Kavorka> to understand parameter signatures.
589              
590             Also of course Intercom at L<http://www.intercom.io>.
591              
592             =head1 AUTHOR
593              
594             Rusty Conover <rusty+cpan@luckydinosaur.com>
595              
596             =head1 COPYRIGHT
597              
598             This software is copyright (c) 2015 by Lucky Dinosaur LLC. L<http://www.luckydinosaur.com>
599              
600             This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
601              
602             =head1 DISCLAIMER OF WARRANTIES
603              
604             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
605              
606             =cut
607              
608            
609             1;