diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-06-16 15:04:30 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-06-16 15:04:30 +0100 |
| commit | 7ce1d5e40ed4776bbda347ca2e702c3be6afc18a (patch) | |
| tree | 34bb8b5ff7c0f4c7cca643ca01e012e3363c69cd | |
| parent | e28198cc95142b0f6aced153c629537d1b1b939b (diff) | |
| parent | f7d2cf89623ec0925208d3353406c04ca6019fee (diff) | |
| download | perlweeklychallenge-club-7ce1d5e40ed4776bbda347ca2e702c3be6afc18a.tar.gz perlweeklychallenge-club-7ce1d5e40ed4776bbda347ca2e702c3be6afc18a.tar.bz2 perlweeklychallenge-club-7ce1d5e40ed4776bbda347ca2e702c3be6afc18a.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rwxr-xr-x | challenge-012/joelle-maslak/perl5/ch-3-simulator.pl | 33 | ||||
| -rwxr-xr-x | challenge-012/joelle-maslak/perl5/ch-3.pl | 7 | ||||
| -rwxr-xr-x | challenge-012/joelle-maslak/perl6/ch-3.p6 | 13 |
3 files changed, 36 insertions, 17 deletions
diff --git a/challenge-012/joelle-maslak/perl5/ch-3-simulator.pl b/challenge-012/joelle-maslak/perl5/ch-3-simulator.pl index e25ffff641..d387d97837 100755 --- a/challenge-012/joelle-maslak/perl5/ch-3-simulator.pl +++ b/challenge-012/joelle-maslak/perl5/ch-3-simulator.pl @@ -12,28 +12,33 @@ use feature 'signatures'; no warnings 'experimental::signatures'; get '/services/v2/syno.php' => sub ($c) { - if (($c->param('uid') // '') ne '1001') { + 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' }); + $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' } ); + } elsif ( $c->param('word') eq 'bogus word' ) { + say "WORD: " . $c->param('word'); + $c->render( json => {} ); } else { say "WORD: " . $c->param('word'); $c->render( json => { - results => { - result => { + result => [ + { term => "consistent", - definition => "(sometimes followed by 'with') in agreement or consistent or reliable", + 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", + synonyms => "ordered, coherent, logical, reproducible, uniform", + antonyms => +"scratchy, unreconciled, uneven, contradictory, inconsistent, conflicting, incompatible, spotty, heterogeneous, discrepant, heterogenous, self-contradictory, unconformable", }, - }, + ], } ); } diff --git a/challenge-012/joelle-maslak/perl5/ch-3.pl b/challenge-012/joelle-maslak/perl5/ch-3.pl index 52e08c599e..e13f1069c2 100755 --- a/challenge-012/joelle-maslak/perl5/ch-3.pl +++ b/challenge-012/joelle-maslak/perl5/ch-3.pl @@ -63,11 +63,16 @@ sub get_synonyms(%args) { $url .= '&format=json'; my $ua = Mojo::UserAgent->new(); + $ua->max_redirects(16); 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}; + if (exists($json->{result})) { + say "Synonyms: " . $json->{result}[0]{synonyms}; + } else { + say "No synonyms found"; + } return; } diff --git a/challenge-012/joelle-maslak/perl6/ch-3.p6 b/challenge-012/joelle-maslak/perl6/ch-3.p6 index f2c540903a..352c8c4079 100755 --- a/challenge-012/joelle-maslak/perl6/ch-3.p6 +++ b/challenge-012/joelle-maslak/perl6/ch-3.p6 @@ -9,7 +9,12 @@ sub MAIN( Str:D :$config-file? = $*HOME.add(".stands4u").Str, Str:D :$urlbase? = 'http://www.stands4.com'; ) { - say "Synonyms: " ~ get-synonyms(:$word, :$urlbase, :$config-file); + my $syn = get-synonyms(:$word, :$urlbase, :$config-file); + if $syn ne '' { + say "Synonyms: $syn"; + } else { + say "No synonyms exist"; + } } sub get-api-key(Str:D $config-file -->Str:D) { @@ -51,7 +56,11 @@ sub get-synonyms( my $resp = await $client.get("/services/v2/syno.php?$querystring"); my $json = await $resp.body; - return $json<results><result><synonyms>; + if $json<result>:exists { + return $json<result>[0]<synonyms>; + } else { + return ''; + } CATCH { when X::Cro::HTTP::Error { |
