File Coverage

blib/lib/Kelp/Module/YAML/KelpExtensions.pm
Criterion Covered Total %
statement 69 70 98.5
branch 11 14 78.5
condition 6 10 60.0
subroutine 21 22 95.4
pod 0 6 0.0
total 107 122 87.7


line stmt bran cond sub pod time code
1             package Kelp::Module::YAML::KelpExtensions;
2             $Kelp::Module::YAML::KelpExtensions::VERSION = '2.00';
3 1     1   9 use Kelp::Base -strict;
  1         2  
  1         8  
4              
5 1     1   221 use Kelp::Module::YAML;
  1         2  
  1         16  
6 1     1   643 use Kelp::Request;
  1         102175  
  1         55  
7 1     1   1867 use Kelp::Response;
  1         16135  
  1         55  
8 1     1   10 use Kelp::Test;
  1         3  
  1         10  
9              
10 1     1   36 use Try::Tiny;
  1         4  
  1         78  
11 1     1   7 use Test::More;
  1         3  
  1         57  
12 1     1   512 use Test::Deep;
  1         3  
  1         8  
13              
14             sub _replace
15             {
16 1     1   15 my ($name, $new) = @_;
17              
18 1     1   508 no strict 'refs';
  1         4  
  1         97  
19 1     1   8 no warnings 'redefine';
  1         2  
  1         1351  
20              
21 1         2 my $old = \&{$name};
  1         6  
22 1         7 *{$name} = sub {
23 2     2   159 unshift @_, $old;
24 2         8 goto $new;
25 1         20 };
26             }
27              
28             sub Kelp::Request::is_yaml
29             {
30 13     13 0 89504 my $self = shift;
31 13 50       53 return 0 unless $self->content_type;
32 13         145 return $self->content_type =~ m{$Kelp::Module::YAML::content_type_re};
33             }
34              
35             sub Kelp::Request::yaml_param
36             {
37 6     6 0 3482 my $self = shift;
38              
39 6   66     62 my $hash = $self->{_param_yaml_content} //= do {
40 3   100     16 my $hash = $self->yaml_content // {};
41 3 100       97 ref $hash eq 'HASH' ? $hash : {ref $hash, $hash};
42             };
43              
44 6 100       39 return $hash->{$_[0]} if @_;
45 3         20 return keys %$hash;
46             }
47              
48             sub Kelp::Request::yaml_content
49             {
50 8     8 0 806 my $self = shift;
51 8 100       27 return undef unless $self->is_yaml;
52              
53             return try {
54 6     6   371 my @documents = $self->app->get_encoder(yaml => 'internal')->decode($self->content);
55 5 100       20438 die if @documents > 1;
56 4         19 return $documents[0];
57             }
58             catch {
59 2     2   1280 undef;
60 6         147 };
61             }
62              
63             # Response
64              
65             sub Kelp::Response::yaml
66             {
67 2     2 0 3063 my $self = shift;
68 2   33     12 $self->set_content_type(
69             $Kelp::Module::YAML::content_type,
70             $self->charset || $self->app->charset
71             );
72              
73 2         288 return $self;
74             }
75              
76             _replace(
77             'Kelp::Response::_render_ref',
78             sub {
79             my ($super, $self, $body) = @_;
80              
81             if ($self->content_type =~ m{$Kelp::Module::YAML::content_type_re}) {
82             return $self->app->get_encoder(yaml => 'internal')->encode($body);
83             }
84             else {
85             return $super->($self, $body);
86             }
87             }
88             );
89              
90             # Test
91              
92             sub Kelp::Test::yaml_content
93             {
94 2     2 0 4 my $self = shift;
95 2         4 my $result;
96 2         11 my $decoder = $self->app->get_encoder(yaml => 'internal');
97             try {
98 2     2   122 $result = $decoder->decode(
99             $self->_decode($self->res->content)
100             );
101             }
102             catch {
103 0     0   0 fail("Poorly formatted YAML");
104 2         89 };
105 2         10029 return $result;
106             }
107              
108             sub Kelp::Test::yaml_cmp
109             {
110 2     2 0 10462 my ($self, $expected, $test_name) = @_;
111 2         6 local $Test::Builder::Level = $Test::Builder::Level + 1;
112              
113 2   50     17 $test_name ||= "YAML structure matches";
114 2 50       9 like $self->res->header('content-type'), qr/yaml/, 'Content-Type is YAML'
115             or return $self;
116 2         2099 my $json = $self->yaml_content;
117 2 50       16 cmp_deeply($json, $expected, $test_name) or diag explain $json;
118 2         22859 return $self;
119             }
120              
121             1;
122