| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::Notifications::JSON; | 
| 2 | 5 |  |  | 5 |  | 5404 | use Mojo::Base 'Mojolicious::Plugin::Notifications::Engine'; | 
|  | 5 |  |  |  |  | 19 |  | 
|  | 5 |  |  |  |  | 48 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | # TODO: | 
| 5 |  |  |  |  |  |  | #   Probably introduce a notify_json function | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | has key => 'notifications'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | # Nothing to register | 
| 10 |  |  |  |  |  |  | sub register { | 
| 11 | 5 |  |  | 5 | 1 | 17 | my ($plugin, $app, $param) = @_; | 
| 12 | 5 | 50 |  |  |  | 30 | $plugin->key($param->{key}) if $param->{key}; | 
| 13 |  |  |  |  |  |  | }; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Notification method | 
| 17 |  |  |  |  |  |  | sub notifications { | 
| 18 | 15 |  |  | 15 | 1 | 50 | my ($self, $c, $notify_array, $rule, $json, %param) = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 15 |  | 33 |  |  | 92 | my $key = $param{key} // $self->key; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 15 | 100 |  |  |  | 163 | return $json unless @$notify_array; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 13 | 50 | 66 |  |  | 64 | if (!$json || ref $json) { | 
| 25 | 13 |  |  |  |  | 27 | my @msgs; | 
| 26 | 13 |  |  |  |  | 38 | foreach (@$notify_array) { | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # Confirmation message | 
| 29 | 22 | 100 | 100 |  |  | 146 | if (ref $_->[1] && ref $_->[1] eq 'HASH' && | 
|  |  |  | 33 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 30 |  |  |  |  |  |  | ($_->[1]->{ok} || $_->[1]->{cancel} || $_[1]->{confirm})) { | 
| 31 | 4 |  |  |  |  | 54 | my $param = $_->[1]; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 4 |  |  |  |  | 14 | my $opt = {}; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # Set confirmation path | 
| 36 | 4 | 100 |  |  |  | 14 | if ($param->{confirm}) { | 
| 37 |  |  |  |  |  |  | $opt->{$param->{confirm_label} // 'confirm'} = { | 
| 38 |  |  |  |  |  |  | method => 'GET', | 
| 39 |  |  |  |  |  |  | url => $param->{confirm} | 
| 40 | 1 |  | 50 |  |  | 13 | }; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | else { | 
| 44 |  |  |  |  |  |  | # Set confirmation path | 
| 45 | 3 | 100 |  |  |  | 8 | if ($param->{ok}) { | 
| 46 |  |  |  |  |  |  | $opt->{$param->{ok_label} // 'ok'} = { | 
| 47 |  |  |  |  |  |  | method => 'POST', | 
| 48 |  |  |  |  |  |  | url => $param->{ok} | 
| 49 | 2 |  | 100 |  |  | 23 | }; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | # Set cancelation path | 
| 53 | 3 | 50 |  |  |  | 9 | if ($param->{cancel}) { | 
| 54 |  |  |  |  |  |  | $opt->{$param->{cancel_label} // 'cancel'} = { | 
| 55 |  |  |  |  |  |  | method => 'POST', | 
| 56 |  |  |  |  |  |  | url => $param->{cancel} | 
| 57 | 3 |  | 100 |  |  | 31 | }; | 
| 58 |  |  |  |  |  |  | }; | 
| 59 |  |  |  |  |  |  | }; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 4 |  |  |  |  | 24 | push @msgs, [$_->[0], ''.$_->[-1], $opt]; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # Normal notification | 
| 65 |  |  |  |  |  |  | else { | 
| 66 | 18 |  |  |  |  | 243 | push(@msgs, [$_->[0], ''.$_->[-1]]); | 
| 67 |  |  |  |  |  |  | }; | 
| 68 |  |  |  |  |  |  | }; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 13 | 100 |  |  |  | 87 | if (!$json) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 71 | 2 |  |  |  |  | 62 | return { $key => \@msgs } | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # Obect is an array | 
| 75 |  |  |  |  |  |  | elsif (index(ref $json, 'ARRAY') >= 0) { | 
| 76 | 1 |  |  |  |  | 5 | push(@$json, { $key => \@msgs }); | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # Object is a hash | 
| 80 |  |  |  |  |  |  | elsif (index(ref $json, 'HASH') >= 0) { | 
| 81 | 10 |  | 50 |  |  | 54 | my $n = ($json->{$key} //= []); | 
| 82 | 10 |  |  |  |  | 34 | push(@$n, @msgs); | 
| 83 |  |  |  |  |  |  | }; | 
| 84 |  |  |  |  |  |  | }; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 11 |  |  |  |  | 114 | return $json; | 
| 87 |  |  |  |  |  |  | }; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | 1; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | __END__ |