File Coverage

blib/lib/OpenAPI/Render/HTMLForms.pm
Criterion Covered Total %
statement 39 39 100.0
branch 17 20 85.0
condition 13 15 86.6
subroutine 10 10 100.0
pod 0 6 0.0
total 79 90 87.7


line stmt bran cond sub pod time code
1             package OpenAPI::Render::HTMLForms;
2              
3 1     1   13484 use strict;
  1         2  
  1         29  
4 1     1   5 use warnings;
  1         1  
  1         49  
5              
6             our $VERSION = '0.1.0'; # VERSION
7              
8 1     1   848 use CGI qw(-nosticky -utf8 h1 h2 h3 p input filefield popup_menu legend submit start_div end_div start_fieldset end_fieldset start_form end_form start_html end_html);
  1         32796  
  1         6  
9              
10 1     1   1209 use parent qw(OpenAPI::Render);
  1         282  
  1         5  
11              
12             sub header
13             {
14 1     1 0 3 my( $self ) = @_;
15             return start_html( -title => $self->{api}{info}{title} . ' v' .
16             $self->{api}{info}{version},
17 1         37 -script =>
18             '
19              
20             function replace_url_parameters( form ) {
21             var url = form.getAttribute( "action" );
22             var inputs = form.getElementsByTagName( "input" );
23             for( var i = 0; i < inputs.length; i++ ) {
24             var data_in_path = inputs[i].getAttribute( "data-in-path" );
25             if( data_in_path ) {
26             url = url.replace( "{" + inputs[i].name + "}", inputs[i].value );
27             inputs[i].disabled = "disabled";
28             }
29             }
30             form.setAttribute( "action", url );
31             }
32              
33             ' );
34             }
35              
36             sub footer
37             {
38 1     1 0 5 return end_html;
39             }
40              
41             sub path_header
42             {
43 3     3 0 8 my( $self, $path ) = @_;
44 3         10 return h1( $path );
45             }
46              
47             sub operation_header
48             {
49 10     10 0 21 my( $self, $path, $operation ) = @_;
50             return start_form( -action => $self->{base_url} . $path,
51             -method => $operation ) .
52             start_fieldset .
53             legend( uc( $operation ) .
54             ( $self->{api}{paths}{$path}{$operation}{description}
55 10 50       38 ? ': ' . $self->{api}{paths}{$path}{$operation}{description} : '' ) );
56             }
57              
58             sub operation_footer
59             {
60 10     10 0 22 my( $self, $path, $operation ) = @_;
61              
62 10         14 my %submit_options;
63 10 100 100     41 if( $operation eq 'get' || $operation eq 'post' ) {
64 5         11 $submit_options{-onclick} = 'replace_url_parameters( this.form )';
65             } else {
66             $submit_options{-name} =
67 5         24 sprintf 'Submit Query (cannot be handled for %s)',
68             uc $operation;
69 5         11 $submit_options{-disabled} = 'disabled';
70             }
71              
72 10         31 return submit( %submit_options ) . end_fieldset . end_form;
73             }
74              
75             sub parameter
76             {
77 74     74 0 133 my( $self, $parameter ) = @_;
78 74         106 my @parameter;
79 74 100       184 return @parameter if $parameter->{_is_pattern};
80              
81             push @parameter,
82             h3( $parameter->{name} ),
83 68 100       149 $parameter->{description} ? p( $parameter->{description} ) : ();
84 68 100 100     3543 if( $parameter->{schema} && $parameter->{schema}{enum} ) {
    100 100        
      66        
      66        
85 6         10 my @values = @{$parameter->{schema}{enum}};
  6         17  
86 6 50       27 if( !$parameter->{required} ) {
87 6         14 unshift @values, '';
88             }
89             push @parameter,
90             popup_menu( -name => $parameter->{name},
91             -values => \@values,
92 6 50       27 ($parameter->{in} eq 'path'
93             ? ( '-data-in-path' => 1 ) : ()) );
94             } elsif( ($parameter->{schema}{type} &&
95             $parameter->{schema}{type} eq 'object') ||
96             ($parameter->{schema}{format} &&
97             $parameter->{schema}{format} eq 'binary') ) {
98             push @parameter,
99 36         95 filefield( -name => $parameter->{name} );
100             } else {
101             push @parameter,
102             input( { -type => 'text',
103             -name => $parameter->{name},
104             ($parameter->{in} eq 'path'
105             ? ( '-data-in-path' => 1 ) : ()),
106             (exists $parameter->{example}
107             ? ( -placeholder => $parameter->{example} )
108 26 100       146 : ()) } );
    100          
109             }
110 68         15613 return @parameter;
111             }
112              
113             1;