X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fstemmaweb%2FController%2FStemweb.pm;h=a9e77556c0ad41914516a1ec7b243d2d5241e6ef;hb=39283bfbf63a29df210f3de33a6dcdb01d70e397;hp=2a319030b9c7008b904251f9ff43fb0fb2ac6173;hpb=664580033ad210926cafbb4940b20078f61b7cc0;p=scpubgit%2Fstemmaweb.git diff --git a/lib/stemmaweb/Controller/Stemweb.pm b/lib/stemmaweb/Controller/Stemweb.pm index 2a31903..a9e7755 100644 --- a/lib/stemmaweb/Controller/Stemweb.pm +++ b/lib/stemmaweb/Controller/Stemweb.pm @@ -5,13 +5,17 @@ use Encode qw/ decode_utf8 /; use JSON; use LWP::UserAgent; use Safe::Isa; +use Scalar::Util qw/ looks_like_number /; use TryCatch; use URI; BEGIN { extends 'Catalyst::Controller' } -## TODO Move the /algorithms/available function to the Stemweb module -my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi'; +has stemweb_url => ( + is => 'ro', + isa => 'Str', + default => 'http://slinkola.users.cs.helsinki.fi', + ); =head1 NAME @@ -87,11 +91,11 @@ parameters. Returns the JSON answer as obtained from Stemweb. sub available :Local :Args(0) { my( $self, $c ) = @_; my $ua = LWP::UserAgent->new(); - my $resp = $ua->get( $STEMWEB_BASE_URL . '/algorithms/available' ); + my $resp = $ua->get( $self->stemweb_url . '/algorithms/available' ); if( $resp->is_success ) { - $c->stash->{'result'} = $resp->content; + $c->stash->{'result'} = decode_json( $resp->content ); } else { - $c->stash->{'result'} = '{}'; + $c->stash->{'result'} = {}; } $c->forward('View::JSON'); } @@ -109,7 +113,7 @@ indicate that the job is still running. sub query :Local :Args(1) { my( $self, $c, $jobid ) = @_; my $ua = LWP::UserAgent->new(); - my $resp = $ua->get( $STEMWEB_BASE_URL . "/algorithms/jobstatus/$jobid" ); + my $resp = $ua->get( $self->stemweb_url . "/algorithms/jobstatus/$jobid" ); if( $resp->is_success ) { # Process it my $response = decode_utf8( $resp->content ); @@ -160,9 +164,9 @@ sub _process_stemweb_result { # Check all stemmata for the given jobid and return them. @$stemmata = grep { $_->came_from_jobid && $_->from_jobid eq $answer->{jobid} } $tradition->stemmata; } - $DB::single = 1; if( @$stemmata ) { # If we got here, success! + # TODO Use helper in Root.pm to do this my @steminfo = map { { name => $_->identifier, directed => _json_bool( !$_->is_undirected ), @@ -180,7 +184,7 @@ sub _process_stemweb_result { } $c->stash->{'result'} = { status => 'notfound' }; } - } elsif( $answer->{status} < 1 ) { + } elsif( $answer->{status} == -1 ) { $c->stash->{'result'} = { 'status' => 'running' }; } else { return _json_error( $c, 500, @@ -222,12 +226,12 @@ sub request :Local :Args(0) { data => $t->collation->as_tsv({noac => 1}), userid => $c->user->get_object->email, textid => $tid, - parameters => $reqparams }; + parameters => _cast_nonstrings( $reqparams ) }; # Call to the appropriate URL with the request parameters. my $ua = LWP::UserAgent->new(); $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) ); - my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/", + my $resp = $ua->post( $self->stemweb_url . "/algorithms/process/$algorithm/", 'Content-Type' => 'application/json; charset=utf-8', 'Content' => encode_json( $stemweb_request ) ); if( $resp->is_success ) { @@ -269,6 +273,20 @@ sub _check_permission { return _json_error( $c, 403, 'You do not have permission to view this tradition.' ); } +# QUICK HACK to deal with strict Stemweb validation. +sub _cast_nonstrings { + my $params = shift; + foreach my $k ( keys %$params ) { + my $v = $params->{$k}; + if( looks_like_number( $v ) ) { + $params->{$k} = $v * 1; + } elsif ( !defined $v || $v eq 'true' ) { + $params->{$k} = _json_bool( $v ); + } + } + return $params; +} + # Helper to throw a JSON exception sub _json_error { my( $c, $code, $errmsg ) = @_; @@ -276,7 +294,7 @@ sub _json_error { $c->stash->{'result'} = { 'error' => $errmsg }; $c->forward('View::JSON'); return 0; -} +} sub _json_bool { return $_[0] ? JSON::true : JSON::false;