| 1 | package Plack::App::APISchema::MockServer; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use parent qw(Plack::Component); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Plack::Util::Accessor qw(schema); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Plack::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Encode qw(encode_utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use APISchema::JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use APISchema::Generator::Router::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use APISchema::Generator::Markdown::ResourceResolver; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use APISchema::Generator::Markdown::ExampleFormatter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 7 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 16 | 7 | my ($self, $env) = @_; | |||||||||
| 17 | |||||||||||
| 18 | 7 | my $req = Plack::Request->new($env); | |||||||||
| 19 | |||||||||||
| 20 | 7 | my ($matched, $router_simple_route) = $self->router->routematch($env); | |||||||||
| 21 | |||||||||||
| 22 | 7 | ▶ | unless ($matched) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 1 | return [404, ['Content-Type' => 'text/plain; charset=utf-8'], ['not found']]; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 6 | my $root = $self->schema->get_resource_root; | |||||||||
| 27 | |||||||||||
| 28 | 6 | my $route = $self->schema->get_route_by_name($router_simple_route->name); | |||||||||
| 29 | |||||||||||
| 30 | 6 | my $default_code = $route->default_responsible_code; | |||||||||
| 31 | 6 | my $response_resource = $route->canonical_response_resource($root, [ | |||||||||
| 32 | $default_code | ||||||||||
| 33 | ]); | ||||||||||
| 34 | |||||||||||
| 35 | 6 | my $resolver = APISchema::Generator::Markdown::ResourceResolver->new(schema => $root); | |||||||||
| 36 | |||||||||||
| 37 | 6 | my $formatter = APISchema::Generator::Markdown::ExampleFormatter->new( | |||||||||
| 38 | resolver => $resolver, | ||||||||||
| 39 | spec => $response_resource, | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | # TODO: serve all headers defined in example | ||||||||||
| 43 | # TODO: format body with encoding | ||||||||||
| 44 | 6 | return [$default_code, ['Content-Type' => 'application/json; charset=utf-8'], [encode_utf8($formatter->body)]]; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 8 | ▶ | sub router { | ||||||||
|
Subroutine
router: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 48 | 8 | my ($self) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 8 | ▶ | return $self->{router} if $self->{router}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 7 | my $generator = APISchema::Generator::Router::Simple->new; | |||||||||
| 53 | 7 | $self->{router} = $generator->generate_router($self->schema); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 1 | 1; | |||||||||
| 57 | __END__ | ||||||||||