File Coverage

blib/lib/Mojolicious/Plugin/XML/LX.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 17 19 89.4


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::XML::LX;
2 1     1   14863 use Mojo::Base 'Mojolicious::Plugin';
  1         7951  
  1         6  
3              
4 1     1   773 use 5.006;
  1         3  
5 1     1   3 use strict;
  1         5  
  1         14  
6 1     1   2 use warnings;
  1         2  
  1         39  
7              
8             our $VERSION = '0.02';
9              
10 1     1   216 use XML::LibXML;
  0            
  0            
11             use XML::Hash::LX qw(hash2xml);
12              
13             use Encode qw(decode_utf8);
14             use Mojo::Util qw(camelize xml_escape);
15              
16             sub register {
17             my ($self, $app, $conf) = @_;
18              
19             $conf ||= {};
20              
21             # Add XML type if not exists
22             $app->types->type(xml => 'application/xml')
23             unless $app->types->type('xml');
24              
25             # http://mojolicious.org/perldoc/Mojolicious/Guides/Rendering
26             # #Adding-a-handler-to-generate-binary-data
27              
28             # Add XML handler
29             $app->renderer->add_handler(xml => sub {
30             my ($renderer, $c, $output, $options) = @_;
31              
32             my %opts = %$conf;
33             $opts{encoding} = $options->{encoding} if $options->{encoding};
34              
35             my $data = delete $c->stash->{xml};
36             my $dom = hash2xml $data, doc => 1, %opts;
37             $$output = $dom->toString( 2 );
38             });
39              
40             # Automatic apply XML handler
41             $app->hook(before_render => sub {
42             my ($c, $args) = @_;
43             $args->{handler} = 'xml'
44             if exists $args->{xml} || exists $c->stash->{xml};
45             });
46             }
47              
48             =head1 NAME
49              
50             Mojolicious::Plugin::XML::LX - is a plugin
51             to support simple XML response from HASH.
52              
53              
54             =head1 SYNOPSIS
55              
56             $self->render(xml => {
57             response => {
58             status => 'ok',
59             message => 'hello world!',
60             }
61             });
62              
63             # You get:
64              
65            
66            
67             hello world!
68             ok
69            
70              
71              
72             =head1 DESCRIPTION
73              
74             L based on L
75             companion for L.
76              
77             All configuration parameters apply to L.
78              
79             =head1 AUTHOR
80              
81             Roman V. Nikolaev, C<< >>
82              
83             =head1 BUGS
84              
85             Please report any bugs or feature requests to C, or through
86             the web interface at L. I will be notified, and then you'll
87             automatically be notified of progress on your bug as I make changes.
88              
89              
90              
91              
92             =head1 SUPPORT
93              
94             You can find documentation for this module with the perldoc command.
95              
96             perldoc Mojolicious::Plugin::XML::LX
97              
98              
99             You can also look for information at:
100              
101             =over 4
102              
103             =item * RT: CPAN's request tracker (report bugs here)
104              
105             L
106              
107             =item * AnnoCPAN: Annotated CPAN documentation
108              
109             L
110              
111             =item * CPAN Ratings
112              
113             L
114              
115             =item * Search CPAN
116              
117             L
118              
119             =back
120              
121              
122             =head1 ACKNOWLEDGEMENTS
123              
124              
125             =head1 LICENSE AND COPYRIGHT
126              
127             Copyright 2016 Roman V. Nikolaev.
128              
129             This program is free software; you can redistribute it and/or modify it
130             under the terms of the the Artistic License (2.0). You may obtain a
131             copy of the full license at:
132              
133             L
134              
135             Any use, modification, and distribution of the Standard or Modified
136             Versions is governed by this Artistic License. By using, modifying or
137             distributing the Package, you accept this license. Do not use, modify,
138             or distribute the Package, if you do not accept this license.
139              
140             If your Modified Version has been derived from a Modified Version made
141             by someone other than you, you are nevertheless required to ensure that
142             your Modified Version complies with the requirements of this license.
143              
144             This license does not grant you the right to use any trademark, service
145             mark, tradename, or logo of the Copyright Holder.
146              
147             This license includes the non-exclusive, worldwide, free-of-charge
148             patent license to make, have made, use, offer to sell, sell, import and
149             otherwise transfer the Package with respect to any patent claims
150             licensable by the Copyright Holder that are necessarily infringed by the
151             Package. If you institute patent litigation (including a cross-claim or
152             counterclaim) against any party alleging that the Package constitutes
153             direct or contributory patent infringement, then this Artistic License
154             to you shall terminate on the date that such litigation is filed.
155              
156             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
157             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
158             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
159             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
160             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
161             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
162             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
163             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
164              
165              
166             =cut
167              
168             1; # End of Mojolicious::Plugin::XML::LX