r1397@llin: dpavlin | 2007-10-31 01:38:40 +0100
[webpac2] / lib / WebPAC / Output / JSON.pm
1 package WebPAC::Output::JSON;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7 __PACKAGE__->mk_accessors(qw(path));
8
9 use Data::Dump qw/dump/;
10 use JSON;
11 use File::Slurp;
12
13 =head1 NAME
14
15 WebPAC::Output::JSON - Create JSON output
16
17 =head1 VERSION
18
19 Version 0.00
20
21 =cut
22
23 our $VERSION = '0.00';
24
25 =head1 SYNOPSIS
26
27 Create JSON output for export into other sistems
28
29 =head1 FUNCTIONS
30
31 =head2 new
32
33  my $out = new WebPAC::Output::JSON({
34         path => '/path/to/file.js',
35  });
36
37 Options are:
38
39 =over 4
40
41 =item path
42
43 path to JSON file
44
45 =back
46
47 =head2 init
48
49   $out->init;
50
51 =cut
52
53 sub init {
54         my $self = shift;
55         my $log = $self->_get_logger;
56
57         $log->debug('init');
58
59         $self->{_data} = [];
60
61         return 1;
62 }
63
64
65 =head2 add
66
67 Adds one entry to database.
68
69   $out->add( 42, $ds );
70
71 =cut
72
73 sub add {
74         my $self = shift;
75
76         my ( $id, $ds ) = @_;
77
78         my $log = $self->_get_logger;
79         $log->logdie("need id") unless defined $id;
80         $log->logdie("need ds") unless $ds;
81
82         $log->debug("id: $id ds = ",sub { dump($ds) });
83
84         push @{ $self->{_data} }, $self->ds_to_hash( $ds, 'display' );
85
86         return 1;
87 }
88
89 =head2 finish
90
91  $out->finish;
92
93 =cut
94
95 sub finish {
96         my $self = shift;
97
98         my $log = $self->_get_logger();
99
100         $log->info("writing JSON output to ", $self->path);
101         write_file( $self->path, objToJson( { items => $self->{_data} } ) );
102
103 }
104
105 =head1 AUTHOR
106
107 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
108
109 =head1 COPYRIGHT & LICENSE
110
111 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
112
113 This program is free software; you can redistribute it and/or modify it
114 under the same terms as Perl itself.
115
116 =cut
117
118 1; # End of WebPAC::Output::JSON