aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-019/joelle-maslak/perl5/ch-3.pl62
-rwxr-xr-xchallenge-019/joelle-maslak/perl6/ch-3.p655
2 files changed, 117 insertions, 0 deletions
diff --git a/challenge-019/joelle-maslak/perl5/ch-3.pl b/challenge-019/joelle-maslak/perl5/ch-3.pl
new file mode 100755
index 0000000000..6bcd2cdcdd
--- /dev/null
+++ b/challenge-019/joelle-maslak/perl5/ch-3.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+use v5.26;
+use strict;
+use warnings;
+
+# Not having details of which part of the API to implement, this script
+# looks up all the top book on the selectd list (defaults to
+# hardcover-nonfiction)
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+use Getopt::Long;
+use Mojo::UserAgent;
+use Mojo::Util qw(url_escape);
+use Path::Tiny;
+use Perl6::Slurp;
+
+MAIN: {
+ my $configfile = path( $ENV{HOME} // "", ".nyt" )->stringify();
+ my $baseurl = 'https://api.nytimes.com/svc/books/v3/';
+ GetOptions(
+ "base-url=s" => \$baseurl,
+ "config-file=s" => \$configfile,
+ );
+ my $list = shift(@ARGV) // 'hardcover-nonfiction';
+
+ if ( @ARGV != 0 ) { die("Unknown parameter"); }
+
+ my $secret = get_client_secret($configfile);
+ my $books = get_books( $secret, $list, $baseurl );
+
+ die("No results, are you using a valid list name?") if $books->{num_results} == 0;
+
+ my $top = $books->{results}[0];
+ say "Top book on $list: " . $top->{book_details}[0]{title};
+}
+
+sub get_client_secret($configfile) {
+ my (@lines) = grep { length($_) } slurp($configfile);
+ die "Config-file ($configfile) must consist of one line" if @lines != 1;
+ chomp $lines[0];
+ return $lines[0];
+}
+
+sub get_books ( $secret, $list, $baseurl ) {
+ my $ua = Mojo::UserAgent->new();
+
+ my $tx = $ua->get(
+ "${baseurl}lists.json?list=" . url_escape($list) . "&api-key=" . url_escape($secret),
+ );
+
+ if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) {
+ return $tx->result->json;
+ }
+
+ my $body = $tx->result->json;
+ die "Error from API endpoint: " . $body->{fault}{faultstring};
+}
+
diff --git a/challenge-019/joelle-maslak/perl6/ch-3.p6 b/challenge-019/joelle-maslak/perl6/ch-3.p6
new file mode 100755
index 0000000000..96d4510714
--- /dev/null
+++ b/challenge-019/joelle-maslak/perl6/ch-3.p6
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl6
+use v6;
+
+# Not having details of whcih part of the API to implement, this script
+# looks up the top book on the selected list (defaults to
+# hardcover-nonfiction)
+
+use Cro::HTTP::Client;
+use URI::Encode;
+
+sub MAIN(
+ Str:D $list = 'hardcover-nonfiction',
+ Str:D :$config-file? = $*HOME.add(".nyt").Str,
+ Str:D :$base-url = 'https://api.nytimes.com/svc/books/v3/';
+) {
+ my $books = get-books(:$config-file, :$base-url, :$list);
+ die "No results. Are you using a valid list name?" if $books<num_results> == 0;
+
+ my $top = $books<results>[0];
+ say "Top book on $list: $top<book_details>[0]<title>";
+}
+
+sub get-client-secret(Str:D $config-file -->Str:D) {
+ my @lines = $config-file.IO.lines().grep( *.chars > 0 );
+ die "Config-file ($config-file) must consist of one line" if @lines.elems ≠ 1;
+
+ return @lines[0];
+}
+
+sub get-books(
+ Str:D :$list,
+ Str:D :$base-url,
+ Str:D :$config-file,
+) {
+ my $secret = get-client-secret($config-file);
+
+ my $client = Cro::HTTP::Client.new(
+ base-uri => $base-url,
+ );
+
+ my $resp = await $client.get(
+ "lists.json?list={ uri_encode_component($list) }" ~
+ "&api-key={ uri_encode_component($secret) }",
+ );
+ my $json = await $resp.body;
+ return $json;
+
+ CATCH {
+ when X::Cro::HTTP::Error {
+ my $body = await .response.body;
+ die "Error from API endpoint: $body<fault><faultstring>";
+ }
+ }
+}
+