Skip to content

Commit

Permalink
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
melo committed Oct 8, 2010
1 parent 31f3f0d commit 30a0b30
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/Protocol/Notifo.pm
@@ -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;
34 changes: 34 additions & 0 deletions t/01-basics.t
@@ -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();

0 comments on commit 30a0b30

Please sign in to comment.