swaggboi

perl-object-halp

May 22nd, 2020
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.23 KB | None | 0 0
  1. # debugger says:
  2. #   DB<1> s
  3. #HTTP::Tiny::Handle::new(/usr/share/perl5/HTTP/Tiny.pm:891):
  4. #891:       return bless {
  5. #892:           rbuf             => '',
  6. #893:           timeout          => 60,
  7. #894:           max_line_size    => 16384,
  8. #895:           max_header_lines => 64,
  9. #896:           verify_SSL       => 0,
  10. #897:           SSL_options      => {},
  11. #898:           %args
  12. #  DB<1> q
  13.  
  14. # BEGIN my script:
  15. #!/usr/bin/env perl
  16.  
  17. use strict;
  18. use warnings;
  19. #use Mozilla::CA; # I expect the request to fail with this commented out
  20. use WebService::Discord::Webhook;
  21. use Data::Dumper; # Uncomment for debugging
  22.  
  23. # Webhook URL
  24. my $url = 'https://discordapp.com/api/webhooks/BIG/' .
  25.     'LONG-URL';
  26.  
  27. # Create webhook object
  28. my $webhook = WebService::Discord::Webhook->new(
  29.     url => $url,
  30.     verify_SSL => '1'
  31.     );
  32.  
  33. print Dumper($webhook->get());
  34.  
  35. # END my script
  36. # perl module contains this:
  37. sub new {
  38.   my $class = shift;
  39.  
  40.   my %params;
  41.   if ( scalar @_ > 1 ) {
  42.     %params = @_;
  43.   } else {
  44.     $params{url} = shift;
  45.   }
  46.  
  47.   # check parameters
  48.   my ( $id, $token );
  49.   if ( $params{url} ) {
  50.     if ( $params{url} =~ m{discordapp\.com/api/webhooks/(\d+)/([^/?]+)}i ) {
  51.       $id    = $1;
  52.       $token = $2;
  53.     } else {
  54.       croak "Failed to parse ID and Token from URL";
  55.     }
  56.   } elsif ( $params{id} && $params{token} ) {
  57.     if ( $params{id} =~ m/^\d+$/ && $params{token} =~ m{^[^/?]+$} ) {
  58.       $id    = $params{id};
  59.       $token = $params{token};
  60.     } else {
  61.       croak "Failed to validate ID and Token";
  62.     }
  63.   } else {
  64.     croak "Must provide either URL, or ID and Token";
  65.   }
  66.  
  67.   # Create an LWP UserAgent for REST requests
  68.   my %attributes =
  69.     ( agent =>
  70. "p5-WebService-Discord-Webhook (https://github.com/greg-kennedy/p5-WebService-Discord-Webhook, $VERSION)"
  71.     );
  72.   if ( $params{timeout} )    { $attributes{timeout}    = $params{timeout} }
  73.   if ( $params{verify_SSL} ) { $attributes{verify_SSL} = $params{verify_SSL} }
  74.  
  75.   my $http = HTTP::Tiny->new(%attributes);
  76.  
  77.   # create class with some params
  78.   my $self = bless { id => $id, token => $token, http => $http }, $class;
  79.   if ( $params{wait} ) { $self->{wait} = 1 }
  80.  
  81.   # call get to populate additional details
  82.   #$self->get();
  83.  
  84.   return $self;
  85. }
Add Comment
Please, Sign In to add comment