aboutsummaryrefslogtreecommitdiff
path: root/challenge-017
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-07-21 16:01:12 -0600
committerJoelle Maslak <jmaslak@antelope.net>2019-07-21 16:01:12 -0600
commit85e978869cd283a0e177baabe5305affbc6c99dd (patch)
treee160623856263bd1054f54e5962d93e1b2c45464 /challenge-017
parentd62b85003dc0bf26d579a93fd0230b74d18f35cf (diff)
downloadperlweeklychallenge-club-85e978869cd283a0e177baabe5305affbc6c99dd.tar.gz
perlweeklychallenge-club-85e978869cd283a0e177baabe5305affbc6c99dd.tar.bz2
perlweeklychallenge-club-85e978869cd283a0e177baabe5305affbc6c99dd.zip
Perl 5 solution to 17.3
Diffstat (limited to 'challenge-017')
-rwxr-xr-xchallenge-017/joelle-maslak/perl5/ch-3.pl95
1 files changed, 95 insertions, 0 deletions
diff --git a/challenge-017/joelle-maslak/perl5/ch-3.pl b/challenge-017/joelle-maslak/perl5/ch-3.pl
new file mode 100755
index 0000000000..3b2f6d643c
--- /dev/null
+++ b/challenge-017/joelle-maslak/perl5/ch-3.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/env perl
+use v5.26;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+use Getopt::Long;
+use Mojo::UserAgent;
+use Path::Tiny;
+use Perl6::Slurp;
+
+MAIN: {
+ binmode STDOUT, ':utf8';
+
+ my $configfile = path( $ENV{HOME} // "", ".bhagavad-gita" )->stringify();
+ my $baseurl = 'https://bhagavadgita.io/';
+ GetOptions(
+ "base-url=s" => \$baseurl,
+ "config-file=s" => \$configfile,
+ );
+ die "Must provide chapter and verse" if @ARGV < 2;
+ my $chapter = 0+shift(@ARGV);
+ my $verse = 0+shift(@ARGV);
+
+ if ( @ARGV != 0 ) { die("Unknown parameter"); }
+
+ my $id = get_client_id($configfile);
+ my $secret = get_client_secret($configfile);
+ my $token = get_token($id, $secret, $baseurl);
+ say get_verse($token, $chapter, $verse, $baseurl);
+}
+
+sub get_client_id($configfile) {
+ my (@lines) = grep { length($_) } slurp($configfile);
+ die "Config-file ($configfile) must consist of two lines" if @lines != 2;
+ chomp $lines[0];
+ return $lines[0];
+}
+
+sub get_client_secret($configfile) {
+ my (@lines) = grep { length($_) } slurp($configfile);
+ die "Config-file ($configfile) must consist of two lines" if @lines != 2;
+ chomp $lines[1];
+ return $lines[1];
+}
+
+sub get_token($id, $secret, $baseurl) {
+ my $ua = Mojo::UserAgent->new();
+ $ua->max_redirects(16);
+
+ my $tx = $ua->post(
+ "${baseurl}auth/oauth/token",
+ form => {
+ client_id => $id,
+ client_secret => $secret,
+ grant_type => 'client_credentials',
+ scope => 'verse chapter',
+ },
+ );
+
+ if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) {
+ my $json = $tx->result->json;
+ return $json->{access_token};
+ }
+
+ my $body = $tx->result->json;
+ die "Error from API endpoint: " . $body->{error};
+}
+
+sub get_verse($token, $chapter, $verse, $baseurl) {
+ my $ua = Mojo::UserAgent->new();
+ $ua->once(
+ start => sub ( $ua, $tx ) {
+ $tx->req->headers->header( 'Authorization' => "Bearer " . $token);
+ }
+ );
+ $ua->max_redirects(16);
+
+ my $tx = $ua->get(
+ "${baseurl}api/v1/chapters/$chapter/verses/$verse",
+ );
+
+ if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) {
+ my $json = $tx->result->json;
+ return $json->{text};
+ }
+
+ my $body = $tx->result->json;
+ die "Error from API endpoint: " . $body->{message};
+}
+