ee10d0b6adb97652d6934564199c26ba8e52fe21
[webpac2] / lib / WebPAC / Output / CouchDB.pm
1 package WebPAC::Output::CouchDB;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7 __PACKAGE__->mk_accessors(qw(
8         database
9
10         host
11         port
12 ));
13
14 use Data::Dump qw/dump/;
15 use Net::CouchDb;
16
17 =head1 NAME
18
19 WebPAC::Output::CouchDB - feed data into CouchDB
20
21 =head1 FUNCTIONS
22
23 =head2 init
24
25   $out->init;
26
27 =cut
28
29 sub init {
30         my $self = shift;
31         my $log = $self->_get_logger;
32
33         $log->debug('init');
34
35         my $cdb = Net::CouchDb->new(
36                 host => $self->host || "localhost",
37                 port => $self->port || 5984,
38         );
39
40         $log->info("CouchDB server info ", dump( $cdb->server_info ) );
41
42         my $database = $self->database || 'webpac2';
43
44         $cdb->debug( $self->debug );
45         eval { $cdb->delete_db( $self->database ) };
46         $cdb->create_db( $self->database );
47         $self->{_cdb} = $cdb->db( $self->database );
48
49         return 1;
50 }
51
52
53 =head2 add
54
55 Adds one entry to database.
56
57   $out->add( 42, $ds );
58
59 =cut
60
61 sub add {
62         my $self = shift;
63
64         my ( $id, $ds ) = @_;
65
66         my $log = $self->_get_logger;
67
68         my $doc = Net::CouchDb::Document->new( $id, $ds );
69         $self->{_cdb}->put( $doc );
70
71         return 1;
72 }
73
74 =head2 finish
75
76  $out->finish;
77
78 =cut
79
80 sub finish {
81         my $self = shift;
82
83         my $log = $self->_get_logger();
84
85         $log->info('finish');
86         1;
87
88 }
89
90 =head1 AUTHOR
91
92 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
93
94 =head1 COPYRIGHT & LICENSE
95
96 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
97
98 This program is free software; you can redistribute it and/or modify it
99 under the same terms as Perl itself.
100
101 =cut
102
103 1; # End of WebPAC::Output::CouchDB