File Coverage

lib/WWW/PushBullet.pm
Criterion Covered Total %
statement 36 86 41.8
branch 5 16 31.2
condition 1 2 50.0
subroutine 10 18 55.5
pod 12 12 100.0
total 64 134 47.7


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