File Coverage

lib/WWW/PushBullet.pm
Criterion Covered Total %
statement 39 106 36.7
branch 5 18 27.7
condition 1 2 50.0
subroutine 11 20 55.0
pod 12 12 100.0
total 68 158 43.0


line stmt bran cond sub pod time code
1             package WWW::PushBullet;
2              
3             =head1 NAME
4              
5             WWW::PushBullet - Module giving easy access to PushBullet API
6              
7             =head1 DESCRIPTION
8              
9             Module giving easy access to PushBullet API
10              
11             =head1 SYNOPSIS
12              
13             use WWW::PushBullet;
14            
15             my $pb = WWW::PushBullet->new({apikey => $apikey});
16            
17             $pb->push_address({ device_iden => $device_iden, name => $name,
18             address => $address });
19            
20             $pb->push_file({ device_iden => $device_iden, file => $filename);
21            
22             $pb->push_link({ device_iden => $device_iden, title => $title,
23             url => $url });
24            
25             $pb->push_list({ device_iden => $device_iden, title => $title,
26             items => \@items });
27            
28             $pb->push_note({ device_iden => $device_iden, title => $title,
29             body => $body });
30              
31             =cut
32              
33 1     1   16711 use strict;
  1         2  
  1         27  
34 1     1   4 use warnings;
  1         1  
  1         24  
35              
36 1     1   424 use Data::Dump qw(dump);
  1         5796  
  1         68  
37 1     1   671 use JSON;
  1         9245  
  1         5  
38 1     1   731 use LWP::UserAgent;
  1         38067  
  1         31  
39 1     1   495 use MIME::Types;
  1         4238  
  1         948  
40              
41             our $VERSION = '1.4.0';
42              
43             my %PUSHBULLET = (
44             REALM => 'Pushbullet',
45             SERVER => 'api.pushbullet.com:443',
46             URL_APIV2 => 'https://api.pushbullet.com/v2',
47             );
48              
49             $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
50              
51             =head1 SUBROUTINES/METHODS
52              
53             =head2 new($params)
54              
55             Creates a new instance of PushBullet API
56              
57             my $pb = WWW::PushBullet->new({apikey => $apikey});
58              
59             =cut
60              
61             sub new
62             {
63 2     2 1 606 my ($class, $params) = @_;
64              
65 2 100       8 return (undef) if (!defined $params->{apikey});
66 1         10 my $ua = LWP::UserAgent->new;
67 1         2642 $ua->agent("WWW::PushBullet/$VERSION");
68 1 50       54 $ua->proxy('https', $params->{proxy}) if (defined $params->{proxy});
69 1         6 $ua->credentials($PUSHBULLET{SERVER}, $PUSHBULLET{REALM},
70             $params->{apikey}, '');
71              
72 1   50     21 my $self = {
73             _ua => $ua,
74             _apikey => $params->{apikey},
75             _debug => $params->{debug} || 0,
76             };
77              
78 1         4 bless $self, $class;
79              
80 1         3 return ($self);
81             }
82              
83             =head2 DEBUG
84              
85             Prints Debug message when '_debug' is enabled
86              
87             =cut
88              
89             sub DEBUG
90             {
91 2     2 1 9 my ($self, $line) = @_;
92              
93 2 100       7 if ($self->{_debug})
94             {
95 1         7 my $str = sprintf '[DEBUG] %s', $line;
96 1         161 printf "$str\n";
97              
98 1         7 return ($str);
99             }
100              
101 1         4 return (undef);
102             }
103              
104             =head2 api_key()
105              
106             Returns current PushBullet API key
107              
108             my $apikey = $pb->api_key();
109              
110             =cut
111              
112             sub api_key
113             {
114 1     1 1 372 my $self = shift;
115              
116 1         6 return ($self->{_apikey});
117             }
118              
119             =head2 debug_mode
120              
121             Sets Debug mode
122              
123             $pb->debug_mode(1);
124              
125             =cut
126              
127             sub debug_mode
128             {
129 2     2 1 696 my ($self, $mode) = @_;
130              
131 2         4 $self->{_debug} = $mode;
132              
133 2         5 return ($self->{_debug});
134             }
135              
136             =head2 contacts()
137              
138             Returns list of contacts
139              
140             my $contacts = $pb->contacts();
141            
142             foreach my $c (@{$contacts})
143             {
144             printf "Contact '%s' (%s) => %s\n", $c->{name}, $c->{iden}, $c->{email};
145             }
146              
147             =cut
148              
149             sub contacts
150             {
151 0     0 1 0 my $self = shift;
152              
153 0         0 my $res = $self->{_ua}->get("$PUSHBULLET{URL_APIV2}/contacts");
154              
155 0 0       0 if ($res->is_success)
156             {
157 0         0 my $data = JSON->new->decode($res->content);
158 0         0 return ($data->{contacts});
159             }
160             else
161             {
162 0         0 print $res->status_line, "\n";
163 0         0 return (undef);
164             }
165             }
166              
167             =head2 devices()
168            
169             Returns list of devices
170              
171             my $devices = $pb->devices();
172            
173             foreach my $d (@{$devices})
174             {
175             printf "Device '%s' (%s)=> id %s\n",
176             $d->{nickname}, $d->{model}, $d->{iden};
177             }
178              
179             =cut
180              
181             sub devices
182             {
183 0     0 1 0 my $self = shift;
184              
185 0         0 my $res = $self->{_ua}->get("$PUSHBULLET{URL_APIV2}/devices");
186              
187 0 0       0 if ($res->is_success)
188             {
189 0         0 my $data = JSON->new->decode($res->content);
190 0         0 return ($data->{devices});
191             }
192             else
193             {
194 0         0 print $res->status_line, "\n";
195 0         0 return (undef);
196             }
197             }
198              
199             =head2 _pushes($content)
200              
201             Generic pushes function (not supposed to be used directly)
202              
203             =cut
204              
205             sub _pushes
206             {
207 0     0   0 my ($self, $content) = @_;
208              
209 0         0 my $res = $self->{_ua}->post(
210             "$PUSHBULLET{URL_APIV2}/pushes",
211             Content_Type => 'application/json',
212             Content => JSON->new->encode($content)
213             );
214              
215 0 0       0 if ($res->is_success)
216             {
217 0         0 my $data = JSON->new->decode($res->content);
218 0         0 return ($data);
219             }
220             else
221             {
222 0         0 print $res->status_line, "\n";
223 0         0 return (undef);
224             }
225             }
226              
227             =head2 _upload_request($file_name, $file_type)
228              
229             Upload request to AWS (used by push_file)
230              
231             =cut
232              
233             sub _upload_request
234             {
235 0     0   0 my ($self, $file_name, $file_type) = @_;
236              
237 0         0 my $res = $self->{_ua}->post(
238             "$PUSHBULLET{URL_APIV2}/upload-request",
239             Content_Type => undef,
240             Content => ['file_name', $file_name, 'file_type', $file_type]
241             );
242              
243 0 0       0 if ($res->is_success)
244             {
245 0         0 my $data = JSON->new->decode($res->content);
246 0         0 my @array_data = %{$data->{data}};
  0         0  
247 0         0 push @array_data, 'file', [$file_name];
248 0         0 my $res = $self->{_ua}->post(
249             $data->{upload_url},
250             Content_Type => 'form-data',
251             Content => \@array_data
252             );
253 0 0       0 if ($res->is_success)
254             {
255 0         0 return ($data->{file_url});
256             }
257             else
258             {
259 0         0 print $res->status_line, "\n";
260 0         0 return (undef);
261             }
262             }
263             else
264             {
265 0         0 print $res->status_line, "\n";
266 0         0 return (undef);
267             }
268             }
269              
270             =head2 push_address($params)
271              
272             Pushes address (with name & address)
273              
274             $pb->push_address(
275             {
276             device_iden => $device_iden,
277             name => 'GooglePlex',
278             address => '1600 Amphitheatre Pkwy, Mountain View, CA 94043, Etats-Unis'
279             }
280             );
281              
282             =cut
283              
284             sub push_address
285             {
286 0     0 1 0 my ($self, $params) = @_;
287              
288 0         0 $params->{type} = 'address';
289 0         0 $self->DEBUG(sprintf('push_address: %s', dump($params)));
290 0         0 my $result = $self->_pushes($params);
291              
292 0         0 return ($result);
293             }
294              
295             =head2 push_file($params)
296              
297             Pushes file
298              
299             $pb->push_file(
300             {
301             device_iden => $device_iden,
302             file_name => '/var/www/index.html',
303             body => 'File Description'
304             }
305             );
306              
307             =cut
308              
309             sub push_file
310             {
311 0     0 1 0 my ($self, $params) = @_;
312              
313 0         0 my $mt = MIME::Types->new();
314 0         0 my $type = $mt->mimeTypeOf($params->{file_name});
315 0         0 $self->DEBUG(sprintf('push_file: %s', dump($params)));
316 0         0 my $file_url = $self->_upload_request($params->{file_name}, $type->type());
317 0 0       0 if (defined $file_url)
318             {
319 0         0 $params->{type} = 'file';
320 0         0 $params->{file_type} = $type->type();
321 0         0 $params->{file_url} = $file_url;
322 0         0 my $result = $self->_pushes($params);
323              
324 0         0 return ($result);
325             }
326              
327 0         0 return (undef);
328             }
329              
330             =head2 push_link($params)
331              
332             Pushes link (with title & url)
333              
334             $pb->push_link(
335             {
336             device_iden => $device_iden,
337             title => 'WWW::PushBullet Perl module on GitHub',
338             url => 'https://github.com/sebthebert/WWW-PushBullet'
339             }
340             );
341              
342             =cut
343              
344             sub push_link
345             {
346 0     0 1 0 my ($self, $params) = @_;
347              
348 0         0 $params->{type} = 'link';
349 0         0 $self->DEBUG(sprintf('push_link: %s', dump($params)));
350 0         0 my $result = $self->_pushes($params);
351              
352 0         0 return ($result);
353             }
354              
355             =head2 push_list($params)
356              
357             Pushes list (with title & items)
358              
359             $pb->push_list(
360             {
361             device_iden => $device_iden,
362             title => 'One list with 3 items',
363             items => [ 'One', 'Two', 'Three' ]
364             }
365             );
366              
367             =cut
368              
369             sub push_list
370             {
371 0     0 1 0 my ($self, $params) = @_;
372              
373 0         0 $params->{type} = 'list';
374              
375             #$params->{items} = join(',', @{$params->{items}});
376 0         0 $self->DEBUG(sprintf('push_list: %s', dump($params)));
377 0         0 my $result = $self->_pushes($params);
378              
379 0         0 return ($result);
380             }
381              
382             =head2 push_note($params)
383              
384             Pushes note (with title & body)
385              
386             $pb->push_note(
387             {
388             device_iden => $device_iden,
389             title => 'Note Title',
390             body => 'Note Body'
391             }
392             );
393              
394             =cut
395              
396             sub push_note
397             {
398 0     0 1 0 my ($self, $params) = @_;
399              
400 0         0 $params->{type} = 'note';
401 0         0 $self->DEBUG(sprintf('push_note: %s', dump($params)));
402 0         0 my $result = $self->_pushes($params);
403              
404 0         0 return ($result);
405             }
406              
407             =head2 version()
408              
409             Returns WWW::PushBullet module version
410              
411             =cut
412              
413             sub version
414             {
415 1     1 1 395 return ($VERSION);
416             }
417              
418             1;
419              
420             =head1 LICENSE
421            
422             This program is free software; you can redistribute it and/or modify it
423             under the same terms as Perl itself.
424              
425             =head1 REPOSITORY
426              
427             L
428              
429             =head1 AUTHOR
430              
431             Sebastien Thebert
432              
433             =cut