lib/WWW/PushBullet.pm

filter: Statementstmt 39.0% 43 / 110 Branchbran 27.7% 5 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 55.0% 11 / 20 Podpod 100% 12 / 12 totaltotal 44.1% 72 / 163 SCAR 37.0
CC
11
Cov
38%
_upload_request
12.0
contacts
6.0
devices
6.0
CRAP 40.3
« blib/lib/WWW/PushBullet.pm ↑ Summary
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 our $VERSION = '1.4.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 "Contact '%s' (%s) => %s\n", $c->{name}, $c->{iden}, $c->{email};
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=2 SCAR=17.9
208  
209 my $res = $self->{_ua}->post(
210 0 "$PUSHBULLET{URL_APIV2}/pushes",
211 Content_Type => 'application/json',
212 Content => JSON->new->encode($content)
213 );
214  
215 0 ▶ if ($res->is_success)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
216 {
217 0 my $data = JSON->new->decode($res->content);
218 0 return ($data);
219 }
220 else
221 {
222 0 print $res->status_line, "\n";
223 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 ▶ my ($self, $file_name, $file_type) = @_;
Subroutine _upload_request: not called CC=3 SCAR=24.8
236  
237 my $res = $self->{_ua}->post(
238 0 "$PUSHBULLET{URL_APIV2}/upload-request",
239 Content_Type => undef,
240 Content => ['file_name', $file_name, 'file_type', $file_type]
241 );
242  
243 0 ▶ if ($res->is_success)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
244 {
245 0 my $data = JSON->new->decode($res->content);
246 0 my @array_data = %{$data->{data}};
247 0 push @array_data, 'file', [$file_name];
248 my $res = $self->{_ua}->post(
249 $data->{upload_url},
250 0 Content_Type => 'form-data',
251 Content => \@array_data
252 );
253 0 ▶ if ($res->is_success)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
254 {
255 0 return ($data->{file_url});
256 }
257 else
258 {
259 0 print $res->status_line, "\n";
260 0 return (undef);
261 }
262 }
263 else
264 {
265 0 print $res->status_line, "\n";
266 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 ▶ my ($self, $params) = @_;
Subroutine push_address: not called CC=1 SCAR=6.9
Pod documented
287  
288 0 $params->{type} = 'address';
289 0 $self->DEBUG(sprintf('push_address: %s', dump($params)));
290 0 my $result = $self->_pushes($params);
291  
292 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 ▶ my ($self, $params) = @_;
Subroutine push_file: not called CC=2 SCAR=17.9
Pod documented
312  
313 0 my $mt = MIME::Types->new();
314 0 my $type = $mt->mimeTypeOf($params->{file_name});
315 0 $self->DEBUG(sprintf('push_file: %s', dump($params)));
316 0 my $file_url = $self->_upload_request($params->{file_name}, $type->type());
317 0 ▶ if (defined $file_url)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_url) 0 0 0
318 {
319 0 $params->{type} = 'file';
320 0 $params->{file_type} = $type->type();
321 0 $params->{file_url} = $file_url;
322 0 my $result = $self->_pushes($params);
323  
324 0 return ($result);
325 }
326  
327 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 ▶ my ($self, $params) = @_;
Subroutine push_link: not called CC=1 SCAR=6.9
Pod documented
347  
348 0 $params->{type} = 'link';
349 0 $self->DEBUG(sprintf('push_link: %s', dump($params)));
350 0 my $result = $self->_pushes($params);
351  
352 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 ▶ my ($self, $params) = @_;
Subroutine push_list: not called CC=1 SCAR=6.9
Pod documented
372  
373 0 $params->{type} = 'list';
374  
375 #$params->{items} = join(',', @{$params->{items}});
376 0 $self->DEBUG(sprintf('push_list: %s', dump($params)));
377 0 my $result = $self->_pushes($params);
378  
379 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 ▶ my ($self, $params) = @_;
Subroutine push_note: not called CC=1 SCAR=6.9
Pod documented
399  
400 0 $params->{type} = 'note';
401 0 $self->DEBUG(sprintf('push_note: %s', dump($params)));
402 0 my $result = $self->_pushes($params);
403  
404 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 ▶ return ($VERSION);
Subroutine version: called CC=1 SCAR=0.0
Pod documented
416 }
417  
418 1 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<https://github.com/sebthebert/WWW-PushBullet>
428  
429 =head1 AUTHOR
430  
431 Sebastien Thebert <www-pushbullet@onetool.pm>
432  
433 =cut
« blib/lib/WWW/PushBullet.pm ↑ Summary