diff options
| -rwxr-xr-x | challenge-023/joelle-maslak/perl5/ch-3.pl | 40 | ||||
| -rwxr-xr-x | challenge-023/joelle-maslak/perl6/ch-3.p6 | 32 |
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"; + } + } +} + + |
