فهرست منبع

Add getter module for these things

Andy Baugh 7 سال پیش
والد
کامیت
4514bf6f85
1فایلهای تغییر یافته به همراه129 افزوده شده و 0 حذف شده
  1. 129 0
      lib/Cpanel/iContact/Provider/Local/Getter.pm

+ 129 - 0
lib/Cpanel/iContact/Provider/Local/Getter.pm

@@ -0,0 +1,129 @@
+package Cpanel::iContact::Provider::Local::Getter;
+
+use strict;
+use warnings;
+
+# Specifically for the constant that is the dir
+use Cpanel::iContact::Provider::Local;
+
+=encoding utf-8
+
+=head1 NAME
+
+Cpanel::iContact::Provider::Local::Getter - Get notifications stored by the "Store Locally" iContact module
+
+=head1 SYNOPSIS
+
+    use lib '/var/cpanel/perl';
+    use Cpanel::iContact::Provider::Local::Getter;
+
+    # Get the latest notification
+    my @notification_times = Cpanel::iContact::Provider::Local::Getter::get_notice_times( 'user' => 'root' );
+    my $notification = Cpanel::iContact::Provider::Local::Getter::get_notice( $notification_times[0], 'user' => 'root');
+    ...
+
+    # OK, let's get ALL notifications AND their text
+    my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => 'root' );
+
+=head1 DESCRIPTION
+
+Provide a way to retrieve locally saved iContact notices.
+
+=cut
+
+=head2 get_notice_times
+
+=over 2
+
+=item Input (hash)
+
+=over 3
+
+user: (required) specify the user to get these things for
+
+=back
+
+=item Output (array)
+
+=over 3
+
+UNIX Epoch timestamps sorted by latest date first.
+
+=back
+
+=back
+
+=cut
+
+# 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" ) );
+}
+
+=head2 get_notice
+
+=over 2
+
+=item Input (mixed)
+
+=over 3
+
+time: (integer/string) Time to look for, get em from get_notice_times above
+
+user: (hash param) specify the user to get these things for
+
+=back
+
+=item Output (string)
+
+=over 3
+
+Notification contents.
+
+=back
+
+=back
+
+=cut
+
+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>;
+}
+
+=head2 get_all_notices
+
+=over 2
+
+=item Input (hash)
+
+=over 3
+
+user: (required) specify the user to get these things for
+
+=back
+
+=item Output (hash)
+
+=over 3
+
+time => notification contents.
+
+=back
+
+=back
+
+=cut
+
+sub get_all_notices {
+	my $user = $_[1];
+	return map {
+		my $time = substr( $_, 0, -4 ); $time => get_notice( $time, 'user' => $user );
+	} grep {
+		/\d+\.txt/
+	} glob( $Cpanel::iContact::Provider::Local::DIR . "/$user/*.txt" );
+}
+
+1;