Skip to content

Commit

Permalink
changes to api parser to handle more attribute and paramater structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Harrison committed May 2, 2013
1 parent 7ba2c01 commit cf7b963
Showing 1 changed file with 96 additions and 105 deletions.
201 changes: 96 additions & 105 deletions src/WebApplication/scripts/api2html.pl
Original file line number Diff line number Diff line change
Expand Up @@ -72,114 +72,105 @@ sub usage {

$html .= "<h1><a name='overview' style='padding-top: 50px;'>".$structure->{service}->{name}." Overview</a></h1><p>".$structure->{service}->{description}."</p><hr>";

my %param_types = ( body => "<p>This parameter must be passed in the message body.</p>",
options => "<p>This is an optional parameter and may be passed in the query string.</p>",
required => "<p>This is a required parameter and must be passed as a REST parameter.</p>" );

# iterate over the resources
foreach my $res (sort { $a->{name} cmp $b->{name} } @{$structure->{resources}}) {
$html .= "<h1><a name='".$res->{name}."' style='padding-top: 50px;'>".$res->{name}."</a></h1><h2>Description</h2><p>".$res->{description}."</p>";
foreach my $req (@{$res->{requests}}) {
$html .= "<h2>".$req->{name}." - request</h2><h3>Description</h3><p>".$req->{description}."</p><p>This is a ".$req->{type}." ".$req->{method}." request.";
unless (ref($req->{attributes})) {
$html .= " This request has no parameters.";
next;
}
$html .= "</p><h3>Parameters</h3><ul>";
foreach my $param (keys(%{$req->{parameters}->{body}})) {
my $pm = $req->{parameters}->{body}->{$param};
$pm->[0] =~ s/cv/controlled vocabulary/;
$html .= "<li><b>$param</b> (".$pm->[0];
if (ref($pm->[1]) eq 'ARRAY') {
if (! ref($pm->[0]) && $pm->[0] eq 'list') {
$html .= " of ".$pm->[1]->[0].")</li><p>";
$html .= $pm->[1]->[1]."</p>";
} else {
$html .= ")</li><p>";
$html .= "This parameter value can be chosen from the following (the first being default):</p><ul style='list-style: none;'>";
foreach my $cvitem (@{$pm->[1]}) {
$html .= "<li><b>".$cvitem->[0]."</b> - ".$cvitem->[1]."</li>";
}
$html .= "</ul><br>";
}
} else {
$html .= ")</p><p>".$pm->[1]."</p>";
}
$html .= "<p>This parameter must be passed in the message body.</p>";
}
foreach my $param (keys(%{$req->{parameters}->{options}})) {
my $pm = $req->{parameters}->{options}->{$param};
$pm->[0] =~ s/cv/controlled vocabulary/;
$html .= "<li><b>$param</b> (".$pm->[0];
if (ref($pm->[1]) eq 'ARRAY') {
if (! ref($pm->[0]) && $pm->[0] eq 'list') {
$html .= " of ".$pm->[1]->[0].")</li><p>";
$html .= $pm->[1]->[1]."</p>";
} else {
$html .= ")</li><p>";
$html .= "This parameter value can be chosen from the following (the first being default):</p><ul style='list-style: none;'>";
foreach my $cvitem (@{$pm->[1]}) {
$html .= "<li><b>".$cvitem->[0]."</b> - ".$cvitem->[1]."</li>";
}
$html .= "</ul><br>";
}
} else {
$html .= ")</li><p>".$pm->[1]."</p>";
}
$html .= "<p>This is an optional parameter and may be passed in the query string.</p>";
}
foreach my $param (keys(%{$req->{parameters}->{required}})) {
my $pm = $req->{parameters}->{required}->{$param};
$pm->[0] =~ s/cv/controlled vocabulary/;
$html .= "<li><b>$param</b> (".$pm->[0];
if (ref($pm->[1]) eq 'ARRAY') {
if (! ref($pm->[0]) && $pm->[0] eq 'list') {
$html .= " of ".$pm->[1]->[0].")</li><p>";
$html .= $pm->[1]->[1]."</p>";
} else {
$html .= ")</li><p>";
$html .= "This parameter value can be chosen from the following (the first being default):</p><ul style='list-style: none;'>";
foreach my $cvitem (@{$pm->[1]}) {
$html .= "<li><b>".$cvitem->[0]."</b> - ".$cvitem->[1]."</li>";
}
$html .= "</ul><br>";
}
} else {
$html .= ")</li><p>".$pm->[1]."</p>";
}
$html .= "<p>This is a required parameter and must be passed as a REST parameter.</p>";
}
$html .= "</ul>";
$html .= "<h3>Return Attributes</h3><ul>";
foreach my $param (keys(%{$req->{attributes}})) {
my $att = $param;
my $att_type = $req->{attributes}->{$param}->[0];
my $att_desc = $req->{attributes}->{$param}->[1];
my $att_obj;
while (ref($att_desc) eq 'ARRAY') {
unless ($att_type =~ /object/) {
$att_type .= " of ".$att_desc->[0]."s";
} else {
$att_obj = $att_desc->[0];
}
$att_desc = $att_desc->[1];
}
$html .= "<li><b>$att</b> ($att_type)</li><p>";
if ($att_obj) {
$html .= "This attribute has an object structure:</p><ul style='list-style: none;'>";
foreach my $key (keys(%$att_obj)) {
my $obj_type = $att_obj->{$key}->[0];
my $obj_att = $att_obj->{$key}->[1];
while (ref($obj_att) eq 'ARRAY') {
$obj_type .= " of ".$obj_att->[0]."s";
$obj_att = $obj_att->[1];
}
$html .= "<li><b>$key ($obj_type)</b> - ".$obj_att."</li>";
}
$html .= "</ul><br>";
} else {
$html .= $att_desc."</p>";
}
print $res->{name}."\n";
$html .= "<h1><a name='".$res->{name}."' style='padding-top: 50px;'>".$res->{name}."</a></h1><h2>Description</h2><p>".$res->{description}."</p>";
foreach my $req (@{$res->{requests}}) {
print "\t".$req->{name}."\n";
$html .= "<h2>".$req->{name}." - request</h2><h3>Description</h3><p>".$req->{description}."</p><p>This is a ".$req->{type}." ".$req->{method}." request.";
unless (ref($req->{attributes})) {
$html .= " This request has no parameters.";
next;
}
$html .= "</p><h3>Parameters</h3><ul>";
# iterate over paramaters
foreach my $ptype (sort keys %param_types) {
foreach my $param (keys(%{$req->{parameters}->{$ptype}})) {
my $pm = $req->{parameters}->{$ptype}->{$param};
$pm->[0] =~ s/cv/controlled vocabulary/;
$html .= "<li><b>$param</b> (".$pm->[0];
if (ref($pm->[1]) eq 'ARRAY') {
if (! ref($pm->[0]) && $pm->[0] eq 'list') {
$html .= " of ".$pm->[1]->[0].")</li><p>";
$html .= $pm->[1]->[1]."</p>";
} else {
$html .= ")</li><p>";
$html .= "This parameter value can be chosen from the following (the first being default):</p><ul style='list-style: none;'>";
foreach my $cvitem (@{$pm->[1]}) {
$html .= "<li><b>".$cvitem->[0]."</b> - ".$cvitem->[1]."</li>";
}
$html .= "</ul><br>";
}
} else {
$html .= ")</p><p>".$pm->[1]."</p>";
}
$html .= $param_types{$ptype};
}
}
$html .= "</ul>";
$html .= "<h3>Return Attributes</h3><ul>";
# iterate over attributes
foreach my $param (keys(%{$req->{attributes}})) {
my ($att_obj, $att_hash);
my $att = $param;
my $att_type = $req->{attributes}->{$param}->[0];
my $att_desc = $req->{attributes}->{$param}->[1];
if ($att_type eq 'cv') {
$html .= "<li><b>$att</b> (controlled vocabulary)</li><p>";
$html .= "This parameter value can be chosen from the following (the first being default):</p><ul style='list-style: none;'>";
foreach my $cvitem (@$att_desc) {
$html .= "<li><b>".$cvitem->[0]."</b> - ".$cvitem->[1]."</li>";
}
$html .= "</ul><br>";
} else {
while (ref($att_desc) eq 'ARRAY') {
if ($att_type =~ /object/) {
$att_obj = $att_desc->[0];
} elsif (($att_type =~ /hash/) && ref($att_desc) eq 'ARRAY') {
$att_hash = $att_desc->[0];
} else {
$att_type .= " of ".$att_desc->[0]."s";
}
$att_desc = $att_desc->[1];
}
$html .= "<li><b>$att</b> ($att_type)</li><p>";
if ($att_obj) {
$html .= "This attribute has an object structure:</p><ul style='list-style: none;'>";
foreach my $key (keys(%$att_obj)) {
my $obj_type = $att_obj->{$key}->[0];
my $obj_att = $att_obj->{$key}->[1];
while (ref($obj_att) eq 'ARRAY') {
$obj_type .= " of ".$obj_att->[0]."s";
$obj_att = $obj_att->[1];
}
$html .= "<li><b>$key ($obj_type)</b> - ".$obj_att."</li>";
}
$html .= "</ul><br>";
} elsif ($att_hash) {
$html .= "This attribute has a hash structure:</p><ul style='list-style: none;'>";
foreach my $key (('key', 'value')) {
my $hash_type = $att_hash->{$key}->[0];
my $hash_att = $att_hash->{$key}->[1];
while (ref($hash_att) eq 'ARRAY') {
$hash_type .= " of ".$hash_att->[0]."s";
$hash_att = $hash_att->[1];
}
$html .= "<li><b>$key ($hash_type)</b> - ".$hash_att."</li>";
}
$html .= "</ul><br>";
} else {
$html .= $att_desc."</p>"
}
}
}
$html .= "</p></ul>";
}
$html .= "</p></ul>";
}
$html .= "<hr>";
$html .= "<hr>";
}

$html .= template_end();
Expand Down

0 comments on commit cf7b963

Please sign in to comment.