From d706c081a9c60453988737f42f5751b21da0cb5b Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 16 Jun 2019 07:33:43 -0600 Subject: Work with actual API, not documented API! --- .../joelle-maslak/perl5/ch-3-simulator.pl | 33 +++++++++++++--------- challenge-012/joelle-maslak/perl5/ch-3.pl | 7 ++++- 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; + if $json:exists { + return $json[0]; + } else { + return ''; + } CATCH { when X::Cro::HTTP::Error { -- cgit