File Coverage

blib/lib/Swagger2/Markdown/API/Blueprint.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 18 19 94.7


line stmt bran cond sub pod time code
1             package Swagger2::Markdown::API::Blueprint;
2              
3 2     2   10 use strict;
  2         4  
  2         56  
4 2     2   9 use warnings;
  2         2  
  2         53  
5              
6 2     2   1194 use Template::Stash;
  2         24185  
  2         1033  
7              
8             $Template::Stash::LIST_OPS->{ request_headers } = sub {
9             my $list = shift;
10             my $headers = [ grep { $_->{in} eq 'header' } @$list ];
11             return @{ $headers } ? $headers : undef;
12             };
13              
14             $Template::Stash::LIST_OPS->{ path_and_query_params } = sub {
15             my $list = shift;
16             my $params = [ grep { $_->{in} =~ /path|query/ } @$list ];
17             return @{ $params } ? $params : undef;
18             };
19              
20             $Template::Stash::LIST_OPS->{ query_params } = sub {
21             my $list = shift;
22             my $params = [ map { $_->{name} } grep { $_->{in} =~ /query/ } @$list ];
23             return @{ $params } ? $params : undef;
24             };
25              
26             $Template::Stash::LIST_OPS->{ none_path_and_query_params } = sub {
27             my $list = shift;
28             my $params = [ grep { $_->{in} !~ /path|query/ } @$list ];
29             return @{ $params } ? $params : undef;
30             };
31              
32             $Template::Stash::HASH_OPS->{ sort_methods_by_group } = sub {
33             my $hash = shift;
34              
35             return sort {
36             ( $hash->{$a}{"x-api-blueprint"}{group} // '' )
37             cmp ( $hash->{$b}{"x-api-blueprint"}{group} // '' )
38             || $a cmp $b
39             } keys %{ $hash };
40             };
41              
42             sub template {
43 10     10 0 12 my ( $args ) = @_;
44              
45 10         19 my $template = _blueprint();
46 10         46 return \$template;
47             }
48              
49             1;
50              
51             # vim: ft=tt2;ts=4:sw=4:et
52              
53             sub _blueprint {
54              
55 10     10   19 return << 'EndOfBlueprint';
56             [%-
57             USE Dumper;
58             -%]
59              
60             [%- BLOCK resource_section -%]
61             [%- IF group; prefix = '##'; ELSE; prefix = '#'; END -%]
62             [%-
63             IF query_params;
64             qpath = path _ '{?' _ query_params.join(',') _ '}';
65             ELSE;
66             qpath = path;
67             END;
68             -%]
69             [%- SWITCH o.resource_section -%]
70             [%- CASE 'uri' -%]
71             [% prefix %] [% qpath _ "\n" -%]
72             [%- CASE 'name_uri' -%]
73             [% prefix %] [% summary %] [[% e.basePath _ qpath -%]][% "\n" -%]
74             [%- CASE 'method_uri' -%]
75             [% prefix %] [% method | upper %] [% e.basePath _ qpath -%][% "\n" -%]
76             [%- CASE 'name_method_uri' -%]
77             [% prefix %] [% summary %] [[% method | upper %] [% e.basePath _ qpath -%]][% "\n" -%]
78             [%- CASE -%]
79             [% IF method.defined %][% prefix %] [% method | upper %] [% e.basePath _ qpath %][% "\n" %][% END -%]
80             [%- END -%]
81             [%- END -%]
82              
83             [%- BLOCK action_section -%]
84             [%- IF group; prefix = '###'; ELSE; prefix = '##'; END -%]
85             [%- SWITCH o.action_section -%]
86             [%- CASE 'method' -%]
87             [% prefix %] [% method | upper; "\n" -%]
88             [%- CASE 'name_method' -%]
89             [% prefix %] [% summary %] [[% method | upper -%]][% "\n" -%]
90             [%- CASE 'name_method_uri' -%]
91             [% prefix %] [% summary %] [[% method | upper %] [% e.basePath _ path %]][% "\n" -%]
92             [%- CASE -%]
93             [% IF method.defined %][% prefix %] [% method | upper %] [% e.basePath _ path %][% "\n" %][% END -%]
94             [%- END -%]
95             [%- END -%]
96              
97             [%-
98             BLOCK definition_type;
99             IF o.data_structures;
100             IF schema.type == 'array';
101             'array';
102             FOREACH item IN ref.items;
103             '[' _ item.value.split( "/" ).-1 _ ']';
104             END;
105             ELSIF schema.type == 'object';
106             ref_key = '$ref';
107             IF ref.$ref_key;
108             definition = ref.$ref_key;
109             definition.split( "/" ).-1;
110             ELSE;
111             schema.type;
112             END;
113             ELSE;
114             FOREACH t IN schema.type;
115             t.defined ? t : 'null';
116             LAST;
117             END;
118             END;
119             ELSE;
120             schema.type;
121             END;
122             END;
123             -%]
124              
125             [%-
126             BLOCK definition;
127             example = "x-example";
128             h = schema.properties;
129             FOREACH property IN h.keys.sort;
130             "$indent+ $property";
131             IF h.$property.$example.defined;
132             IF h.$property.type == 'boolean';
133             h.$property.$example ? ": true" : ": false";
134             ELSE;
135             ": `${h.$property.$example}`";
136             END;
137             END;
138             ref_key = '$ref';
139              
140             " (";
141             INCLUDE definition_type
142             schema = h.$property
143             ref = h.$property;
144             ")";
145              
146             IF h.$property.description;
147             IF h.$property.description.match( '^\n' );
148             "\n\n$indent ";
149             h.$property.description.remove( '^\n' );
150             ELSE;
151             " - ${h.$property.description}";
152             END;
153             END;
154             "\n";
155             # recursion
156             IF h.$property.type == 'object';
157             INCLUDE definition
158             schema = h.$property
159             indent = "$indent "
160             ;
161             END;
162             END;
163             END;
164             -%]
165              
166             [%-
167             BLOCK parameters;
168             FOREACH param IN params;
169             IF loop.first;
170             "\n+ Parameters\n\n";
171             END;
172             example = 'x-example';
173             " + ${param.name}";
174             IF param.$example.defined;
175             IF param.type == 'boolean';
176             param.$example ? ": true" : ": false";
177             ELSE;
178             ": `${param.$example}`";
179             END;
180             END;
181             " (${param.type}";
182             IF NOT param.required; ', optional'; END;
183             ")";
184             IF param.description.match( '^\n' );
185             "\n\n ";
186             param.description.remove( '^\n' );
187             "\n";
188             ELSE;
189             " - ${param.description}\n";
190             END;
191             IF param.default.defined;
192             " + Default: ";
193             IF param.type == 'boolean';
194             param.default ? "true" : "false";
195             ELSE;
196             "`${param.default}`";
197             END;
198             "\n";
199             END;
200             END;
201             END;
202             -%]
203              
204             [%- BLOCK response_section -%]
205             [%- FOREACH response IN e.paths.$path.$method.responses.keys.sort -%]
206             [%- "\n+ Response " _ response -%]
207             [%- IF e.paths.$path.$method.produces -%]
208             [%- %] ([% e.paths.$path.$method.produces.0 %])
209             [%- ELSIF e.produces -%]
210             [%- %] ([% e.produces.0 %])
211             [%- END %]
212             [%- IF
213             o.attributes
214             AND e.paths.$path.$method.responses.$response.schema
215             -%]
216             [%-
217             "\n\n + Attributes (";
218              
219             INCLUDE definition_type
220             schema = e.paths.$path.$method.responses.$response.schema
221             ref = c.paths.$path.$method.responses.$response.schema
222             ;
223              
224             ")\n";
225              
226             IF e.paths.$path.$method.responses.$response.schema.type == 'object';
227             IF o.data_structures;
228             INCLUDE definition
229             schema = c.paths.$path.$method.responses.$response.schema
230             indent = " ";
231             ;
232             "\n";
233             ELSE;
234             INCLUDE definition
235             schema = e.paths.$path.$method.responses.$response.schema
236             indent = " ";
237             ;
238             "\n";
239             END;
240             END;
241             -%]
242             [%- END -%]
243             [%- IF e.paths.$path.$method.responses.$response.headers -%]
244             [%- "\n\n + Headers\n" -%]
245             [%- FOREACH header IN e.paths.$path.$method.responses.$response.headers.keys.sort -%]
246             [%- "\n " _ header %]: [% e.paths.$path.$method.responses.$response.headers.$header.type; -%]
247             [%- END -%]
248             [%- body = "\n\n + Body" -%]
249             [%- body_padding = ' ' -%]
250             [%- ELSIF o.attributes -%]
251             [%- body = " + Body" -%]
252             [%- body_padding = ' ' -%]
253             [%- ELSE -%]
254             [%- body = '' -%]
255             [%- body_padding = ' ' -%]
256             [%- END -%]
257             [%- IF e.paths.$path.$method.responses.$response.schema.example -%]
258             [%-
259             body;
260             "\n\n";
261             # indent correctly
262             e.paths.$path.$method.responses.$response.schema.example.replace(
263             "(?m)^([ ])*",body_padding _ ' $1'
264             );
265             "\n\n"
266             -%]
267             [%- ELSE -%]
268             [%- "\n" -%]
269             [%- END -%]
270             [%- END -%]
271             [%- END -%]
272              
273             [%- BLOCK request_section -%]
274             [%- IF e.paths.$path.$method.parameters.none_path_and_query_params -%]
275             [%- "\n+ Request " -%]
276             [%- IF e.paths.$path.$method.consumes -%]
277             [%- %]([% e.paths.$path.$method.consumes.0 %])
278             [%- IF e.paths.$path.$method.parameters.request_headers -%]
279             [%- "\n\n + Headers" -%]
280             [%- FOREACH header IN e.paths.$path.$method.parameters.request_headers -%]
281             [%- "\n\n" %] [% header.name %]: [% header.type %][% "\n" -%]
282             [%- END -%]
283             [%- END -%]
284             [%- END %]
285             [%- FOREACH param IN e.paths.$path.$method.parameters -%]
286             [%- IF param.schema -%]
287             [%- IF param.schema.example -%]
288             [%- "\n\n " -%]
289             [%- param.schema.example; "\n" -%]
290             [%- LAST -%]
291             [%- ELSE -%]
292             [%- "\n" -%]
293             [%- END -%]
294             [%- END -%]
295             [%- END -%]
296             [%- END -%]
297             [%- END -%]
298              
299             [%- BLOCK method_section -%]
300             [%- FOREACH method IN e.paths.$path.sort_methods_by_group -%]
301             [%- IF method == api_blueprint; NEXT; END -%]
302             [%- summary = e.paths.$path.$method.summary -%]
303             [%- IF o.simple -%]
304             [%- PROCESS resource_section
305             query_params = e.paths.$path.$method.parameters.query_params
306             -%]
307             [%- ELSE -%]
308             [%- PROCESS action_section -%]
309             [%- END -%]
310             [%- IF e.paths.$path.$method.description.defined -%]
311             [%- e.paths.$path.$method.description -%]
312             [%- END -%]
313             [%- PROCESS parameters
314             params = e.paths.$path.$method.parameters.path_and_query_params
315             -%]
316             [%- PROCESS request_section -%]
317             [%- PROCESS response_section -%]
318             [%- END -%]
319             [%- END -%]
320             FORMAT: 1A
321              
322             # [% e.info.title %]
323             [% e.info.description -%]
324              
325             [% FOREACH path IN e.paths.keys.sort -%]
326             [%- api_blueprint = 'x-api-blueprint' -%]
327             [%- IF e.paths.$path.$api_blueprint.defined -%]
328             [%- summary = e.paths.$path.$api_blueprint.summary -%]
329             [%- group = e.paths.$path.$api_blueprint.group -%]
330             [%- IF group -%]
331             [%- "\n" IF NOT loop.first -%]
332             [%- IF group != e.paths.${ loop.prev }.$api_blueprint.group -%]
333             [%- "# Group " _ group _ "\n" -%]
334             [%- e.paths.$path.$api_blueprint.description -%]
335             [%- IF e.paths.$path.keys.size == 1; NEXT; ELSE; "\n"; END -%]
336             [%- END -%]
337             [%- END -%]
338             [%- END -%]
339             [%- PROCESS resource_section
340             query_params = e.paths.$path.get.parameters.query_params
341             -%]
342             [%- IF e.paths.$path.$api_blueprint.defined -%]
343             [%- IF group -%]
344             [%- e.paths.$path.$api_blueprint.group_description _ "\n" -%]
345             [%- ELSE -%]
346             [%- e.paths.$path.$api_blueprint.description _ "\n" -%]
347             [%- END -%]
348             [%- END -%]
349             [%- PROCESS method_section -%]
350             [%- END -%]
351             [%- IF o.data_structures -%]
352             [%- "# Data Structures\n\n" -%]
353             [%- FOREACH definition IN c.definitions.keys.sort -%]
354             [%-
355             "## " _ definition;
356             " (";
357             INCLUDE definition_type
358             schema = c.definitions.$definition
359             ;
360             ")";
361             "\n"
362             -%]
363             [%-
364             INCLUDE definition
365             schema = c.definitions.$definition
366             indent = "";
367             ;
368             "\n";
369             -%]
370             [%- END -%]
371             [%- END -%]
372             [%-# Dumper.dump( o ) -%]
373             [%-# Dumper.dump( e ) -%]
374             [%-# Dumper.dump( c ) -%]
375             [%-# Dumper.dump( d ) -%]
376             EndOfBlueprint
377              
378             }