Getter.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package Cpanel::iContact::Provider::Local::Getter;
  2. use strict;
  3. use warnings;
  4. # Specifically for the constant that is the dir
  5. use Cpanel::iContact::Provider::Local ();
  6. use Cpanel::AdminBin::Serializer ();
  7. use File::Slurper ();
  8. =encoding utf-8
  9. =head1 NAME
  10. Cpanel::iContact::Provider::Local::Getter - Get notifications stored by the "Store Locally" iContact module
  11. =head1 SYNOPSIS
  12. use lib '/var/cpanel/perl';
  13. use Cpanel::iContact::Provider::Local::Getter;
  14. # Get the latest notification
  15. my @notification_times = Cpanel::iContact::Provider::Local::Getter::get_notice_times( 'user' => 'root' );
  16. my $notification = Cpanel::iContact::Provider::Local::Getter::get_notice( $notification_times[0], 'user' => 'root');
  17. ...
  18. # OK, let's get ALL notifications AND their text
  19. my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => 'root' );
  20. =head1 DESCRIPTION
  21. Provide a way to retrieve locally saved iContact notices.
  22. =cut
  23. =head2 get_notice_times
  24. =over 2
  25. =item Input (hash)
  26. =over 3
  27. user: (required) specify the user to get these things for
  28. =back
  29. =item Output (array)
  30. =over 3
  31. UNIX Epoch timestamps sorted by latest date first.
  32. =back
  33. =back
  34. =cut
  35. # Note, since there's only one hash param (for now) just ref second item in array
  36. sub get_notice_times {
  37. return sort( map { substr( $_, rindex( $_, '/' ) + 1, -5 ) } grep { qr/\d+\.json/ } glob( $Cpanel::iContact::Provider::Local::DIR . "/$_[1]/*.json" ) );
  38. }
  39. =head2 get_notice
  40. =over 2
  41. =item Input (mixed)
  42. =over 3
  43. time: (integer/string) Time to look for, get em from get_notice_times above
  44. user: (hash param) specify the user to get these things for
  45. =back
  46. =item Output (string)
  47. =over 3
  48. Notification contents.
  49. =back
  50. =back
  51. =cut
  52. sub get_notice {
  53. my ( $time, %opts ) = @_;
  54. local $/;
  55. my $file = $Cpanel::iContact::Provider::Local::DIR . "/$opts{'user'}/$time.json";
  56. return Cpanel::AdminBin::Serializer::Load(File::Slurper::read_binary($file));
  57. }
  58. =head2 get_all_notices
  59. =over 2
  60. =item Input (hash)
  61. =over 3
  62. user: (required) specify the user to get these things for
  63. =back
  64. =item Output (hash)
  65. =over 3
  66. time => notification contents.
  67. =back
  68. =back
  69. =cut
  70. sub get_all_notices {
  71. my $user = $_[1];
  72. return map {
  73. my $time = substr( $_, rindex( $_, '/' ) + 1, -5 ); $time => get_notice( $time, 'user' => $user );
  74. } grep {
  75. qr/\d+\.json/
  76. } glob( $Cpanel::iContact::Provider::Local::DIR . "/$user/*.json" );
  77. }
  78. 1;