aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandy Lauen <randy.lauen@gmail.com>2019-08-28 21:34:12 -0500
committerRandy Lauen <randy.lauen@gmail.com>2019-08-28 21:34:12 -0500
commitc8826b12ae4835e1673a5783d519912aedf4216c (patch)
treef845a9c05de26a5d6a8dac66859ee18560b9105b
parent338524a725797fb46984f5d98db6df8556dd4f17 (diff)
downloadperlweeklychallenge-club-c8826b12ae4835e1673a5783d519912aedf4216c.tar.gz
perlweeklychallenge-club-c8826b12ae4835e1673a5783d519912aedf4216c.tar.bz2
perlweeklychallenge-club-c8826b12ae4835e1673a5783d519912aedf4216c.zip
task 3 solutions
-rw-r--r--challenge-023/randy-lauen/perl5/ch-3.pl44
-rw-r--r--challenge-023/randy-lauen/perl6/ch-3.p631
2 files changed, 75 insertions, 0 deletions
diff --git a/challenge-023/randy-lauen/perl5/ch-3.pl b/challenge-023/randy-lauen/perl5/ch-3.pl
new file mode 100644
index 0000000000..d4fd4c6973
--- /dev/null
+++ b/challenge-023/randy-lauen/perl5/ch-3.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+=head2 SYNOPSIS
+
+Task:
+Write a script to use Random Poems API.
+
+Usage:
+ $ perl ch-3.pl
+
+Notes:
+This script prints the shortest poem returned by the randompoems endpoint.
+The Poemist API docs are located here: https://poemist.github.io/poemist-apidoc/
+
+=cut
+
+use v5.28;
+use strict;
+use warnings;
+
+use JSON qw( decode_json );
+use List::UtilsBy qw( nsort_by );
+use Net::Curl::Simple ();
+
+binmode(STDOUT, "encoding(UTF-8)");
+
+my $curl = Net::Curl::Simple->new();
+$curl->get( 'https://www.poemist.com/api/v1/randompoems' );
+
+my ($poem) =
+ nsort_by { length( $_->{content} ) }
+ map { $_->@* }
+ decode_json( $curl->content )
+;
+
+say qq["$poem->{title}" by $poem->{poet}->{name}];
+say '';
+say $poem->{content};
+say '';
+say $poem->{url};
+
+exit 0;
+
+
diff --git a/challenge-023/randy-lauen/perl6/ch-3.p6 b/challenge-023/randy-lauen/perl6/ch-3.p6
new file mode 100644
index 0000000000..7ca7213c0a
--- /dev/null
+++ b/challenge-023/randy-lauen/perl6/ch-3.p6
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl6
+
+=begin SYNOPSIS
+
+Task:
+Write a script to use Random Poems API.
+
+Usage:
+ $ perl6 ch-3.p6
+
+Notes:
+This script prints the shortest poem returned by the randompoems endpoint.
+The Poemist API docs are located here: https://poemist.github.io/poemist-apidoc/
+
+=end SYNOPSIS
+
+use Cro::HTTP::Client;
+
+my $response = await Cro::HTTP::Client.get( "https://www.poemist.com/api/v1/randompoems" );
+my $json = await $response.body;
+my $poem = $json.sort({ .<content>.chars }).first;
+
+say qq["$poem<title>" by $poem<poet><name>];
+say '';
+say $poem<content>;
+say '';
+say $poem<url>;
+
+exit;
+
+