diff options
| author | Joelle Maslak <jmaslak@antelope.net> | 2019-06-15 21:48:41 -0600 |
|---|---|---|
| committer | Joelle Maslak <jmaslak@antelope.net> | 2019-06-15 21:48:41 -0600 |
| commit | b595bcb864e2113267c2c82b8946234516e40fea (patch) | |
| tree | 3eef9f8f187e1978a5b7d455822d43b4d9f12443 | |
| parent | 7a9a5d591c2bc3d66f16f14b055ea8ea922b8930 (diff) | |
| download | perlweeklychallenge-club-b595bcb864e2113267c2c82b8946234516e40fea.tar.gz perlweeklychallenge-club-b595bcb864e2113267c2c82b8946234516e40fea.tar.bz2 perlweeklychallenge-club-b595bcb864e2113267c2c82b8946234516e40fea.zip | |
Solution to 12.3 in P5 & P6
| -rwxr-xr-x | challenge-012/joelle-maslak/perl5/ch-3.pl | 77 | ||||
| -rwxr-xr-x | challenge-012/joelle-maslak/perl5/simulator-ch-3.pl | 42 | ||||
| -rwxr-xr-x | challenge-012/joelle-maslak/perl6/ch-3.p6 | 63 |
3 files changed, 182 insertions, 0 deletions
diff --git a/challenge-012/joelle-maslak/perl5/ch-3.pl b/challenge-012/joelle-maslak/perl5/ch-3.pl new file mode 100755 index 0000000000..52e08c599e --- /dev/null +++ b/challenge-012/joelle-maslak/perl5/ch-3.pl @@ -0,0 +1,77 @@ +#!/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 Mojo::Util qw(url_escape); +use Mojo::UserAgent; +use Path::Tiny; +use Perl6::Slurp; + +MAIN: { + my $configfile = path( $ENV{HOME} // "", ".stands4u" )->stringify(); + my $urlbase = 'http://www.stands4.com'; + GetOptions( + "urlbase=s" => \$urlbase, + "config-file=s" => \$configfile, + ); + if ( @ARGV != 1 ) { die("Must specify one word to look up"); } + + my $uid = get_uid($configfile); + my $apikey = get_api_key($configfile); + + my (@syns) = get_synonyms( + uid => $uid, + apikey => $apikey, + urlbase => $urlbase, + word => $ARGV[0], + ); +} + +sub get_uid($configfile) { + my (@lines) = grep { length($_) } slurp($configfile); + die "Config-file ($configfile) must consist of one line" if @lines != 2; + chomp $lines[0]; + return $lines[0]; +} + +sub get_api_key($configfile) { + my (@lines) = grep { length($_) } slurp($configfile); + die "Config-file ($configfile) must consist of one line" if @lines != 2; + chomp $lines[1]; + return $lines[1]; +} + +# Usage: +# uid = Your User ID +# apikey = Your API key +# urlbase = Base URL +# word = Word to look up +# +sub get_synonyms(%args) { + my $url = $args{urlbase} . '/services/v2/syno.php?'; + + $url .= 'uid=' . url_escape( $args{uid} ); + $url .= '&tokenid=' . url_escape( $args{apikey} ); + $url .= '&word=' . url_escape( $args{word} ); + $url .= '&format=json'; + + my $ua = Mojo::UserAgent->new(); + + my $tx = $ua->get($url); + if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) { + my $json = $tx->result->json; + say "Synonyms: " . $json->{results}{result}{synonyms}; + return; + } + + my $body = $tx->result->json; + die "Error from API endpoint: " . $body->{error}; +} + diff --git a/challenge-012/joelle-maslak/perl5/simulator-ch-3.pl b/challenge-012/joelle-maslak/perl5/simulator-ch-3.pl new file mode 100755 index 0000000000..e25ffff641 --- /dev/null +++ b/challenge-012/joelle-maslak/perl5/simulator-ch-3.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use v5.22; + +use Mojolicious::Lite; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +get '/services/v2/syno.php' => sub ($c) { + if (($c->param('uid') // '') ne '1001') { + say $c->param('uid'); + $c->render(status => 500, json => { error => 'Invalid User ID' }); + } elsif (($c->param('tokenid') // '') ne 'tk324324') { + $c->render(status => 500, json => { error => 'Invalid Token' }); + } elsif (($c->param('format') // '') ne 'json') { + $c->render(status => 500, json => { error => 'Invalid Format' }); + } elsif (($c->param('word') // '') eq '') { + $c->render(status => 500, json => { error => 'Must provide word' }); + } else { + say "WORD: " . $c->param('word'); + $c->render( + json => { + results => { + result => { + term => "consistent", + definition => "(sometimes followed by 'with') in agreement or consistent or reliable", + partofspeech => "adj", + synonyms => "ordered, coherent, logical, reproducible, uniform", + antonyms => "scratchy, unreconciled, uneven, contradictory, inconsistent, conflicting, incompatible, spotty, heterogeneous, discrepant, heterogenous, self-contradictory, unconformable", + }, + }, + } + ); + } +}; + +app->start; diff --git a/challenge-012/joelle-maslak/perl6/ch-3.p6 b/challenge-012/joelle-maslak/perl6/ch-3.p6 new file mode 100755 index 0000000000..f2c540903a --- /dev/null +++ b/challenge-012/joelle-maslak/perl6/ch-3.p6 @@ -0,0 +1,63 @@ +#!/usr/bin/env perl6 +use v6; + +use Cro::HTTP::Client; +use URI::Encode; + +sub MAIN( + Str:D $word, + Str:D :$config-file? = $*HOME.add(".stands4u").Str, + Str:D :$urlbase? = 'http://www.stands4.com'; +) { + say "Synonyms: " ~ get-synonyms(:$word, :$urlbase, :$config-file); +} + +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 two lines" if @lines.elems ≠ 2; + + return @lines[1]; +} + +sub get-uid(Str:D $config-file -->Str:D) { + my @lines = $config-file.IO.lines().grep( *.chars > 0 ); + die "Config-file ($config-file) must consist of two lines" if @lines.elems ≠ 2; + + return @lines[0]; +} + +# Usage: +# :$word = word to look up +# :$urlbase = base URL +# :$config-file = Name of config file to use +# +sub get-synonyms( + Str:D :$word, + Str:D :$urlbase, + Str:D :$config-file, +) { + my $api-key = get-api-key($config-file); + my $uid = get-uid\ ($config-file); + my $url = $urlbase; + + my $querystring = ""; + + $querystring ~= "uid={uri_encode_component($uid)}"; + $querystring ~= "&tokenid={uri_encode_component($api-key)}"; + $querystring ~= "&word={uri_encode_component($word)}"; + $querystring ~= "&format=json"; + + my $client = Cro::HTTP::Client.new( base-uri => $url ); + my $resp = await $client.get("/services/v2/syno.php?$querystring"); + my $json = await $resp.body; + + return $json<results><result><synonyms>; + + CATCH { + when X::Cro::HTTP::Error { + my $body = await .response.body; + die "Error from API endpoint: $body<error>"; + } + } +} + |
