Skip to content

Commit

Permalink
Debug mode in WWW::PushBullet module
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthebert committed Jan 22, 2014
1 parent b9f306f commit a24a54c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can get your API key in your [PushBullet account settings](https://www.pushb

You also need this Perl modules:

* [Data::Dump](https://metacpan.org/pod/Data::Dump)
* [File::Slurp](https://metacpan.org/release/File-Slurp)
* [FindBin](https://metacpan.org/pod/FindBin)
* [Getopt::Long](https://metacpan.org/release/Getopt-Long)
Expand Down
77 changes: 42 additions & 35 deletions bin/pushbullet
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ use WWW::PushBullet;

my $FILE_CONFIG = "$FindBin::Bin/../conf/pushbullet.json";

my %opt = ();
my %opt = ();
my @opt_devices = ();
my @opt_items = ();
my @opt_items = ();

=head1 SUBROUTINES/METHODS
Expand All @@ -112,7 +112,10 @@ Prints Debug message when debug mode is enabled (with -D/--debug)
sub DEBUG
{
my $line = shift;
printf "[DEBUG] %s\n", $line if ($opt{debug});

printf "[DEBUG] %s\n", $line if ($opt{debug});

return ($line);
}

=head2 Read_File_Config($file_config)
Expand Down Expand Up @@ -140,20 +143,21 @@ sub Read_Config_File
# MAIN
#

my $status = GetOptions(\%opt,
'address|a=s', # for 'pushbullet address'
'body|b=s', # for 'pushbullet note'
'config|c=s', # to change configuration file
'device|d=s@', # to specify device
'file|f=s', # for 'pushbullet file'
'item|i=s@', # for 'pushbullet list'
'apikey|k=s', # to specify apikey
'name|n=s', # for 'pushbullet address'
'title|t=s', # for 'pushbullet note'
'url|u=s', # for 'pushbullet link, list & note'
'debug|D', # to set debug mode
'help|h', # to print help
'version|v', # to print version
my $status = GetOptions(
\%opt,
'address|a=s', # for 'pushbullet address'
'body|b=s', # for 'pushbullet note'
'config|c=s', # to change configuration file
'device|d=s@', # to specify device
'file|f=s', # for 'pushbullet file'
'item|i=s@', # for 'pushbullet list'
'apikey|k=s', # to specify apikey
'name|n=s', # for 'pushbullet address'
'title|t=s', # for 'pushbullet note'
'url|u=s', # for 'pushbullet link, list & note'
'debug|D', # to set debug mode
'help|h', # to print help
'version|v', # to print version
);
pod2usage(0) if ((!$status) || ($opt{help}));
if ($opt{version})
Expand All @@ -164,26 +168,34 @@ if ($opt{version})

$opt{action} = $ARGV[0];

pod2usage(0) if ((! defined $opt{action}) || ($opt{action} !~ /^(?:address|devices|file|link|list|note)$/));
pod2usage(0)
if ((!defined $opt{action})
|| ($opt{action} !~ /^(?:address|devices|file|link|list|note)$/));
my $conf = Read_Config_File($opt{config});

my $apikey = $opt{apikey} || $conf->{apikey};
my $address = $opt{address} || $conf->{default_address};
my $body = $opt{body} || $conf->{default_body};
my @devices = (defined $opt{device} && scalar @{$opt{device}} ? @{$opt{device}}
: (defined $conf->{default_device_id} ? @{$conf->{default_device_id}} : ()));
my $file = $opt{file} || $conf->{default_file};
my $items = (defined $opt{item} &&scalar @{$opt{item}} ? $opt{item}
: $conf->{default_items});
my $apikey = $opt{apikey} || $conf->{apikey};
my $address = $opt{address} || $conf->{default_address};
my $body = $opt{body} || $conf->{default_body};
my @devices = (
defined $opt{device} && scalar @{$opt{device}}
? @{$opt{device}}
: (defined $conf->{default_device_id} ? @{$conf->{default_device_id}} : ())
);
my $file = $opt{file} || $conf->{default_file};
my $items = (
defined $opt{item} && scalar @{$opt{item}}
? $opt{item}
: $conf->{default_items}
);
my $name = $opt{name} || $conf->{default_name};
my $title = $opt{title} || $conf->{default_title};
my $url = $opt{url} || $conf->{default_url};

my $pb = WWW::PushBullet->new({apikey => $apikey});
my $pb = WWW::PushBullet->new({apikey => $apikey, debug => $opt{debug}});

if ((! scalar @devices) && ($opt{action} ne 'devices'))
if ((!scalar @devices) && ($opt{action} ne 'devices'))
{
DEBUG("No device specified, get all devices from apikey account");
DEBUG('No device specified, get all devices from apikey account');
my $devs = $pb->devices();
foreach my $d (@{$devs})
{
Expand All @@ -197,7 +209,6 @@ if ($opt{action} eq 'address')
{
foreach my $device (@devices)
{
DEBUG("push_address");
$pb->push_address(
{
device_id => $device,
Expand Down Expand Up @@ -226,7 +237,6 @@ elsif ($opt{action} eq 'file')
{
foreach my $device (@devices)
{
DEBUG("push_file");
$pb->push_file({device_id => $device, file => $file});
}
}
Expand All @@ -240,8 +250,7 @@ elsif ($opt{action} eq 'link')
if ((defined $title) && (defined $url) && ($url =~ /^https?:/))
{
foreach my $device (@devices)
{
DEBUG("push_link");
{
$pb->push_link(
{
device_id => $device,
Expand All @@ -262,7 +271,6 @@ elsif ($opt{action} eq 'list')
{
foreach my $device (@devices)
{
DEBUG("push_list");
$pb->push_list(
{
device_id => $device,
Expand All @@ -284,7 +292,6 @@ elsif ($opt{action} eq 'note')
{
foreach my $device (@devices)
{
DEBUG("push_note");
$pb->push_note(
{
device_id => $device,
Expand Down
46 changes: 42 additions & 4 deletions lib/WWW/PushBullet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ package WWW::PushBullet;
use strict;
use warnings;

use Data::Dump qw(dump);
use JSON;
use LWP::UserAgent;
use LWP::Protocol::https;

our $VERSION = '0.8.2';
our $VERSION = '0.9';

my %PUSHBULLET = (
REALM => 'Pushbullet',
Expand All @@ -47,7 +48,7 @@ my %PUSHBULLET = (
);

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

=head1 SUBROUTINES/METHODS
=head2 new($params)
Expand All @@ -62,7 +63,7 @@ sub new
{
my ($class, $params) = @_;

return (undef) if (!defined $params->{apikey});
return (undef) if (!defined $params->{apikey});
my $ua = LWP::UserAgent->new;
$ua->agent("WWW::PushBullet/$VERSION");
$ua->credentials($PUSHBULLET{SERVER}, $PUSHBULLET{REALM}, $params->{apikey},
Expand All @@ -71,13 +72,29 @@ sub new
my $self = {
_ua => $ua,
_apikey => $params->{apikey},
_debug => $params->{debug} || 0,
};

bless $self, $class;

return ($self);
}

=head2 DEBUG
Prints Debug message when '_debug' is enabled
=cut

sub DEBUG
{
my ($self, $line) = @_;

printf "[DEBUG] %s\n", $line if ($self->{_debug});

return ($line);
}

=head2 api_key()
Returns current PushBullet API key
Expand All @@ -93,6 +110,23 @@ sub api_key
return ($self->{_apikey});
}

=head2 debug_mode
Sets Debug mode
$pb->debug_mode(1);
=cut

sub debug_mode
{
my ($self, $mode) = shift;

$self->{_debug} = $mode;

return ($self->{_debug});
}

=head2 devices()
Returns list of devices
Expand Down Expand Up @@ -181,6 +215,7 @@ sub push_address
name => $params->{name},
address => $params->{address},
];
$self->DEBUG(sprintf('push_address: %s', dump($content)));
my $result = $self->_pushes($content);

return ($result);
Expand All @@ -203,6 +238,7 @@ sub push_file
device_id => $params->{device_id},
file => [$params->{file}],
];
$self->DEBUG(sprintf('push_file: %s', dump($content)));
my $result = $self->_pushes($content);

return ($result);
Expand Down Expand Up @@ -232,7 +268,7 @@ sub push_link
title => $params->{title},
url => $params->{url},
];

$self->DEBUG(sprintf('push_link: %s', dump($content)));
my $result = $self->_pushes($content);

return ($result);
Expand Down Expand Up @@ -262,6 +298,7 @@ sub push_list
title => $params->{title},
items => $params->{items},
];
$self->DEBUG(sprintf('push_list: %s', dump($content)));
my $result = $self->_pushes($content);

return ($result);
Expand Down Expand Up @@ -291,6 +328,7 @@ sub push_note
title => $params->{title},
body => $params->{body},
];
$self->DEBUG(sprintf('push_note: %s', dump($content)));
my $result = $self->_pushes($content);

return ($result);
Expand Down

0 comments on commit a24a54c

Please sign in to comment.