| 1 | package Yeb::Plugin::JSON; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | $Yeb::Plugin::JSON::AUTHORITY = 'cpan:GETTY'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Yeb Plugin for JSON response | ||||||||||||||||||||||||||||
| 6 | 1 | $Yeb::Plugin::JSON::VERSION = '0.101'; | |||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | has app => ( is => 'ro', required => 1, weak_ref => 1 ); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | my @json_attrs_untouched = qw( | |||||||||||||||||||||||||||
| 13 | ascii latin1 binary pretty indent space_before space_after | ||||||||||||||||||||||||||||
| 14 | relaxed canonical filter_json_object filter_json_single_key_object | ||||||||||||||||||||||||||||
| 15 | shrink max_depth max_size | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | for (@json_attrs_untouched) { | |||||||||||||||||||||||||||
| 19 | 14 | has $_ => ( | |||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | predicate => 'has_'.$_, | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my @json_attrs_enabled = qw( | |||||||||||||||||||||||||||
| 26 | utf8 allow_nonref convert_blessed allow_blessed | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | for (@json_attrs_enabled) { | |||||||||||||||||||||||||||
| 30 | has $_ => ( | ||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 4 | ); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my @json_attrs = ( @json_attrs_untouched, @json_attrs_enabled ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | has json => ( | ||||||||||||||||||||||||||||
| 39 | is => 'ro', | ||||||||||||||||||||||||||||
| 40 | lazy => 1, | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 42 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 43 | 0 | my $json = JSON::MaybeXS->new; | |||||||||||||||||||||||||||
| 44 | 0 | for my $attr (@json_attrs) { | |||||||||||||||||||||||||||
| 45 | 0 | my $has_attr = 'has_'.$attr; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | if (!$self->can($has_attr) || $self->$has_attr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->can($has_attr) or $self->$has_attr
MC/DC0%0/2
not $self->can($has_attr) or $self->$has_attr
not $self->can($has_attr)
$self->$has_attr
Truth table0%0/3
not(A) or B
A= not $self->can($has_attr)B= $self->$has_attr
| |||||||||||||||||||||||||||||
| 47 | 0 | $json->$attr($self->$attr); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | return $json; | |||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | 1 | ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | has json_class => ( | ||||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||
| 56 | lazy => 1, | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | default => sub { (ref $_[0]->json) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 1 | ); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | has true => ( | ||||||||||||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||||||||||
| 62 | lazy => 1, | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | default => sub { $_[0]->json_class->true }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 1 | ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | has false => ( | ||||||||||||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||||||||||
| 68 | lazy => 1, | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | default => sub { $_[0]->json_class->false }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 1 | ); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub get_vars { | ||||||||||||||||||||||||||
|
Subroutine
get_vars: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 0 | my ( $self, $user_vars ) = @_; | |||||||||||||||||||||||||||
| 74 | 0 | my %stash = %{$self->app->cc->stash}; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | my %user = defined $user_vars ? %{$user_vars} : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | return $self->app->merge_hashs( | |||||||||||||||||||||||||||
| 77 | $self->app->cc->export, | ||||||||||||||||||||||||||||
| 78 | \%user | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | $self->app->register_function('json',sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | 0 | my $user_vars = shift; | |||||||||||||||||||||||||||
| 86 | 0 | my $vars = $self->get_vars($user_vars); | |||||||||||||||||||||||||||
| 87 | 0 | $self->app->cc->content_type('application/json'); | |||||||||||||||||||||||||||
| 88 | 0 | $self->app->cc->body($self->json->encode($vars)); | |||||||||||||||||||||||||||
| 89 | 0 | $self->app->cc->response; | |||||||||||||||||||||||||||
| 90 | 0 | }); | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | $self->app->register_function('true',sub { $self->true }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $self->app->register_function('false',sub { $self->false }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | 1; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||