lib/WWW/PushBullet.pm

filter: Statementstmt 44.4% 40 / 90 Branchbran 31.2% 5 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 55.5% 10 / 18 Podpod 100% 12 / 12 totaltotal 48.9% 68 / 139 CC 11 SCAR 20.3
CC
11
Cov
43%
_pushes
30.0
contacts
6.0
devices
6.0
CRAP 7.6
↑ Summary blib/lib/WWW/PushBullet.pm »
1  
2 =head1 NAME
3  
4 WWW::PushBullet - Module giving easy access to PushBullet API
5  
6 =head1 DESCRIPTION
7  
8 Module giving easy access to PushBullet API
9  
10 =head1 SYNOPSIS
11  
12 use WWW::PushBullet;
13
14 my $pb = WWW::PushBullet->new({apikey => $apikey});
15
16 $pb->push_address({ device_id => $device_id, name => $name,
17 address => $address });
18
19 $pb->push_file({ device_id => $device_id, file => $filename);
20
21 $pb->push_link({ device_id => $device_id, title => $title,
22 url => $url });
23
24 $pb->push_list({ device_id => $device_id, title => $title,
25 items => \@items });
26
27 $pb->push_note({ device_id => $device_id, title => $title,
28 body => $body });
29  
30 =cut
31  
32 package WWW::PushBullet;
33  
34 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 our $VERSION = '1.2.0';
42  
43 1 my %PUSHBULLET = (
44 REALM => 'Pushbullet',
45 SERVER => 'api.pushbullet.com:443',
46 URL_APIV2 => 'https://api.pushbullet.com/v2',
47 );
48  
49 1 $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 ▶ my ($class, $params) = @_;
Subroutine new: called CC=4 SCAR=14.0
Pod documented
64  
65 2 ▶ return (undef) if (!defined $params->{apikey});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"apikey"} 1 1 2
66 1 my $ua = LWP::UserAgent->new;
67 1 $ua->agent("WWW::PushBullet/$VERSION");
68 1 ▶ $ua->proxy('https', $params->{proxy}) if (defined $params->{proxy});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"proxy"} 0 1 1
69 $ua->credentials($PUSHBULLET{SERVER}, $PUSHBULLET{REALM},
70 1 $params->{apikey}, '');
71  
72 my $self = {
73 _ua => $ua,
74 _apikey => $params->{apikey},
75 1 ▶ _debug => $params->{debug} || 0,
Condition50%1/2
$params->{'debug'} || 0
l!l
01
MC/DC0%0/1
$params->{'debug'} || 0
$params->{'debug'}
Truth table50%1/2
A
A= $params->{'debug'}
Aresult
11
00
76 };
77  
78 1 bless $self, $class;
79  
80 1 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 ▶ my ($self, $line) = @_;
Subroutine DEBUG: called CC=2 SCAR=6.9
Pod documented
92  
93 2 ▶ if ($self->{_debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 1 1 2
94 {
95 1 my $str = sprintf '[DEBUG] %s', $line;
96 1 printf "$str\n";
97  
98 1 return ($str);
99 }
100  
101 1 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 ▶ my $self = shift;
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
115  
116 1 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 ▶ my ($self, $mode) = @_;
Subroutine debug_mode: called CC=1 SCAR=0.0
Pod documented
130  
131 2 $self->{_debug} = $mode;
132  
133 2 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 "Device '%s' => id %s\n", $d->{extras}->{model}, $d->{id};
145 }
146  
147 =cut
148  
149 sub contacts
150 {
151 0 ▶ my $self = shift;
Subroutine contacts: not called CC=2 SCAR=17.9
Pod documented
152  
153 0 my $res = $self->{_ua}->get("$PUSHBULLET{URL_APIV2}/contacts");
154  
155 0 ▶ if ($res->is_success)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
156 {
157 0 my $data = JSON->new->decode($res->content);
158 0 return ($data->{contacts});
159 }
160 else
161 {
162 0 print $res->status_line, "\n";
163 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 ▶ my $self = shift;
Subroutine devices: not called CC=2 SCAR=17.9
Pod documented
184  
185 0 my $res = $self->{_ua}->get("$PUSHBULLET{URL_APIV2}/devices");
186  
187 0 ▶ if ($res->is_success)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
188 {
189 0 my $data = JSON->new->decode($res->content);
190 0 return ($data->{devices});
191 }
192 else
193 {
194 0 print $res->status_line, "\n";
195 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 ▶ my ($self, $content) = @_;
Subroutine _pushes: not called CC=5 SCAR=34.0
208  
209 0 my $type = undef;
210 0 foreach my $i (0 .. $#{$content})
211 {
212 0 ▶ $type = $content->[$i + 1] if ($content->[$i] eq 'type');
Branch: 1 branch
BranchTrueFalseTotal
if $content->[$i] eq "type" 0 0 0
213 }
214 my $res = $self->{_ua}->post(
215 0 ▶ "$PUSHBULLET{URL_APIV2}/pushes",
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'file' ? : 0 0 0
216 Content_Type => ($type eq 'file' ? 'form-data' : undef),
217 Content => $content
218 );
219  
220 0 ▶ if ($res->is_success)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
221 {
222 0 my $data = JSON->new->decode($res->content);
223 0 return ($data);
224 }
225 else
226 {
227 0 print $res->status_line, "\n";
228 0 return (undef);
229 }
230 }
231  
232 =head2 push_address($params)
233  
234 Pushes address (with name & address)
235  
236 $pb->push_address(
237 {
238 device_id => $device_id,
239 name => 'GooglePlex',
240 address => '1600 Amphitheatre Pkwy, Mountain View, CA 94043, Etats-Unis'
241 }
242 );
243  
244 =cut
245  
246 sub push_address
247 {
248 0 ▶ my ($self, $params) = @_;
Subroutine push_address: not called CC=1 SCAR=6.9
Pod documented
249  
250 my $content = [
251 type => 'address',
252 device_id => $params->{device_id},
253 name => $params->{name},
254 address => $params->{address},
255 0 ];
256 0 $self->DEBUG(sprintf('push_address: %s', dump($content)));
257 0 my $result = $self->_pushes($content);
258  
259 0 return ($result);
260 }
261  
262 =head2 push_file($params)
263  
264 Pushes file
265  
266 $pb->push_file({ device_id => $device_id, file => '/var/www/index.html' });
267  
268 =cut
269  
270 sub push_file
271 {
272 0 ▶ my ($self, $params) = @_;
Subroutine push_file: not called CC=1 SCAR=6.9
Pod documented
273  
274 my $content = [
275 type => 'file',
276 device_id => $params->{device_id},
277 0 file => [$params->{file}],
278 ];
279 0 $self->DEBUG(sprintf('push_file: %s', dump($content)));
280 0 my $result = $self->_pushes($content);
281  
282 0 return ($result);
283 }
284  
285 =head2 push_link($params)
286  
287 Pushes link (with title & url)
288  
289 $pb->push_link(
290 {
291 device_id => $device_id,
292 title => 'WWW::PushBullet Perl module on GitHub',
293 url => 'https://github.com/sebthebert/WWW-PushBullet'
294 }
295 );
296  
297 =cut
298  
299 sub push_link
300 {
301 0 ▶ my ($self, $params) = @_;
Subroutine push_link: not called CC=1 SCAR=6.9
Pod documented
302  
303 my $content = [
304 type => 'link',
305 device_id => $params->{device_id},
306 title => $params->{title},
307 url => $params->{url},
308 0 ];
309 0 $self->DEBUG(sprintf('push_link: %s', dump($content)));
310 0 my $result = $self->_pushes($content);
311  
312 0 return ($result);
313 }
314  
315 =head2 push_list($params)
316  
317 Pushes list (with title & items)
318  
319 $pb->push_list(
320 {
321 device_id => $device_id,
322 title => 'One list with 3 items',
323 items => [ 'One', 'Two', 'Three' ]
324 }
325 );
326  
327 =cut
328  
329 sub push_list
330 {
331 0 ▶ my ($self, $params) = @_;
Subroutine push_list: not called CC=1 SCAR=6.9
Pod documented
332  
333 my $content = [
334 type => 'list',
335 device_id => $params->{device_id},
336 title => $params->{title},
337 items => $params->{items},
338 0 ];
339 0 $self->DEBUG(sprintf('push_list: %s', dump($content)));
340 0 my $result = $self->_pushes($content);
341  
342 0 return ($result);
343 }
344  
345 =head2 push_note($params)
346  
347 Pushes note (with title & body)
348  
349 $pb->push_note(
350 {
351 device_id => $device_id,
352 title => 'Note Title',
353 body => 'Note Body'
354 }
355 );
356  
357 =cut
358  
359 sub push_note
360 {
361 0 ▶ my ($self, $params) = @_;
Subroutine push_note: not called CC=1 SCAR=6.9
Pod documented
362  
363 my $content = [
364 type => 'note',
365 device_id => $params->{device_id},
366 title => $params->{title},
367 body => $params->{body},
368 0 ];
369 0 $self->DEBUG(sprintf('push_note: %s', dump($content)));
370 0 my $result = $self->_pushes($content);
371  
372 0 return ($result);
373 }
374  
375 =head2 version()
376  
377 Returns WWW::PushBullet module version
378  
379 =cut
380  
381 sub version
382 {
383 1 ▶ return ($VERSION);
Subroutine version: called CC=1 SCAR=0.0
Pod documented
384 }
385  
386 1 1;
387  
388 =head1 LICENSE
389
390 This program is free software; you can redistribute it and/or modify it
391 under the same terms as Perl itself.
392
393 =head1 AUTHOR
394  
395 Sebastien Thebert <www-pushbullet@onetool.pm>
396  
397 =cut
↑ Summary blib/lib/WWW/PushBullet.pm »