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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#!/usr/bin/env perl
use strict;
use warnings;
use JSON;
use IO::All;
use Pod::Usage;
pod2usage(1) unless scalar(@ARGV) > 0;
my @specs;
foreach (@ARGV) {
my $content < io $_;
push @specs, JSON::decode_json($content);
}
print << 'DOT';
digraph {
node [shape=record];
edge [arrowhead = odot, arrowtail = none];
DOT
my $has_interface = scalar(@specs) > 1;
if ($has_interface) {
my $top = $specs[0]->{name};
print " \"", $top, "\"\n";
print " [label=\"{\\N}\"];\n\n";
foreach my $spec (@specs) {
my $name = $spec->{meta}->{module} || $spec->{name};
print " \"", $top, "\" -> \"", $name, "\"\n\n";
}
}
my $MAX_LEN = 90;
my %meth;
foreach my $spec (@specs) {
my $name = $spec->{meta}->{module} || $spec->{name};
print " \"", $name, "\"\n";
print " [label=\"{";
print "«interface»\\n" if ($has_interface);
print "\\N|";
for my $name (sort keys %{$spec->{methods}}) {
die "duplicated $name" if exists $meth{$name};
$meth{$name} = 1;
my $desc = $spec->{methods}->{$name};
print $name, "(";
my $len = length $name;
my $first = 1;
if ($desc->{required_payload}) {
print "payload";
$len += 7;
$first = 0;
}
for (@{$desc->{required_params}}) {
print ", " unless $first;
$len += 2;
if ($len > $MAX_LEN) {
print "\\l ";
$len = 4;
}
print $_;
$len += length $_;
$first = 0;
}
if ($desc->{optional_params}) {
print " " unless $first;
}
for (@{$desc->{optional_params}}) {
if ($len > $MAX_LEN) {
print "\\l ";
$len = 4;
}
print "\\[";
print ", " unless $first;
print $_, "\\]";
$len += 4 + length $_;
$first = 0;
}
if ($desc->{optional_payload}) {
if ($len > $MAX_LEN) {
print "\\l ";
$len = 4;
}
print "\\[";
print ", " unless $first;
print "payload\\]";
$len += 11;
$first = 0;
}
if ($desc->{unattended_params} || $spec->{unattended_params}) {
print ", " unless $first;
print "...";
}
print ")";
print " ⊗" if $desc->{authentication} || $spec->{authentication};
print " DEPRECATED" if $desc->{deprecated};
print "\\l";
if ($ENV{SPORE_DETAILS}) {
print " ", $desc->{method}, " ", $desc->{path}, "\\l";
for my $h (sort keys %{$desc->{headers}}) {
print " ", $h, ": ", $desc->{headers}->{$h}, "\\l";
}
for my $f (sort keys %{$desc->{'form-data'}}) {
print " form-data \\\"", $f, "\\\" ", $desc->{'form-data'}->{$f}, "\\l";
}
my $status = $desc->{expected_status} || $spec->{expected_status};
print " ", join(', ', @{$status}), "\\l" if $status;
}
}
print "}\"];\n\n";
my $note = $spec->{description};
if ($note && $ENV{SPORE_NOTES}) {
$note =~ s/\n/\\n/g;
print " \"__note__", $name, "\"\n";
print " [label=\"", $note, "\" shape=note];\n\n";
print " \"", $name, "\" -> \"__note__", $name, "\"\n";
print " [arrowhead = none, arrowtail = none, style = dashed];\n\n";
}
my $doc = $spec->{meta}->{documentation};
if ($doc && $ENV{SPORE_NOTES}) {
$doc =~ s/\n/\\n/g;
print " \"__doc__", $name, "\"\n";
print " [label=\"", $doc, "\" shape=note];\n\n";
print " \"", $name, "\" -> \"__doc__", $name, "\"\n";
print " [arrowhead = none, arrowtail = none, style = dashed];\n\n";
}
}
print "}\n";
__END__
=head1 NAME
spore2dot
=head1 SYNOPSIS
spore2dot.pl api1.json [api2.json, ...] > api.dot
=cut
|