diff options
| -rwxr-xr-x | challenge-013/joelle-maslak/perl5/ch-3.pl | 74 | ||||
| -rwxr-xr-x | challenge-013/joelle-maslak/perl6/ch-3.p6 | 108 |
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; +} |
