Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added constructor to Protocol::Notifo, including authz header generation
Signed-off-by: Pedro Melo <melo@simplicidade.org>
- Loading branch information
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,28 @@ | ||
package Protocol::Notifo; | ||
|
||
# ABSTRACT: utilities to build the requests for the notifo.com service | ||
|
||
use strict; | ||
use warnings; | ||
package Protocol::Notifo; | ||
use Carp 'confess'; | ||
use JSON 'decode_json'; | ||
use MIME::Base64 'encode_base64'; | ||
use namespace::clean; | ||
|
||
sub new { | ||
my ($class, %args) = @_; | ||
my $self = bless {}, $class; | ||
|
||
for my $f (qw( user api_key )) { | ||
confess("Missing required parameter '$f' to new(), ") unless $args{$f}; | ||
$self->{$f} = $args{$f}; | ||
} | ||
|
||
$self->{base_url} = 'https://api.notifo.com/v1'; | ||
$self->{auth_hdr} = encode_base64(join(':', @$self{qw(user api_key)})); | ||
|
||
return $self; | ||
} | ||
|
||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!perl | ||
|
||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use Test::Exception; | ||
use Protocol::Notifo; | ||
use MIME::Base64 'decode_base64'; | ||
|
||
my $n; | ||
lives_ok | ||
sub { $n = Protocol::Notifo->new(user => 'me', api_key => 'my_key') }, | ||
'new() survived with a prodigal object'; | ||
ok(defined($n), '... which, by the way, looks defined'); | ||
isa_ok($n, 'Protocol::Notifo', '... and even of the proper type'); | ||
|
||
is($n->{user}, 'me', '... good user in there'); | ||
is($n->{api_key}, 'my_key', '... and a nice little API key'); | ||
|
||
is($n->{base_url}, 'https://api.notifo.com/v1', | ||
'The API endpoint is alright'); | ||
is(decode_base64($n->{auth_hdr}), | ||
'me:my_key', '... and the authorization header is perfect'); | ||
|
||
|
||
### Bad boys | ||
throws_ok sub { Protocol::Notifo->new }, | ||
qr/Missing required parameter 'user' to new[(][)], /, | ||
'new() with missing user, expected exception'; | ||
throws_ok sub { Protocol::Notifo->new(user => 'me') }, | ||
qr/Missing required parameter 'api_key' to new[(][)], /, | ||
'new() with missing api_key, expected exception'; | ||
|
||
done_testing(); |