aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-08-29 18:05:24 -0300
committerandrezgz <andrezgz@gmail.com>2019-08-29 18:05:24 -0300
commitdf6f26067815074aec9f7a1cc7d4c7cb30cd27fd (patch)
treeda49f7cb22d1e4515954f6501b27c22d8ff55986
parentd66aa53b988a571018367c1fa3e56f6d056b2354 (diff)
downloadperlweeklychallenge-club-df6f26067815074aec9f7a1cc7d4c7cb30cd27fd.tar.gz
perlweeklychallenge-club-df6f26067815074aec9f7a1cc7d4c7cb30cd27fd.tar.bz2
perlweeklychallenge-club-df6f26067815074aec9f7a1cc7d4c7cb30cd27fd.zip
challenge-023 andrezgz task3 solution
-rw-r--r--challenge-023/andrezgz/perl5/ch-3.pl26
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-023/andrezgz/perl5/ch-3.pl b/challenge-023/andrezgz/perl5/ch-3.pl
new file mode 100644
index 0000000000..4950dc47ee
--- /dev/null
+++ b/challenge-023/andrezgz/perl5/ch-3.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-023/
+# Task #3
+# Write a script to use Random Poems API. This is the easiset API, I have come across so far.
+# You don't need API key for this. They have only route to work with (GET).
+# https://www.poemist.com/api/v1/randompoems
+# The API task is optional but we would love to see your solution.
+
+use strict;
+use warnings;
+
+use LWP::Simple qw (get);
+use JSON qw( decode_json );
+binmode STDOUT, ':encoding(UTF-8)';
+
+
+my $json = get('https://www.poemist.com/api/v1/randompoems');
+die 'No poem for you right now :(' unless defined $json;
+my $poems = decode_json( $json );
+
+my $rnd_poem = $poems->[int rand scalar @$poems];
+my $heading = $rnd_poem->{title}.' by '.$rnd_poem->{poet}->{name};
+my $line = q{=} x length $heading;
+
+print sprintf "%s\n%s\n\n%s\n", $heading, $line, $rnd_poem->{content};