fix Stemweb reporting bug
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stemweb.pm
index 8f7aa4e..a9e7755 100644 (file)
@@ -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,7 +91,7 @@ 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'} = decode_json( $resp->content );
        } else {
@@ -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 );
@@ -162,6 +166,7 @@ sub _process_stemweb_result {
                }
                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 ),
@@ -179,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,
@@ -221,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 ) {
@@ -268,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 ) = @_;
@@ -275,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;