blib/lib/APISchema/Generator/Markdown/Formatter.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 177 CC 22 SCAR 30.1
CC
22
Cov
0%
type
72.0
restriction
20.0
_code
12.0
CRAP 20.4
1 package APISchema::Generator::Markdown::Formatter;
2 use 5.014;
3  
4 # core
5 use Exporter qw(import);
6 our @EXPORT = qw(type json pretty_json code restriction desc anchor method methods content_type http_status http_status_code);
7  
8 # cpan
9 use HTTP::Status qw(status_message);
10 use URI::Escape qw(uri_escape_utf8);
11 use JSON::XS ();
12 my $JSON = JSON::XS->new->canonical(1);
13  
14 use constant +{
15 RESTRICTIONS => [qw(required max_items min_items max_length min_length maximum minimum pattern)],
16 SHORT_DESCRIPTION_LENGTH => 100,
17 };
18  
19 sub type ($) {
20 my $def = shift;
21 my $bar = '|';
22  
23 my $ref = ref $def ? $def->{'$ref'} : $def;
24 if ($ref) {
25 $ref = $ref =~ s!^#/resource/!!r;
26 my $ref_text = "`$ref`";
27 my $name = $ref =~ s!/.*$!!r;
28 $ref_text = sprintf('[%s](#%s)', $ref_text, anchor(resource => $name)) if $name;
29 return $ref_text;
30 }
31  
32 my $type = $def->{type};
33 if ($type) {
34 return sprintf '`%s`', $type unless ref $type;
35 return join $bar, map { code($_) } @{$type} if ref $type eq 'ARRAY';
36 }
37  
38 return join $bar, map { code($_) } @{$def->{enum}} if $def->{enum};
39  
40 return 'undefined';
41 }
42  
43 sub json ($) {
44 my $x = shift;
45 if (ref $x) {
46 $x = $JSON->encode($x);
47 } else {
48 $x = $JSON->encode([$x]);
49 $x =~ s/^\[(.*)\]$/$1/;
50 }
51 return $x;
52 }
53  
54 my $PRETTY_JSON = JSON::XS->new->canonical(1)->indent(1)->pretty(1);
55 sub pretty_json ($) {
56 my $x = shift;
57 if (ref $x) {
58 $x = $PRETTY_JSON->encode($x);
59 } else {
60 $x = $PRETTY_JSON->encode([$x]);
61 $x =~ s/^\[\s*(.*)\s*\]\n$/$1/;
62 }
63 return $x;
64 }
65  
66 sub _code ($) {
67 my $text = shift;
68 return '' unless defined $text;
69 if ($text =~ /[`|]/) {
70 $text =~ s/[|]/|/g;
71 return sprintf '<code>%s</code>', $text;
72 }
73 return sprintf '`%s`', $text;
74 }
75  
76 sub code ($) {
77 my $text = shift;
78 return '' unless defined $text;
79 return _code json $text;
80 }
81  
82 sub anchor ($$) {
83 my ($label, $obj) = @_;
84 my $name = ref $obj ? $obj->title : $obj;
85 return sprintf '%s-%s', $label, uri_escape_utf8($name);
86 }
87  
88 sub restriction ($) {
89 my $def = shift;
90 return '' unless (ref $def) eq 'HASH';
91  
92 my @result = ();
93 for my $r (sort @{+RESTRICTIONS}) {
94 next unless defined $def->{$r};
95  
96 if (ref $def->{$r}) {
97 push @result, _code sprintf "$r%s", json $def->{$r};
98 } else {
99 push @result, _code sprintf "$r(%s)", json $def->{$r};
100 }
101 }
102 return join ' ', @result;
103 }
104  
105 sub desc ($) {
106 my $text = shift || '';
107 $text = $text =~ s/[\r\n].*\z//sr;
108 $text = substr($text, 0, SHORT_DESCRIPTION_LENGTH) . '...'
109 if length($text) > SHORT_DESCRIPTION_LENGTH;
110 return $text;
111 }
112  
113 sub method ($) {
114 my $method = shift;
115 return $method->[0] if (ref $method || '') eq 'ARRAY';
116 return $method;
117 }
118  
119 sub methods ($) {
120 my $method = shift;
121 return join ', ', map { _code($_) } @$method
122 if (ref $method || '') eq 'ARRAY';
123 return _code($method);
124 }
125  
126 sub content_type ($) {
127 my $type = shift;
128 return '-' unless length($type);
129 return "`$type`";
130 }
131  
132 sub http_status ($) {
133 my $code = shift;
134 return undef unless $code;
135 return join(' ', $code, status_message($code));
136 }
137  
138 sub http_status_code {
139 return _code http_status shift;
140 }
141  
142 1;