| 1 | # ABSTRACT: Can turn an OpenAPI to App::Spec file | ||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | package API::CLI::App::Spec; | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.001_001'; # TRIAL VERSION | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use YAML::XS qw/ LoadFile /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use base 'App::Spec'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | has openapi => ( is => 'ro' ); | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 0 | ▶ | sub from_openapi { | ||||||||||||||||||
|
Subroutine
from_openapi: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 17 | 0 | my ($class, %args) = @_; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 0 | my $file = delete $args{file}; | |||||||||||||||||||
| 20 | 0 | my $openapi = LoadFile($file); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 0 | my $spec = $class->openapi2appspec( | |||||||||||||||||||
| 23 | openapi => $openapi, | ||||||||||||||||||||
| 24 | %args, | ||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | my $appspec = $class->read($spec); | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | return $appspec; | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | sub openapi2appspec { | ||||||||||||||||||
|
Subroutine
openapi2appspec: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||
| 33 | 0 | my ($class, %args) = @_; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | my $openapi = $args{openapi}; | |||||||||||||||||||
| 36 | 0 | my $name = $args{name}; | |||||||||||||||||||
| 37 | 0 | my $classname = $args{class}; | |||||||||||||||||||
| 38 | 0 | my $spec; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | my $paths = $openapi->{paths}; | |||||||||||||||||||
| 41 | 0 | my $subcommands = $spec->{subcommands} = {}; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | for my $path (sort keys %$paths) { | |||||||||||||||||||
| 44 | 0 | my $methods = $paths->{ $path }; | |||||||||||||||||||
| 45 | 0 | $path =~ s{(?:\{(\w+)\})}{:$1}g; | |||||||||||||||||||
| 46 | 0 | for my $method (sort keys %$methods) { | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | my $config = $methods->{ $method }; | |||||||||||||||||||
| 49 | 0 | $spec->{name} = $name; | |||||||||||||||||||
| 50 | 0 | $spec->{class} = $classname; | |||||||||||||||||||
| 51 | 0 | ▶ | $spec->{title} //= $openapi->{info}->{title}; | ||||||||||||||||||
|
Condition0%0/3
$spec->{"title"} //= $openapi->{"info"}{"title"}
MC/DC0%0/2
$spec->{"title"} //= $openapi->{"info"}{"title"}
$spec->{"title"}
$openapi->{"info"}{"title"}
Truth table0%0/3
A //= B
A= $spec->{"title"}B= $openapi->{"info"}{"title"}
| |||||||||||||||||||||
| 52 | 0 | $spec->{summary} = $openapi->{info}->{description}; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | my @parameters; | |||||||||||||||||||
| 55 | my @options; | ||||||||||||||||||||
| 56 | 0 | ▶ | if (my $params = $config->{parameters}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | for my $p (@$params) { | |||||||||||||||||||
| 58 | 0 | my $appspec_param = $class->param2appspec($p); | |||||||||||||||||||
| 59 | 0 | ▶ | if ($p->{in} eq 'path') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 60 | 0 | push @parameters, $appspec_param; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | elsif ($p->{in} eq 'query') { | ||||||||||||||||||||
| 63 | 0 | push @options, $appspec_param; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | ▶ | my $apicall = $subcommands->{ uc $method } ||= { | ||||||||||||||||||
|
Condition0%0/2
$$subcommands{uc $method} ||= {"op", "apicall", "summary", "\U$method\E call", "subcommands", {}}
MC/DC0%0/1
$$subcommands{uc $method} ||= {"op", "apicall", "summary", "\U$method\E call", "subcommands", {}}
$$subcommands{uc $method}
Truth table0%0/2
A
A= $$subcommands{uc $method}
| |||||||||||||||||||||
| 69 | op => 'apicall', | ||||||||||||||||||||
| 70 | summary => "\U$method\E call", | ||||||||||||||||||||
| 71 | subcommands => {}, | ||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||
| 73 | 0 | my $desc = $config->{description}; | |||||||||||||||||||
| 74 | 0 | $desc =~ s/\n.*//s; | |||||||||||||||||||
| 75 | 0 | ▶ | if (length $desc > 30) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | $desc = substr($desc, 0, 50) . '...'; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | 0 | ▶ | my $subcmd = $apicall->{subcommands}->{ $path } ||= { | ||||||||||||||||||
|
Condition0%0/2
$apicall->{"subcommands"}{$path} ||= {"summary", $desc, "parameters", \@parameters, "options", \@options}
MC/DC0%0/1
$apicall->{"subcommands"}{$path} ||= {"summary", $desc, "parameters", \@parameters, "options", \@options}
$apicall->{"subcommands"}{$path}
Truth table0%0/2
A
A= $apicall->{"subcommands"}{$path}
| |||||||||||||||||||||
| 79 | summary => $desc, | ||||||||||||||||||||
| 80 | parameters => \@parameters, | ||||||||||||||||||||
| 81 | options => \@options, | ||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | $spec->{openapi} = $openapi; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | my $options = $spec->{options} ||= []; | ||||||||||||||||||
|
Condition0%0/2
$spec->{"options"} ||= []
MC/DC0%0/1
$spec->{"options"} ||= []
$spec->{"options"}
Truth table0%0/2
A
A= $spec->{"options"}
| |||||||||||||||||||||
| 90 | 0 | push @$options, { | |||||||||||||||||||
| 91 | name => "data-file", | ||||||||||||||||||||
| 92 | type => "file", | ||||||||||||||||||||
| 93 | summary => "File with data for POST/PUT/PATCH/DELETE requests", | ||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||
| 95 | 0 | push @$options, { | |||||||||||||||||||
| 96 | name => "debug", | ||||||||||||||||||||
| 97 | type => "flag", | ||||||||||||||||||||
| 98 | summary => "debug", | ||||||||||||||||||||
| 99 | aliases => ['d'], | ||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||
| 101 | 0 | push @$options, { | |||||||||||||||||||
| 102 | name => "verbose", | ||||||||||||||||||||
| 103 | type => "flag", | ||||||||||||||||||||
| 104 | summary => "verbose", | ||||||||||||||||||||
| 105 | aliases => ['v'], | ||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||
| 107 | 0 | $spec->{apppec}->{version} = '0.001'; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | return $spec; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | ▶ | sub param2appspec { | ||||||||||||||||||
|
Subroutine
param2appspec: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 113 | 0 | my ($self, $p) = @_; | |||||||||||||||||||
| 114 | 0 | my $type = $p->{type}; | |||||||||||||||||||
| 115 | 0 | my $required = $p->{required}; | |||||||||||||||||||
| 116 | my $item = { | ||||||||||||||||||||
| 117 | name => $p->{name}, | ||||||||||||||||||||
| 118 | type => $type, | ||||||||||||||||||||
| 119 | required => $required, | ||||||||||||||||||||
| 120 | summary => $p->{description}, | ||||||||||||||||||||
| 121 | 0 | ▶ | $p->{enum} ? (enum => $p->{enum}) : (), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||
| 123 | 0 | ▶ | if ($p->{in} eq 'path') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | elsif ($p->{in} eq 'query') { | ||||||||||||||||||||
| 126 | 0 | $item->{name} = "q-" . $item->{name}; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | 0 | return $item; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 2 | 1; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||