File Coverage

blib/lib/Catmandu/Importer/Zotero.pm
Criterion Covered Total %
statement 15 17 88.2
branch n/a
condition n/a
subroutine 5 6 83.3
pod n/a
total 20 23 86.9


line stmt bran cond sub pod time code
1             package Catmandu::Importer::Zotero;
2              
3 2     2   42085 use Catmandu::Sane;
  2         194133  
  2         14  
4 2     2   2515 use Catmandu::Util qw(:is);
  2         106939  
  2         769  
5 2     2   1498 use WWW::Zotero;
  2         297633  
  2         76  
6 2     2   17 use Moo;
  2         3  
  2         11  
7 2     2   1280 use feature 'state';
  2         4  
  2         1245  
8              
9             with 'Catmandu::Importer';
10              
11             has userID => (is => 'ro');
12             has groupID => (is => 'ro');
13             has collectionID => (is => 'ro');
14             has apiKey => (is => 'ro');
15              
16             # From WWW::Zotero
17             has sort => (is => 'ro');
18             has direction => (is => 'ro');
19             has itemKey => (is => 'ro');
20             has itemType => (is => 'ro');
21             has q => (is => 'ro');
22             has qmode => (is => 'ro');
23             has since => (is => 'ro');
24             has tag => (is => 'ro');
25              
26             has client => (is => 'lazy');
27              
28             sub _build_client {
29 0     0     my ($self) = @_;
30 0           WWW::Zotero->new(key => $self->apiKey);
31             }
32              
33             sub generator {
34             my ($self) = @_;
35              
36             sub {
37             state $generator;
38              
39             unless (defined $generator) {
40             my %options = ();
41              
42             $options{user} = $self->userID if $self->userID;
43             $options{group} = $self->groupID if $self->groupID;
44             $options{sort} = $self->sort if $self->sort;
45             $options{direction} = $self->direction if $self->direction;
46             $options{itemKey} = $self->itemKey if $self->itemKey;
47             $options{itemType} = $self->itemType if $self->itemType;
48             $options{q} = $self->q if $self->q();
49             $options{qmode} = $self->qmode if $self->qmode;
50             $options{since} = $self->since if $self->since;
51             $options{tag} = $self->tag if $self->tag;
52             $options{include} = 'data';
53            
54             $options{itemType} .= " -attachment";
55              
56             if ($self->collectionID) {
57             $options{collectionKey} = $self->collectionID;
58             $generator = $self->client->listCollectionItems(%options, generator => 1);
59             } else {
60             $generator = $self->client->listItems(%options, generator => 1);
61             }
62             }
63              
64             my $record = $generator->();
65              
66             return undef unless $record;
67            
68             if ($record->{meta}->{numChildren} > 0) {
69             # Find children
70             $record->{children} = $self->client->getItemChildren(
71             user => $self->userID,
72             group => $self->groupID,
73             itemKey => $record->{_id}
74             );
75             }
76             else {
77             $record->{children} = [];
78             }
79            
80             $record;
81             };
82             }
83              
84             1;
85              
86             __END__