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
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');
}
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 );
# 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 ),
}
$c->stash->{'result'} = { status => 'notfound' };
}
- } elsif( $answer->{status} < 1 ) {
+ } elsif( $answer->{status} == 1 ) {
$c->stash->{'result'} = { 'status' => 'running' };
} else {
return _json_error( $c, 500,
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 ) {
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 ) = @_;
$c->stash->{'result'} = { 'error' => $errmsg };
$c->forward('View::JSON');
return 0;
-}
+}
sub _json_bool {
return $_[0] ? JSON::true : JSON::false;