File Coverage

blib/lib/Data/CouchDB.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Data::CouchDB;
2              
3 1     1   12440 use 5.006;
  1         3  
  1         36  
4 1     1   10 use strict;
  1         2  
  1         63  
5 1     1   5 use warnings;
  1         4  
  1         42  
6              
7             =head1 NAME
8              
9             Data::CouchDB - CouchDB document management
10              
11             =head1 VERSION
12              
13             Version 0.06
14              
15             =cut
16              
17             our $VERSION = '0.06';
18              
19             =head1 NAME
20              
21             Data::CouchDB
22              
23             =head1 SYNOPSYS
24              
25             my $couchdb = Data::CouchDB->new(
26             replica_host => 'localhost',
27             replica_port => 5432,
28             master_host => 'localhost',
29             master_port => 5432,
30             couch => 'testdb',
31             );
32              
33             =head1 DESCRIPTION
34              
35             This class represents couchdb as a datasource.
36              
37             =head1 ATTRIBUTES
38              
39             =cut
40              
41 1     1   138 use Moose;
  0            
  0            
42             use MooseX::StrictConstructor;
43             use namespace::autoclean;
44              
45             use Cache::RedisDB;
46             use Data::CouchDB::Connection;
47             use Try::Tiny;
48             use LWP::UserAgent;
49              
50             has name => (
51             is => 'ro',
52             isa => 'Str'
53             );
54              
55             =head2 db
56              
57             db with which to operate.
58              
59             =cut
60              
61             has db => (
62             is => 'ro',
63             isa => 'Str',
64             default => 'default',
65             );
66              
67             =head2 couchdb
68              
69             password for "couchdb" user
70              
71             =cut
72              
73             has couchdb => (
74             is => 'ro',
75             default => 'TESTPASS',
76             );
77              
78             =head2 replica_host
79              
80             name of the host to read from.
81              
82             =cut
83              
84             has 'replica_host' => (
85             is => 'ro',
86             default => 'localhost',
87             );
88              
89             =head2 replica_port
90              
91             port number in replica_host through which we can read.
92              
93             =cut
94              
95             has replica_port => (
96             is => 'ro',
97             default => '5984',
98             );
99              
100             =head2 replica_protocol
101              
102             protcol used to read.
103              
104             =cut
105              
106             has replica_protocol => (
107             is => 'ro',
108             isa => 'Str',
109             );
110              
111             =head2 master_host
112              
113             name of the host to write to
114              
115             =cut
116              
117             has 'master_host' => (
118             is => 'ro',
119             default => 'localhost',
120             );
121              
122             =head2 master_port
123              
124             port number in master_host through which we can write.
125              
126             =cut
127              
128             has master_port => (
129             is => 'ro',
130             default => '5984',
131             );
132              
133             =head2 master_protocol
134              
135             protocol used to write.
136              
137             =cut
138              
139             has master_protocol => (
140             is => 'ro',
141             isa => 'Str',
142             );
143              
144             =head2 replica
145              
146             The internal ds used to read from couchdb
147              
148             =cut
149              
150             has replica => (
151             is => 'ro',
152             isa => 'Data::CouchDB::Connection',
153             lazy_build => 1,
154             );
155              
156             =head2 master
157              
158             The internal ds used to write to couchdb
159              
160             =cut
161              
162             has master => (
163             is => 'ro',
164             isa => 'Data::CouchDB::Connection',
165             lazy_build => 1,
166             );
167              
168             =head2 ua
169              
170             Optionally passed ua(user_agent). If not passed the couchdb's default ua is used.
171              
172             =cut
173              
174             has ua => (
175             is => 'ro',
176             isa => 'Maybe[LWP::UserAgent]',
177             );
178              
179             =head1 METHODS
180              
181             =head2 document
182              
183             Get or set a couch document.
184              
185             Usage,
186             To get a document
187             $couchdb->document($doc_id);
188              
189             To set a document
190             $couchdb->document($doc_id, $data);
191              
192             $data is a HashRef
193              
194              
195             =cut
196              
197             my $cache_namespace = 'COUCH_DOCS';
198              
199             sub document {
200             my $self = shift;
201             my $doc = shift;
202             my $data = shift;
203              
204             my $cache_key = $self->db . '_' . $doc;
205             if ($data) {
206             Cache::RedisDB->del($cache_namespace, $cache_key) if ($self->_can_cache);
207             $data = $self->master->document($doc, $data);
208             } else {
209             $data = Cache::RedisDB->get($cache_namespace, $cache_key) if ($self->_can_cache);
210              
211             if (not $data) {
212             $data = $self->replica->document($doc);
213             Cache::RedisDB->set($cache_namespace, $cache_key, $data, 127)
214             if ($data and $self->_can_cache);
215             }
216             }
217              
218             return $data;
219             }
220              
221             =head2 view
222              
223             Query a couchdb view
224              
225             Usage,
226             Without Parameters
227             $couchdb->view($db, $viewname);
228              
229             With Parameters
230             $couchdb->view($db, $viewname, $parameters);
231              
232             $parameters is a HashRef
233              
234              
235             =cut
236              
237             sub view {
238             my $self = shift;
239             my $view = shift;
240             my $params = shift;
241              
242             return $self->replica->view($view, $params);
243             }
244              
245             =head2 document_present
246              
247             A syntatic sugar to check if a document
248              
249             Usage,
250             if ($couchdb->document_present($doc_id)) {
251             ....
252             }
253              
254             Throws,
255             Nothing
256              
257             Returns,
258             1 - if document is found.
259             undef - if document is not found.
260              
261             =cut
262              
263             sub document_present {
264             my $self = shift;
265             my $doc = shift;
266              
267             try { $self->replica->document($doc); } or return;
268              
269             return 1;
270             }
271              
272             =head2 create_document
273              
274             Creates a couch document
275              
276             Usage,
277             my $doc_id = $couchdb->create_document($doc_id);
278              
279             =cut
280              
281             sub create_document {
282             my $self = shift;
283             my $doc = shift;
284              
285             return $self->master->create_document($doc);
286             }
287              
288             =head2 delete_document
289              
290             Deletes a couch document
291              
292             Usage,
293             $couchdb->delete_document($doc_id);
294              
295             =cut
296              
297             sub delete_document {
298             my $self = shift;
299             my $doc = shift;
300              
301             return $self->master->delete_document($doc);
302             }
303              
304             =head2 create_database
305              
306             Creates a CouchDB Database.
307              
308             Usage,
309             $couchdb->create_database($db);
310              
311              
312             =cut
313              
314             sub create_database {
315             my $self = shift;
316              
317             return $self->master->create_database();
318             }
319              
320             =head2 can_read
321              
322             Confirms that you can read from this couchdb
323              
324             Usage,
325             if($couchdb->can_read) {
326             ...
327             }
328              
329             Returns,
330             1 - can read
331             undef - otherwise
332             =cut
333              
334             sub can_read {
335             my $self = shift;
336             return $self->replica->can_connect;
337             }
338              
339             =head2 can_write
340              
341             Confirms that you can write to this couchdb
342              
343             Usage,
344             if($couchdb->can_write) {
345             ...
346             }
347              
348             Returns,
349             1 - can write
350             undef - otherwise
351              
352             =cut
353              
354             sub can_write {
355             my $self = shift;
356             return $self->master->can_connect;
357             }
358              
359             sub _build_replica {
360             my $self = shift;
361             my $params = {};
362              
363             $params->{host} = $self->replica_host;
364             $params->{port} = $self->replica_port;
365             $params->{db} = $self->db;
366              
367             $params->{protocol} = $self->replica_protocol if ($self->replica_protocol);
368             $params->{couchdb} = $self->couchdb if ($self->couchdb);
369              
370             return Data::CouchDB::Connection->new(%$params);
371             }
372              
373             sub _build_master {
374             my $self = shift;
375             my $params = {};
376              
377             $params->{host} = $self->master_host;
378             $params->{port} = $self->master_port;
379             $params->{db} = $self->db;
380              
381             $params->{protocol} = $self->master_protocol if ($self->master_protocol);
382             $params->{couchdb} = $self->couchdb if ($self->couchdb);
383              
384             return Data::CouchDB::Connection->new(%$params);
385             }
386              
387             has '_can_cache' => (
388             is => 'ro',
389             lazy_build => 1,
390             );
391              
392             sub _build__can_cache {
393             return try { Cache::RedisDB::redis_connection(); 1; };
394             }
395              
396             __PACKAGE__->meta->make_immutable;
397              
398              
399             =head1 AUTHOR
400              
401             Binary.com, C<< >>
402              
403             =head1 BUGS
404              
405             Please report any bugs or feature requests to C, or through
406             the web interface at L. I will be notified, and then you'll
407             automatically be notified of progress on your bug as I make changes.
408              
409              
410              
411              
412             =head1 SUPPORT
413              
414             You can find documentation for this module with the perldoc command.
415              
416             perldoc Data::CouchDB
417              
418              
419             You can also look for information at:
420              
421             =over 4
422              
423             =item * RT: CPAN's request tracker (report bugs here)
424              
425             L
426              
427             =item * AnnoCPAN: Annotated CPAN documentation
428              
429             L
430              
431             =item * CPAN Ratings
432              
433             L
434              
435             =item * Search CPAN
436              
437             L
438              
439             =back
440              
441              
442             =head1 ACKNOWLEDGEMENTS
443              
444              
445             =head1 LICENSE AND COPYRIGHT
446              
447             Copyright 2015 Binary.com.
448              
449             This program is free software; you can redistribute it and/or modify it
450             under the terms of the the Artistic License (2.0). You may obtain a
451             copy of the full license at:
452              
453             L
454              
455             Any use, modification, and distribution of the Standard or Modified
456             Versions is governed by this Artistic License. By using, modifying or
457             distributing the Package, you accept this license. Do not use, modify,
458             or distribute the Package, if you do not accept this license.
459              
460             If your Modified Version has been derived from a Modified Version made
461             by someone other than you, you are nevertheless required to ensure that
462             your Modified Version complies with the requirements of this license.
463              
464             This license does not grant you the right to use any trademark, service
465             mark, tradename, or logo of the Copyright Holder.
466              
467             This license includes the non-exclusive, worldwide, free-of-charge
468             patent license to make, have made, use, offer to sell, sell, import and
469             otherwise transfer the Package with respect to any patent claims
470             licensable by the Copyright Holder that are necessarily infringed by the
471             Package. If you institute patent litigation (including a cross-claim or
472             counterclaim) against any party alleging that the Package constitutes
473             direct or contributory patent infringement, then this Artistic License
474             to you shall terminate on the date that such litigation is filed.
475              
476             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
477             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
478             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
479             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
480             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
481             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
482             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
483             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
484              
485              
486             =cut
487              
488             1; # End of Data::CouchDB