| 1 | package Map::Tube::Plugin::Formatter; | ||
| 2 | |||
| 3 | $Map::Tube::Plugin::Formatter::VERSION = '0.15'; | ||
| 4 | $Map::Tube::Plugin::Formatter::AUTHORITY = 'cpan:MANWAR'; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Map::Tube::Plugin::Formatter - Formatter plugin for Map::Tube. | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.15 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use YAML; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use JSON qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use Map::Tube::Plugin::Formatter::Utils qw(xml get_data validate_object); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | |||
| 21 | use Moo::Role; | ||
| 22 | use namespace::autoclean; | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | A very simple add-on for L<Map::Tube> to format the supported objects. | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use strict; use warnings; | ||
| 31 | use Map::Tube::London; | ||
| 32 | |||
| 33 | my $map = Map::Tube::London->new; | ||
| 34 | |||
| 35 | my $node = $map->get_node_by_name('Baker Street'); | ||
| 36 | print $map->to_xml($node) , "\n\n"; | ||
| 37 | print $map->to_json($node), "\n\n"; | ||
| 38 | print $map->to_yaml($node), "\n\n"; | ||
| 39 | print $map->to_string($node), "\n\n"; | ||
| 40 | |||
| 41 | my $line = $map->get_line_by_name('Metropolitan'); | ||
| 42 | print $map->to_xml($line) , "\n\n"; | ||
| 43 | print $map->to_json($line), "\n\n"; | ||
| 44 | print $map->to_yaml($line), "\n\n"; | ||
| 45 | print $map->to_string($line), "\n\n"; | ||
| 46 | |||
| 47 | my $route = $map->get_shortest_route('Baker Street', 'Wembley Park'); | ||
| 48 | print $map->to_xml($route), "\n\n"; | ||
| 49 | print $map->to_json($route), "\n\n"; | ||
| 50 | print $map->to_yaml($route), "\n\n"; | ||
| 51 | print $map->to_string($route),"\n\n"; | ||
| 52 | |||
| 53 | =head1 SUPPORTED FORMATS | ||
| 54 | |||
| 55 | It currently supports the following formats. | ||
| 56 | |||
| 57 | =over 4 | ||
| 58 | |||
| 59 | =item * XML | ||
| 60 | |||
| 61 | =item * JSON | ||
| 62 | |||
| 63 | =item * YAML | ||
| 64 | |||
| 65 | =item * STRING | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =head1 SUPPORTED OBJECTS | ||
| 70 | |||
| 71 | It currently supports the following objects. | ||
| 72 | |||
| 73 | =over 4 | ||
| 74 | |||
| 75 | =item * L<Map::Tube::Node> | ||
| 76 | |||
| 77 | =item * L<Map::Tube::Line> | ||
| 78 | |||
| 79 | =item * L<Map::Tube::Route> | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =head1 METHODS | ||
| 84 | |||
| 85 | =head2 to_xml($object) | ||
| 86 | |||
| 87 | It takes an object (supported) and returns XML representation of the same. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub to_xml { | ||
| 92 | my ($self, $object) = @_; | ||
| 93 | |||
| 94 | validate_object($object); | ||
| 95 | |||
| 96 | my $data = {}; | ||
| 97 | if (ref($object) eq 'Map::Tube::Node') { | ||
| 98 | $data = { | ||
| 99 | node => { | ||
| 100 | attributes => { | ||
| 101 | id => $object->id, | ||
| 102 | name => $object->name, | ||
| 103 | }, | ||
| 104 | children => { | ||
| 105 | link => [ map {{ id => $_, name => $self->get_node_by_id($_)->name }} (split /\,/,$object->link) ], | ||
| 106 | line => [ map {{ id => $_->id, name => $_->name }} (@{$object->line}) ], | ||
| 107 | }, | ||
| 108 | }, | ||
| 109 | }; | ||
| 110 | } | ||
| 111 | elsif (ref($object) eq 'Map::Tube::Line') { | ||
| 112 | $data = { | ||
| 113 | line => { | ||
| 114 | attributes => { | ||
| 115 | id => $object->id, | ||
| 116 | name => $object->name, | ||
| 117 | color => $object->color || 'undef', | ||
| 118 | }, | ||
| 119 | children => { | ||
| 120 | station => [ map {{ id => $_->id, name => $_->name }} (@{$object->get_stations}) ], | ||
| 121 | }, | ||
| 122 | }, | ||
| 123 | }; | ||
| 124 | } | ||
| 125 | elsif (ref($object) eq 'Map::Tube::Route') { | ||
| 126 | my $children = {}; | ||
| 127 | my $nodes = $object->nodes; | ||
| 128 | my $size = $#$nodes; | ||
| 129 | foreach my $i (1..($size-1)) { | ||
| 130 | push @{$children->{node}}, { name => $nodes->[$i]->as_string, order => $i }; | ||
| 131 | } | ||
| 132 | |||
| 133 | $data = { | ||
| 134 | route => { | ||
| 135 | attributes => { | ||
| 136 | from => $object->from->as_string, | ||
| 137 | to => $object->to->as_string, | ||
| 138 | }, | ||
| 139 | children => $children, | ||
| 140 | }, | ||
| 141 | }; | ||
| 142 | } | ||
| 143 | |||
| 144 | return xml($data); | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | =head2 to_json($object) | ||
| 149 | |||
| 150 | It takes an object (supported) and returns JSON representation of the same. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub to_json { | ||
| 155 | my ($self, $object) = @_; | ||
| 156 | |||
| 157 | return JSON->new->utf8(1)->pretty->encode(get_data($self, $object)); | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 to_yaml($object) | ||
| 161 | |||
| 162 | It takes an object (supported) and returns YAML representation of the same. | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub to_yaml { | ||
| 167 | my ($self, $object) = @_; | ||
| 168 | |||
| 169 | return Dump(get_data($self, $object)); | ||
| 170 | } | ||
| 171 | |||
| 172 | =head2 to_string($object) | ||
| 173 | |||
| 174 | It takes an object (supported) and returns STRING representation of the same. | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub to_string { | ||
| 179 | my ($self, $object) = @_; | ||
| 180 | |||
| 181 | validate_object($object); | ||
| 182 | |||
| 183 | return $object->as_string; | ||
| 184 | } | ||
| 185 | |||
| 186 | =head1 AUTHOR | ||
| 187 | |||
| 188 | Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||
| 189 | |||
| 190 | =head1 REPOSITORY | ||
| 191 | |||
| 192 | L<https://github.com/manwar/Map-Tube-Plugin-Formatter> | ||
| 193 | |||
| 194 | =head1 SEE ALSO | ||
| 195 | |||
| 196 | =over 4 | ||
| 197 | |||
| 198 | =item * L<Map::Tube> | ||
| 199 | |||
| 200 | =back | ||
| 201 | |||
| 202 | =head1 BUGS | ||
| 203 | |||
| 204 | Please report any bugs or feature requests to C<bug-map-tube-plugin-formatter at rt.cpan.org>, | ||
| 205 | or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Map-Tube-Plugin-Formatter>. | ||
| 206 | I will be notified and then you'll automatically be notified of progress on your | ||
| 207 | bug as I make changes. | ||
| 208 | |||
| 209 | =head1 SUPPORT | ||
| 210 | |||
| 211 | You can find documentation for this module with the perldoc command. | ||
| 212 | |||
| 213 | perldoc Map::Tube::Plugin::Formatter | ||
| 214 | |||
| 215 | You can also look for information at: | ||
| 216 | |||
| 217 | =over 4 | ||
| 218 | |||
| 219 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 220 | |||
| 221 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Map-Tube-Plugin-Formatter> | ||
| 222 | |||
| 223 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 224 | |||
| 225 | L<http://annocpan.org/dist/Map-Tube-Plugin-Formatter> | ||
| 226 | |||
| 227 | =item * CPAN Ratings | ||
| 228 | |||
| 229 | L<http://cpanratings.perl.org/d/Map-Tube-Plugin-Formatter> | ||
| 230 | |||
| 231 | =item * Search CPAN | ||
| 232 | |||
| 233 | L<http://search.cpan.org/dist/Map-Tube-Plugin-Formatter/> | ||
| 234 | |||
| 235 | =back | ||
| 236 | |||
| 237 | =head1 LICENSE AND COPYRIGHT | ||
| 238 | |||
| 239 | Copyright (C) 2015 - 2016 Mohammad S Anwar. | ||
| 240 | |||
| 241 | This program is free software; you can redistribute it and / or modify it under | ||
| 242 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||
| 243 | license at: | ||
| 244 | |||
| 245 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 246 | |||
| 247 | Any use, modification, and distribution of the Standard or Modified Versions is | ||
| 248 | governed by this Artistic License.By using, modifying or distributing the Package, | ||
| 249 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||
| 250 | not accept this license. | ||
| 251 | |||
| 252 | If your Modified Version has been derived from a Modified Version made by someone | ||
| 253 | other than you,you are nevertheless required to ensure that your Modified Version | ||
| 254 | complies with the requirements of this license. | ||
| 255 | |||
| 256 | This license does not grant you the right to use any trademark, service mark, | ||
| 257 | tradename, or logo of the Copyright Holder. | ||
| 258 | |||
| 259 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||
| 260 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||
| 261 | Package with respect to any patent claims licensable by the Copyright Holder that | ||
| 262 | are necessarily infringed by the Package. If you institute patent litigation | ||
| 263 | (including a cross-claim or counterclaim) against any party alleging that the | ||
| 264 | Package constitutes direct or contributory patent infringement,then this Artistic | ||
| 265 | License to you shall terminate on the date that such litigation is filed. | ||
| 266 | |||
| 267 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||
| 268 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||
| 269 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||
| 270 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||
| 271 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||
| 272 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||
| 273 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | 1; # End of Map::Tube::Plugin::Formatter | ||