aboutsummaryrefslogtreecommitdiff
path: root/challenge-021
diff options
context:
space:
mode:
authorRandy Lauen <randy.lauen@gmail.com>2019-08-17 18:50:53 -0500
committerRandy Lauen <randy.lauen@gmail.com>2019-08-17 18:50:53 -0500
commitab6aecbbe767d193b05ca04c083d2a3b3b268cf0 (patch)
tree9973027abb3942682a45bedf04399168b7d57a6d /challenge-021
parentc0308242591792881f13f6182752206c5408a500 (diff)
downloadperlweeklychallenge-club-ab6aecbbe767d193b05ca04c083d2a3b3b268cf0.tar.gz
perlweeklychallenge-club-ab6aecbbe767d193b05ca04c083d2a3b3b268cf0.tar.bz2
perlweeklychallenge-club-ab6aecbbe767d193b05ca04c083d2a3b3b268cf0.zip
solution for task 3
Diffstat (limited to 'challenge-021')
-rw-r--r--challenge-021/randy-lauen/perl6/ch-3.p661
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-021/randy-lauen/perl6/ch-3.p6 b/challenge-021/randy-lauen/perl6/ch-3.p6
new file mode 100644
index 0000000000..ca51f8a140
--- /dev/null
+++ b/challenge-021/randy-lauen/perl6/ch-3.p6
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl6
+
+=begin SYNOPSIS
+
+Task: Write a script to use BC Gov News API.
+
+This script prints out the top and feature post headlines.
+
+Example output:
+
+ $ perl6 ch-3.p6
+ topPostKey
+ * News Release: Third urgent and primary care centre opens in Greater Vancouver
+ * Backgrounder: North Vancouver UPCC
+
+ featurePostKey
+ * News Release: Province seeks feedback to ban, reduce, recycle more plastics
+
+=end SYNOPSIS
+
+use Cro::HTTP::Client;
+use URI::Encode;
+
+class ApiClient {
+ has %.params;
+ has $.base-uri;
+ has $!client = Cro::HTTP::Client.new(
+ :http('1.1'),
+ :base-uri( $!base-uri )
+ :headers( [ Accept => 'application/json' ] ),
+ );
+
+ method get( $path ) {
+ return self!get-json( "$path?" ~ %!params.pairs.map( { .key ~ '=' ~ .value } ).join('&') );
+ }
+
+ method !get-json( $uri ) {
+ my $encoded = uri_encode( $uri );
+ my $response = await $!client.get( $encoded );
+ return await $response.body;
+ }
+}
+
+
+sub MAIN() {
+ my $client = ApiClient.new(
+ base-uri => 'https://news.api.gov.bc.ca',
+ params => %( :api-version('1.0') ),
+ );
+ my %homeInfo = $client.get( '/api/Home' );
+
+ for <topPostKey featurePostKey> -> $key {
+ my %postInfo = $client.get( "/api/Posts/%homeInfo{$key}" );
+ say $key;
+
+ for %postInfo<documents>.flat -> $doc {
+ say "* $doc<pageTitle>: $doc<headline>";
+ }
+ say '';
+ }
+}