aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-22 23:40:14 +0100
committerGitHub <noreply@github.com>2019-06-22 23:40:14 +0100
commitdf9d25f3e112532243474dd14ee3ffc531d5f13c (patch)
tree2c0ee2e06b0b3f2e6f5fca5a38d6c27b91b5a674
parentf89590c962cbfe3822ffdcdde5f337336481eca8 (diff)
parent27e122ba91a3024dff5a0f28d53a54a32ac819af (diff)
downloadperlweeklychallenge-club-df9d25f3e112532243474dd14ee3ffc531d5f13c.tar.gz
perlweeklychallenge-club-df9d25f3e112532243474dd14ee3ffc531d5f13c.tar.bz2
perlweeklychallenge-club-df9d25f3e112532243474dd14ee3ffc531d5f13c.zip
Merge pull request #285 from jmaslak/joelle-13-3-1
Solution for challenge 12.3 in P6 & P5
-rwxr-xr-xchallenge-013/joelle-maslak/perl5/ch-3.pl74
-rwxr-xr-xchallenge-013/joelle-maslak/perl6/ch-3.p6108
2 files changed, 182 insertions, 0 deletions
diff --git a/challenge-013/joelle-maslak/perl5/ch-3.pl b/challenge-013/joelle-maslak/perl5/ch-3.pl
new file mode 100755
index 0000000000..d7949b683f
--- /dev/null
+++ b/challenge-013/joelle-maslak/perl5/ch-3.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+use v5.22;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+use Getopt::Long;
+use JSON::PP;
+use Mojo::Util qw(url_escape);
+use Mojo::UserAgent;
+use Path::Tiny;
+use Perl6::Slurp;
+
+MAIN: {
+ my $configfile = path( $ENV{HOME} // "", ".rapidapi" )->stringify();
+ my $urlbase = 'https://wordsapiv1.p.mashape.com/words/';
+ GetOptions(
+ "urlbase=s" => \$urlbase,
+ "config-file=s" => \$configfile,
+ );
+ if ( @ARGV != 1 ) { die("Must specify one word to look up"); }
+
+ my $apikey = get_api_key($configfile);
+
+ my ($info) = get_info(
+ apikey => $apikey,
+ urlbase => $urlbase,
+ word => $ARGV[0],
+ );
+}
+
+sub get_api_key($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];
+}
+
+# Usage:
+# apikey = Your API key
+# urlbase = Base URL
+# word = Word to look up
+#
+sub get_info(%args) {
+ my $url = $args{urlbase} . url_escape( $args{word} );
+
+ my $ua = Mojo::UserAgent->new();
+ $ua->max_redirects(16);
+ $ua->once(
+ start => sub ( $ua, $tx ) {
+ $tx->req->headers->header( 'X-Mashape-Key' => $args{apikey} );
+ }
+ );
+
+ my $tx = $ua->get($url);
+ if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) {
+ my $json = $tx->result->json;
+ if ( exists( $json->{results} ) ) {
+ my $pretty = JSON::PP->new->utf8->pretty->indent->encode($json);
+ say $pretty;
+ } else {
+ say "Could not fetch info from API";
+ }
+ return;
+ }
+
+ my $body = $tx->result->json;
+ die "Error from API endpoint: " . $body->{message};
+}
+
diff --git a/challenge-013/joelle-maslak/perl6/ch-3.p6 b/challenge-013/joelle-maslak/perl6/ch-3.p6
new file mode 100755
index 0000000000..4e1d399070
--- /dev/null
+++ b/challenge-013/joelle-maslak/perl6/ch-3.p6
@@ -0,0 +1,108 @@
+#!/usr/bin/env perl6
+use v6;
+
+use Cro::HTTP::Client;
+use URI::Encode;
+
+sub MAIN(
+ Str:D $word,
+ Str:D :$config-file? = $*HOME.add(".rapidapi").Str,
+ Str:D :$urlbase? = 'https://wordsapiv1.p.mashape.com/words/';
+) {
+ my $info = get-word(:$word, :$urlbase, :$config-file);
+ if $info ne '' {
+ pretty-print($info);
+ } else {
+ say "No results";
+ }
+}
+
+sub get-api-key(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.first;
+}
+
+# Usage:
+# :$word = word to look up
+# :$urlbase = base URL
+# :$config-file = Name of config file to use
+#
+sub get-word(
+ Str:D :$word,
+ Str:D :$urlbase,
+ Str:D :$config-file,
+) {
+ my $api-key = get-api-key($config-file);
+ my $url = $urlbase;
+
+ my $client = Cro::HTTP::Client.new(
+ base-uri => $url,
+ headers => [ X-Mashape-Key => $api-key ],
+ );
+ my $resp = await $client.get(uri_encode_component($word));
+ my $json = await $resp.body;
+
+ if $json<results>:exists {
+ return $json;
+ } else {
+ return;
+ }
+
+ CATCH {
+ when X::Cro::HTTP::Error {
+ my $body = await .response.body;
+ die "Error from API endpoint: $body<message>";
+ }
+ }
+}
+
+multi sub pretty-print(Array $structure, $indent = "", $continuation = False) {
+ say "" if $continuation;
+ my $cnt = 0;
+ for @$structure -> $item {
+ print "{$indent}[{$cnt.fmt("%2d")}] ";
+ pretty-print($item, "$indent ", True);
+ $cnt++;
+ }
+}
+multi sub pretty-print(Hash $structure, $indent = "", $continuation = False) {
+ say "" if $continuation;
+ my $max = ($structure.keys.map( { cannonical-key($_) } )».chars).max;
+ for $structure.keys.sort -> $key {
+ print "$indent";
+ print "{ cannonical-key($key) }: ".fmt("%-{$max+2}s");
+ pretty-print($structure{$key}, "{$indent}{' ' x ($max + 2)}", True);
+ }
+}
+multi sub pretty-print(Str() $structure, $indent = "", $continuation = False) {
+ if $continuation {
+ say "$structure";
+ } else {
+ say "$indent$structure";
+ }
+}
+
+sub cannonical-key($key) {
+ my %cannonical =
+ definition => "Definition",
+ partOfSpeech => "Part of Speech",
+ typeOf => "Type of (less speicific)",
+ hasTypes => "Has Types",
+ partOf => "Part of (thing)",
+ hasParts => "Has Parts",
+ examples => "Examples",
+ syllables => "Syllables",
+ count => "Count",
+ list => "List",
+ word => "Word",
+ frquency => "Frequency",
+ inCategory => "In Category",
+ pronunciation => "Pronunciation",
+ results => "Results",
+ synonyms => "Synonyms",
+ ;
+
+ return %cannonical{$key} // $key;
+}