aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-09-01 08:13:46 +0100
committerGitHub <noreply@github.com>2019-09-01 08:13:46 +0100
commit115a47c26b91bc4465dd5eb49be4f70999d566de (patch)
treead5fad6d8283f9853a89579b99006ddf496e73de
parentbc8c17c8c2ac784b8a09208b22974d1b50336642 (diff)
parent21b18d9c9108d50dd6504ee075dc77203a0107d7 (diff)
downloadperlweeklychallenge-club-115a47c26b91bc4465dd5eb49be4f70999d566de.tar.gz
perlweeklychallenge-club-115a47c26b91bc4465dd5eb49be4f70999d566de.tar.bz2
perlweeklychallenge-club-115a47c26b91bc4465dd5eb49be4f70999d566de.zip
Merge pull request #582 from jmaslak/joelle-23-3-1
Joelle's Solutions to 23.3 in P6 & P5
-rwxr-xr-xchallenge-023/joelle-maslak/perl5/ch-3.pl40
-rwxr-xr-xchallenge-023/joelle-maslak/perl6/ch-3.p632
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-023/joelle-maslak/perl5/ch-3.pl b/challenge-023/joelle-maslak/perl5/ch-3.pl
new file mode 100755
index 0000000000..83b20050b6
--- /dev/null
+++ b/challenge-023/joelle-maslak/perl5/ch-3.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+use v5.26;
+use strict;
+use warnings;
+
+# Gets the name of the most recent newsletter
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+use Getopt::Long;
+use Mojo::UserAgent;
+
+MAIN: {
+ my $url = 'https://www.poemist.com/api/v1/randompoems';
+ GetOptions( "url=s" => \$url );
+ if ( @ARGV > 0 ) { die("Invalid parameter") }
+
+ my $poem = get_poem($url);
+ say $poem->{title};
+ say "By " . $poem->{poet}{name};
+ say "";
+ say $poem->{content};
+}
+
+sub get_poem( $url ) {
+ my $ua = Mojo::UserAgent->new();
+
+ my $tx = $ua->get($url);
+
+ if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) {
+ my $body = $tx->result->json;
+ return $body->[0];
+ } else {
+ die "Error from API endpoint";
+ }
+}
+
diff --git a/challenge-023/joelle-maslak/perl6/ch-3.p6 b/challenge-023/joelle-maslak/perl6/ch-3.p6
new file mode 100755
index 0000000000..c744f50f6e
--- /dev/null
+++ b/challenge-023/joelle-maslak/perl6/ch-3.p6
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl6
+use v6;
+
+use Cro::HTTP::Client;
+
+sub MAIN(
+ Str:D :$url = 'https://www.poemist.com/api/v1/randompoems',
+) {
+ my $poem = get-poem(:$url);
+ say $poem[0]<title>;
+ say "By " ~ $poem[0]<poet><name>;
+ say "";
+ say $poem[0]<content>;
+}
+
+sub get-poem(Str:D :$url) {
+ my $client = Cro::HTTP::Client.new();
+
+ my $resp = await $client.get($url);
+ my $json = await $resp.body;
+
+ return $json;
+
+ CATCH {
+ when X::Cro::HTTP::Error {
+ my $body = await .response.body;
+ die "Error from API endpoint";
+ }
+ }
+}
+
+