File Coverage

blib/lib/Test/JSON/RPC/Autodoc.pm
Criterion Covered Total %
statement 42 42 100.0
branch 5 6 83.3
condition 7 8 87.5
subroutine 12 12 100.0
pod 3 5 60.0
total 69 73 94.5


line stmt bran cond sub pod time code
1             package Test::JSON::RPC::Autodoc;
2 3     3   87497 use 5.008001;
  3         6  
3 3     3   8 use strict;
  3         3  
  3         44  
4 3     3   9 use warnings;
  3         9  
  3         63  
5 3     3   1216 use File::ShareDir;
  3         12840  
  3         117  
6 3     3   2012 use Path::Tiny qw/path/;
  3         24765  
  3         144  
7 3     3   1399 use Text::Xslate;
  3         20625  
  3         112  
8 3     3   1075 use Test::JSON::RPC::Autodoc::Request;
  3         6  
  3         761  
9              
10             our $VERSION = "0.15";
11              
12             sub new {
13 3     3 1 322 my ($class, %opt) = @_;
14 3 100       16 my $app = $opt{app} or die 'app parameter must not be null!';
15             my $self = bless {
16             app => $app,
17             document_root => $opt{document_root} || 'docs',
18             path => $opt{path} || '/',
19             requests => [],
20             index_file => $opt{index_file} || undef,
21 2   50     24 }, $class;
      100        
      100        
22 2         6 return $self;
23             }
24              
25             sub new_request {
26 4     4 1 2782 my ($self, $label) = @_;
27             my $req = Test::JSON::RPC::Autodoc::Request->new(
28             app => $self->{app},
29             path => $self->{path},
30 4   100     48 label => $label || '',
31             );
32 4         4 push @{$self->{requests}}, $req;
  4         9  
33 4         7 return $req;
34             }
35              
36             sub write {
37 2     2 1 1132 my ($self, $filename) = @_;
38 2         4 my $tx = $self->load_tx();
39 2         425 my $text = $tx->render('template.tx', { requests => $self->{requests} });
40 2         9 path($self->{document_root}, $filename)->spew_utf8($text);
41 2 100       2320 $self->append_to_index($filename) if $self->{index_file};
42             }
43              
44             sub append_to_index {
45 1     1 0 2 my ($self, $filename) = @_;
46 1         3 my $tx = $self->load_tx();
47             my $text = $tx->render('index_part.tx', {
48             requests => $self->{requests},
49 1         155 path => path($filename),
50             });
51 1         5 my $path = path($self->{document_root}, $self->{index_file});
52 1         18 $path->append_utf8($text);
53             }
54              
55             sub load_tx {
56 3     3 0 4 my $dir = './share';
57 3 50       40 $dir = File::ShareDir::dist_dir('Test-JSON-RPC-Autodoc') unless -d $dir;
58 3         25 return Text::Xslate->new( path => $dir );
59             }
60              
61             1;
62             __END__