File Coverage

blib/lib/OpenAPI/Generator/From/Definitions.pm
Criterion Covered Total %
statement 49 53 92.4
branch 13 22 59.0
condition 1 3 33.3
subroutine 11 11 100.0
pod 2 2 100.0
total 76 91 83.5


line stmt bran cond sub pod time code
1             package OpenAPI::Generator::From::Definitions;
2              
3 2     2   1075 use strict;
  2         5  
  2         66  
4 2     2   11 use warnings;
  2         4  
  2         68  
5              
6 2     2   11 use Carp qw(croak);
  2         4  
  2         176  
7 2     2   15 use File::Find;
  2         3  
  2         124  
8 2     2   454 use OpenAPI::Generator::Util qw(merge_definitions);
  2         6  
  2         327  
9              
10             BEGIN {
11              
12 2 50   2   8 if (eval { require YAML::XS }) {
  2 50       333  
13 0         0 YAML::XS->import('Load');
14             }
15 2         300 elsif (eval { require YAML }) {
16 0         0 YAML->import('Load');
17             }
18             else {
19 2         575 require CPAN::Meta::YAML;
20 2         5817 CPAN::Meta::YAML->import('Load');
21             }
22              
23 2 50       10 if (eval { require JSON::XS }) {
  2         368  
24 0         0 JSON::XS->import('decode_json')
25             }
26             else {
27 2         1555 require JSON::PP;
28 2         31119 JSON::PP->import('decode_json');
29             }
30              
31             }
32              
33             sub new {
34              
35 1     1 1 6 bless {}, shift
36             }
37              
38             sub generate {
39              
40 1     1 1 4 my($self, $conf) = @_;
41              
42 1         3 my $src = $conf->{src};
43 1         6 $self->_check_src($src);
44 1         5 my @files = $self->_src_as_files($src);
45              
46 1 50       8 if (!@files) {
47 0         0 croak "no files found in $src";
48             }
49              
50 1         3 my @defs = @{ $conf->{definitions} };
  1         5  
51              
52 1         3 for my $file (@files) {
53 3 50       3784 croak "$file is not readable" unless -r $file;
54              
55 3         25 local $/ = '';
56 3 50       226 open my $fh, '<', $file or croak "can't open file $file";
57 3         81 my $content = <$fh>;
58 3         34 close $fh;
59              
60              
61 3 100       22 if ($file =~ /\.json$/) {
62 1         6 push @defs, decode_json($content);
63             }
64             else {
65 2         14 push @defs, Load($content);
66             }
67             }
68              
69 1         693 merge_definitions(@defs)
70             }
71              
72             sub _check_src {
73              
74 1 50 33 1   59 croak "$_[1] is not file or directory" unless(-f $_[1] or -d $_[1]);
75 1 50       20 croak "$_[1] is not readable" unless(-r $_[1]);
76             }
77              
78             sub _src_as_files {
79              
80 1 50   1   14 return $_[1] if -f $_[1];
81              
82 1         4 my @files;
83 1 100   4   131 find sub { push @files, $File::Find::name if /\.(yml|yaml|json)$/ }, $_[1];
  4         186  
84             @files
85 1         8 }
86              
87             1
88              
89             __END__