| 1 | package Serge::Engine::Plugin::parse_json_keyvalue; | ||||||||||||
| 2 | 1 | ▶ | use parent Serge::Engine::Plugin::Base::Parser; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use JSON -support_by_pp; # -support_by_pp is used to make Perl on Mac happy | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Serge::Mail; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Serge::Util qw(xml_escape_strref); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | sub name { | ||||||||||||
| 11 | 0 | ▶ | return 'JSON Dictionary Parser Plugin'; | ||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 12 | } | ||||||||||||
| 13 | |||||||||||||
| 14 | sub init { | ||||||||||||
| 15 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 16 | |||||||||||||
| 17 | 0 | $self->SUPER::init(@_); | |||||||||||
| 18 | |||||||||||||
| 19 | 0 | $self->{errors} = {}; | |||||||||||
| 20 | |||||||||||||
| 21 | 0 | $self->merge_schema({ | |||||||||||
| 22 | email_from => 'STRING', | ||||||||||||
| 23 | email_to => 'ARRAY', | ||||||||||||
| 24 | email_subject => 'STRING', | ||||||||||||
| 25 | }); | ||||||||||||
| 26 | |||||||||||||
| 27 | 0 | $self->add('after_job', \&report_errors); | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | sub validate_data { | ||||||||||||
| 31 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
validate_data: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 32 | |||||||||||||
| 33 | 0 | $self->SUPER::validate_data; | |||||||||||
| 34 | |||||||||||||
| 35 | 0 | ▶ | if (!defined $self->{data}->{email_from}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | print "WARNING: 'email_from' is not defined. Will skip sending any reports.\n"; | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | if (!defined $self->{data}->{email_to}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 0 | print "WARNING: 'email_to' is not defined. Will skip sending any reports.\n"; | |||||||||||
| 41 | } | ||||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | sub report_errors { | ||||||||||||
| 45 | 0 | ▶ | my ($self, $phase) = @_; | ||||||||||
|
Subroutine
report_errors: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 46 | |||||||||||||
| 47 | 0 | my $email_from = $self->{data}->{email_from}; | |||||||||||
| 48 | 0 | ▶ | if (!$email_from) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 0 | $self->{errors} = {}; | |||||||||||
| 50 | 0 | return; | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 0 | my $email_to = $self->{data}->{email_to}; | |||||||||||
| 54 | 0 | ▶ | if (!$email_to) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 0 | $self->{errors} = {}; | |||||||||||
| 56 | 0 | return; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 0 | ▶ | my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: JSON Parse Errors'); | ||||||||||
|
Condition0%0/2
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: JSON Parse Errors'
MC/DC0%0/1
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: JSON Parse Errors'
$self->{'data'}{'email_subject'}
Truth table0%0/2
A
A= $self->{'data'}{'email_subject'}
| |||||||||||||
| 60 | |||||||||||||
| 61 | 0 | my $text; | |||||||||||
| 62 | 0 | foreach my $key (sort keys %{$self->{errors}}) { | |||||||||||
| 63 | 0 | my $pre_contents = $self->{errors}->{$key}; | |||||||||||
| 64 | 0 | xml_escape_strref(\$pre_contents); | |||||||||||
| 65 | 0 | $text .= "<hr />\n<p><b style='color: red'>$key</b> <pre>".$pre_contents."</pre></p>\n"; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 0 | $self->{errors} = {}; | |||||||||||
| 69 | |||||||||||||
| 70 | 0 | ▶ | if ($text) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 0 | $text = qq| | |||||||||||
| 72 | <html> | ||||||||||||
| 73 | <head> | ||||||||||||
| 74 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||||||||||||
| 75 | </head> | ||||||||||||
| 76 | <body style="font-family: sans-serif; font-size: 120%"> | ||||||||||||
| 77 | |||||||||||||
| 78 | <p> | ||||||||||||
| 79 | # This is an automatically generated message. | ||||||||||||
| 80 | |||||||||||||
| 81 | The following parsing errors were found when attempting to localize JSON key-value file. | ||||||||||||
| 82 | </p> | ||||||||||||
| 83 | |||||||||||||
| 84 | $text | ||||||||||||
| 85 | |||||||||||||
| 86 | </body> | ||||||||||||
| 87 | </html> | ||||||||||||
| 88 | |; | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | Serge::Mail::send_html_message( | |||||||||||
| 91 | $email_from, # from | ||||||||||||
| 92 | $email_to, # to (list) | ||||||||||||
| 93 | $email_subject, # subject | ||||||||||||
| 94 | $text # message body | ||||||||||||
| 95 | ); | ||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | sub parse { | ||||||||||||
| 101 | 0 | ▶ | my ($self, $textref, $callbackref, $lang) = @_; | ||||||||||
|
Subroutine
parse: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 102 | |||||||||||||
| 103 | 0 | ▶ | die 'callbackref not specified' unless $callbackref; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | |||||||||||||
| 105 | # Finding translatable strings in file | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | my $messages = from_json($$textref); | |||||||||||
| 108 | |||||||||||||
| 109 | 0 | ▶ | if (!$messages) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | my $error = "from_json() returned empty data structure"; | |||||||||||
| 111 | 0 | $self->{errors}->{$self->{parent}->{engine}->{current_file_rel}} = $error; | |||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | print "******************\n$$textref\n******************\n" if $self->{parent}->{debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | |||||||||||||
| 115 | 0 | die $error; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | foreach my $key (sort keys %$messages) { | |||||||||||
| 119 | 0 | my $msg = $messages->{$key}; | |||||||||||
| 120 | 0 | $messages->{$key} = &$callbackref($msg, undef, $key, undef, $lang, $key); | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | # need to force indent_length, otherwise it will be zero when PP extension 'escape_slash' is used | ||||||||||||
| 124 | # (looks like an issue in some newer version of JSON:PP) | ||||||||||||
| 125 | # also, force 'canonical' to sort keys alphabetically to ensure the structure won't be changed on subsequent script runs | ||||||||||||
| 126 | 0 | return to_json($messages, {pretty => 1, indent_length => 3, canonical => 1, escape_slash => 1}); | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 1 | 1; | |||||||||||