| 1 | package OpenAPI::Render::reStructuredText; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.2.0'; # VERSION | ||
| 7 | |||
| 8 | use Text::ASCIITable; | ||
| 9 | |||
| 10 | use parent qw(OpenAPI::Render); | ||
| 11 | |||
| 12 | sub header | ||
| 13 | { | ||
| 14 | my( $self ) = @_; | ||
| 15 | return _h( $self->{api}{info}{title} . ' v' . | ||
| 16 | $self->{api}{info}{version}, | ||
| 17 | '=' ); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub path_header | ||
| 21 | { | ||
| 22 | my( $self, $path ) = @_; | ||
| 23 | |||
| 24 | my $text = _h( $path, '-' ); | ||
| 25 | my @url_parameters; | ||
| 26 | if( $self->{api}{paths}{$path}{parameters} ) { | ||
| 27 | @url_parameters = grep { $_->{in} eq 'path' } | ||
| 28 | @{$self->{api}{paths}{$path}{parameters}}; | ||
| 29 | } | ||
| 30 | if( @url_parameters ) { | ||
| 31 | $text .= _h( 'URL parameters', '+' ); | ||
| 32 | $self->_start_table( 'Name', 'Description', 'Example' ); | ||
| 33 | foreach (@url_parameters) { | ||
| 34 | $self->{table}->addRow( $_->{name}, | ||
| 35 | $_->{description}, | ||
| 36 | $_->{example} ); | ||
| 37 | } | ||
| 38 | $text .= $self->parameters_footer; | ||
| 39 | } | ||
| 40 | |||
| 41 | return $text; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub operation_header | ||
| 45 | { | ||
| 46 | my( $self, $path, $operation ) = @_; | ||
| 47 | return _h( uc( $operation ) . | ||
| 48 | ( $self->{api}{paths}{$path}{$operation}{description} | ||
| 49 | ? ': ' . $self->{api}{paths}{$path}{$operation}{description} : '' ), | ||
| 50 | '+' ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub parameters_header | ||
| 54 | { | ||
| 55 | my( $self ) = @_; | ||
| 56 | $self->_start_table( 'Name', 'Description', 'Mandatory?', 'Format', 'Example' ); | ||
| 57 | return _h( 'Parameters', '~' ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub parameter | ||
| 61 | { | ||
| 62 | my( $self, $parameter ) = @_; | ||
| 63 | return '' if $parameter->{in} eq 'path'; # should be already handled | ||
| 64 | |||
| 65 | my $table = $self->{table}; | ||
| 66 | $table->addRow( $parameter->{name}, | ||
| 67 | $parameter->{description}, | ||
| 68 | $parameter->{required} ? 'yes' : 'no', | ||
| 69 | $parameter->{schema}{type}, | ||
| 70 | $parameter->{example} ); | ||
| 71 | return ''; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub parameters_footer | ||
| 75 | { | ||
| 76 | my( $self ) = @_; | ||
| 77 | return $self->{table}->draw( [ '+', '+', '-', '+' ], | ||
| 78 | [ '|', '|', '|' ], | ||
| 79 | [ '+', '+', '=', '+' ], | ||
| 80 | [ '|', '|', '|' ], | ||
| 81 | [ '+', '+', '-', '+' ] ) . "\n"; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub responses_header | ||
| 85 | { | ||
| 86 | my( $self ) = @_; | ||
| 87 | $self->_start_table( 'HTTP code', 'Description' ); | ||
| 88 | return _h( 'Responses', '~' ); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub response | ||
| 92 | { | ||
| 93 | my( $self, $code, $response ) = @_; | ||
| 94 | my $table = $self->{table}; | ||
| 95 | $table->addRow( $code, $response->{description} ); | ||
| 96 | return ''; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub responses_footer { ¶meters_footer } | ||
| 100 | |||
| 101 | sub _h | ||
| 102 | { | ||
| 103 | my( $text, $symbol ) = @_; | ||
| 104 | $symbol = '-' unless $symbol; | ||
| 105 | return $text . "\n" . ( $symbol x length $text ) . "\n\n"; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _start_table | ||
| 109 | { | ||
| 110 | my( $self, @columns ) = @_; | ||
| 111 | $self->{table} = Text::ASCIITable->new; | ||
| 112 | $self->{table}->setOptions( 'drawRowLine', 1 ); | ||
| 113 | $self->{table}->setCols( @columns ); | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |