Snippets

Brian Medley Simple playback and record with Mojo::Useragent

Created by Brian Medley
#!/opt/perl

package Record::UserAgent;

use Mojo::Base qw(Mojo::UserAgent);

use Mojo::Util qw(steady_time spurt);

sub _read {
    spurt($_[2], sprintf("_read.%s", steady_time));

    shift->SUPER::_read(@_);
}

package main;

use Mojo::IOLoop;
use Mojo::UserAgent;

my $ua;

if (@ARGV) {
    # nc -l 127.0.0.1 4000 < _read.1442857237.29646
    
    $ua = Mojo::UserAgent->new;
    $ua->get("http://127.0.0.1:4000" => sub {
      my ($ua, $tx) = @_;

      say $tx->res->body;

      Mojo::IOLoop->stop;
    });
}
else {
    $ua = Record::UserAgent->new;
    $ua->get('http://cnn.com' => sub {
      my ($ua, $tx) = @_;

      $DB::single = 1;

      say $tx->res->body;

      Mojo::IOLoop->stop;
    });
}

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.