Selaa lähdekoodia

Change to using JSON, add getter test

Andy Baugh 7 vuotta sitten
vanhempi
sitoutus
4ceed9ce8f

+ 4 - 0
lib/Cpanel/AdminBin/Serializer.pm

@@ -6,4 +6,8 @@ sub Dump {
     return Cpanel::JSON::XS->new()->allow_blessed()->encode(@_);
 }
 
+sub Load {
+    return Cpanel::JSON::XS->new()->allow_blessed()->decode(@_);
+}
+
 1;

+ 4 - 3
lib/Cpanel/iContact/Provider/Local.pm

@@ -68,10 +68,10 @@ sub send {
     # my $html    = ${ $args_hr->{'html_body'} };
 
     # Send it
-    my $time = localtime;
+    my $time = time;
     $time =~ tr/ /-/;
     my $user = getpwuid($<);
-    my $file = "$DIR/$user/$time.txt";
+    my $file = "$DIR/$user/$time.json";
     try {
         # Make the dir if it doesn't exist
         if( !-d "$DIR/$user" ) {
@@ -84,8 +84,9 @@ sub send {
                 };
             }
         }
+	    require Cpanel::AdminBin::Serializer;
         open( my $fh, ">", $file ) || die "Couldn't open '$file': $!";
-        print $fh "$subject\n\n$text\n";
+        print $fh Cpanel::AdminBin::Serializer::Dump( { 'subject' => $subject, 'text' => $text } );
         close $fh;
     }
     catch {

+ 9 - 7
lib/Cpanel/iContact/Provider/Local/Getter.pm

@@ -4,7 +4,8 @@ use strict;
 use warnings;
 
 # Specifically for the constant that is the dir
-use Cpanel::iContact::Provider::Local;
+use Cpanel::iContact::Provider::Local ();
+use Cpanel::AdminBin::Serializer      ();
 
 =encoding utf-8
 
@@ -57,7 +58,7 @@ UNIX Epoch timestamps sorted by latest date first.
 
 # Note, since there's only one hash param (for now) just ref second item in array
 sub get_notice_times {
-    return sort( map { substr( $_, 0, -4 ) } grep { /\d+\.txt/ } glob( $Cpanel::iContact::Provider::Local::DIR . "/$_[1]/*.txt" ) );
+    return sort( map { substr( $_, rindex( $_, '/' ), -5 ) } grep { qr/\d+\.json/ } glob( $Cpanel::iContact::Provider::Local::DIR . "/$_[1]/*.json" ) );
 }
 
 =head2 get_notice
@@ -89,8 +90,9 @@ Notification contents.
 sub get_notice {
 	my ( $time, %opts ) = @_;
 	local $/;
-	open my $fh, '<', $Cpanel::iContact::Provider::Local::DIR . "/$opts{'user'}/$time.txt" or die "can't open $file: $!";
-    return <$fh>;
+	my $file = $Cpanel::iContact::Provider::Local::DIR . "/$opts{'user'}/$time.json";
+	open my $fh, '<', $file or die "can't open $file: $!";
+    return Cpanel::AdminBin::Serializer::Load(<$fh>);
 }
 
 =head2 get_all_notices
@@ -120,10 +122,10 @@ time => notification contents.
 sub get_all_notices {
 	my $user = $_[1];
 	return map {
-		my $time = substr( $_, 0, -4 ); $time => get_notice( $time, 'user' => $user );
+		my $time = substr( $_, rindex( $_, '/' ), -5 ); $time => get_notice( $time, 'user' => $user );
 	} grep {
-		/\d+\.txt/
-	} glob( $Cpanel::iContact::Provider::Local::DIR . "/$user/*.txt" );
+		qr/\d+\.json/
+	} glob( $Cpanel::iContact::Provider::Local::DIR . "/$user/*.json" );
 }
 
 1;

+ 9 - 6
t/Cpanel-iContact-Provider-Local.t

@@ -9,13 +9,14 @@ use Test::More;
 use Test::Fatal;
 use File::Temp ();
 
-use Cpanel::iContact::Provider::Local ();
+use Cpanel::iContact::Provider::Local         ();
+use Cpanel::iContact::Provider::Local::Getter ();
 
 plan tests => 1;
 
 # First, let's mock out the parent, and other stuff we wouldn't wanna do in a unit test
 subtest "Provider bits work as expected ('unit' test)" => sub {
-
+    plan tests => 6;
     # Create tempdir for jamming stuff into
     my $tmp_obj = File::Temp->newdir();
     my $tmp_dir = $tmp_obj->dirname;
@@ -26,10 +27,12 @@ subtest "Provider bits work as expected ('unit' test)" => sub {
     my $ex = exception { $spammer->send() };
     is( $ex, undef, "send doesn't throw on GreatSuccess" ) || diag explain $ex;
     my $user = getpwuid($<);
-    my @files = glob( "$tmp_dir/iContact_notices/$user/*.txt" );
+    my @files = glob( "$tmp_dir/iContact_notices/$user/*.json" );
     ok( scalar(@files), "Looks like a file was written..." ) || diag explain \@files;
+    like( $files[0], qr/\d\.json/, "..and it looks like we'd expect it to" ) || diag explain \@files;
 
-    # Thu-Dec-20-13:46:46-2018 
-    like( $files[0], qr/[A-Z][a-z]{2}-[A-Z][a-z]{2}-\d{2}-\d{2}:\d{2}:\d{2}-\d{4}\.txt/, "..and it looks like we'd expect it to" ) || diag explain \@files;
-    #diag `cat $files[0]`;
+    # Now let's check on them another way
+    my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => $user );
+    ok( scalar(keys(%notifications)), "Got the expected notifications added to dir..." ) || diag explain \%notifications;
+    like( (keys(%notifications))[0], qr/\d+/, "..and it looks like we'd expect it to" ) || diag explain \%notifications;
 };