File Coverage

blib/lib/Swagger2/Editor.pm
Criterion Covered Total %
statement 54 61 88.5
branch 9 18 50.0
condition 2 5 40.0
subroutine 10 10 100.0
pod 1 1 100.0
total 76 95 80.0


line stmt bran cond sub pod time code
1             package Swagger2::Editor;
2 1     1   497 use Mojo::Base 'Mojolicious';
  1         1  
  1         4  
3 1     1   57979 use Mojo::Util;
  1         2  
  1         35  
4 1     1   5 use File::Basename;
  1         1  
  1         56  
5 1     1   332 use Swagger2;
  1         2  
  1         6  
6              
7             has specification_file => sub { $ENV{SWAGGER_API_FILE} || '' };
8              
9             has _swagger => sub { Swagger2->new };
10              
11             sub _get {
12 1     1   7150 my $c = shift;
13              
14             $c->respond_to(
15             txt => {data => $c->app->_swagger->pod->to_string, layout => undef},
16             any => sub {
17 1     1   496 my $c = shift;
18 1 50       3 $c->stash(layout => undef) if $c->req->is_xhr;
19 1         22 $c->render(template => 'editor');
20             }
21 1         3 );
22             }
23              
24             sub _post {
25 2     2   41466 my $c = shift;
26 2   50     6 my $spec = $c->req->body || '{}';
27 2         41 my $file = $c->app->specification_file;
28              
29             eval {
30 2         11 my $s = Swagger2->new->parse($spec);
31 2 50 33     43 Mojo::Util::spurt($spec, $file) if $file and -w $file;
32 2         310 $c->render(text => $c->podify($s->pod), layout => undef);
33 2 50       14 } or do {
34 0         0 my $e = $@;
35 0         0 $c->app->log->error($e);
36 0         0 $e =~ s!^(Could not.*?:)\s+!$1\n\n!s;
37 0         0 $e =~ s!\s+at \S+\.pm line \d\S+!!g;
38 0         0 $c->render(template => 'error', error => $e);
39             };
40             }
41              
42             sub startup {
43 1     1 1 9354 my $self = shift;
44 1         1 my $raw = '';
45              
46 1 50       3 if (my $file = $self->specification_file) {
47 1 50       14 $raw
48             = $file =~ /^https?:/
49             ? $self->ua->get($file)->res->body
50             : Mojo::Util::slurp(File::Spec->catfile(split '/', $file));
51 1         84 $self->_swagger->parse($raw, $file);
52             }
53              
54 1         1 unshift @{$self->renderer->classes}, __PACKAGE__;
  1         4  
55 1         11 unshift @{$self->static->paths}, File::Spec->catdir(File::Basename::dirname(__FILE__), 'public');
  1         3  
56              
57 1         41 $self->routes->get('/' => \&_get);
58 1         175 $self->routes->post('/' => \&_post);
59 1         81 $self->defaults(raw => $raw, swagger => $self->_swagger, layout => 'default');
60 1         25 $self->plugin('PODRenderer');
61 1         14191 $self->helper(podify => \&_podify);
62             }
63              
64             sub _podify {
65 3     3   83 my ($c, $pod) = @_;
66 3         10 my $dom = Mojo::DOM->new($c->pod_to_html($pod->to_string));
67 3         56508 my $ul = '
    ';
68 3         4 my ($sub, @parts);
69              
70 3         12 for my $e ($dom->find('h1, h2')->each) {
71 30         16155 my $id = $e->{id};
72 30         355 my $text = $e->all_text;
73 30     30   657 my $anchor = $c->tag(a => href => "#$id", sub {$text});
  30         805  
74              
75 30 50       1118 if ($e->type eq 'h1') {
76 0 0       0 $ul .= '' if $sub;
77 0         0 $sub = 0;
78             }
79             else {
80 30 100       269 $ul .= '
    ' unless $sub;
81 30         29 $sub = 1;
82             }
83              
84 30         62 $ul .= "
  • $anchor
  • ";
    85              
    86 30         153 $e->content($c->link_to($text => Mojo::URL->new->fragment('toc'), id => $id));
    87             }
    88              
    89 3 50       1166 $ul .= '' if $ul;
    90              
    91 3         13 return $c->b(qq(

    TABLE OF CONTENTS

    $ul$dom
    ));
    92             }
    93              
    94             $ENV{SWAGGER_LOAD_EDITOR} ? __PACKAGE__->new : 1;
    95              
    96             __DATA__