-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelperMethods.pm
64 lines (53 loc) · 1.5 KB
/
HelperMethods.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package HelperMethods;
use strict;
use warnings;
#----------------------------------------------------------------------
# imports
#----------------------------------------------------------------------
use JSON;
use CGI;
#----------------------------------------------------------------------
# helper routines
#----------------------------------------------------------------------
# extremely ad hoc method to extract URIs from JSON generated
# by tnrs service and tree store service
sub get_values_from_json
{
my $json = shift;
}
sub get_taxa_uris_from_tnrs_output
{
my $json = shift;
my %uris = get_uri_mappings_from_tnrs_output($json);
return map(@$_, values %uris);
}
sub get_uri_mappings_from_tnrs_output
{
my $json = shift;
my $data = JSON->new()->decode($json);
my %uris = ();
foreach my $name (@{$data->{names}}) {
my $submitted_name = $name->{submittedName};
foreach my $match (@{$name->{matches}}) {
foreach my $uri ($match->{uri}) {
$uris{$submitted_name} = [] unless exists $uris{$submitted_name};
push(@{$uris{$submitted_name}}, $uri);
}
}
}
return %uris;
}
# a 'die' method that works in both CGI and commandline context
sub fatal
{
my ($msg, $is_cgi, $http_status) = @_;
if ($is_cgi) {
$http_status ||= 500;
print CGI->header(-status => $http_status, -type => 'text/plain');
print $msg;
exit 0;
} else {
die "$msg\n";
}
}
1;