File Coverage

blib/lib/RDF/AllegroGraph/Catalog4.pm
Criterion Covered Total %
statement 30 76 39.4
branch 0 32 0.0
condition 0 5 0.0
subroutine 10 15 66.6
pod 4 5 80.0
total 44 133 33.0


line stmt bran cond sub pod time code
1             package RDF::AllegroGraph::Catalog4;
2              
3 15     15   132 use strict;
  15         33  
  15         493  
4 15     15   76 use warnings;
  15         31  
  15         669  
5              
6             require Exporter;
7 15     15   80 use base qw(RDF::AllegroGraph::Catalog);
  15         29  
  15         2362  
8              
9             =pod
10              
11             =head1 NAME
12              
13             RDF::AllegroGraph::Catalog4 - AllegroGraph catalog handle for AGv4
14              
15             =cut
16              
17 15     15   11525 use RDF::AllegroGraph::Repository4;
  15         48  
  15         885  
18 15     15   103 use RDF::AllegroGraph::Utils;
  15         31  
  15         479  
19              
20 15     15   90 use JSON;
  15         34  
  15         124  
21 15     15   2180 use HTTP::Status;
  15         31  
  15         5688  
22 15     15   97 use Fcntl;
  15         37  
  15         5218  
23 15     15   122 use Data::Dumper;
  15         30  
  15         10435  
24              
25             =pod
26              
27             =head1 INTERFACE
28              
29             =head2 Constructor
30              
31             The constructor will try to connect to the server and will C if fetching the repositories (even
32             the empty list) fails.
33              
34             =cut
35              
36             sub new {
37 0     0 0   my $class = shift;
38 0           my %options = @_;
39 0 0         die "no NAME" unless $options{NAME};
40 0 0         die "no SERVER" unless $options{SERVER};
41 0           my $self = bless \%options, $class;
42 0           eval { # test whether it exists, by probing the repositories (could be anything else for that matter)
43 0 0         $self->repositories unless $self->{NAME} eq '/'; # for non-root catalogs we check whether they exist
44 0 0         }; if ($@) { # if something weird happened here
45 0           die "catalog '".$self->{NAME}."' does not exist on the server";
46             }
47 0           return $self; # otherwise we continue with normal business
48             }
49              
50             =pod
51              
52              
53             =head2 Methods
54              
55             =over
56              
57             =item B
58              
59             Removes the named catalog from the server.
60              
61             B: I have no idea what happens with any repositories in there.
62              
63             =cut
64              
65             sub disband {
66 0     0 1   my $self = shift;
67 0           my $requ = HTTP::Request->new (DELETE => $self->{SERVER}->{ADDRESS} . '/catalogs' . $self->{NAME});
68 0           my $resp = $self->{SERVER}->{ua}->request ($requ);
69 0 0         die "protocol error: ".$resp->status_line.' ('.$resp->content.')' unless $resp->is_success;
70             }
71              
72             =pod
73              
74             =item B
75              
76             I<@repos> = I<$cat>->repositories
77              
78             This method returns a list of L objects of this catalog.
79              
80             =cut
81              
82             sub repositories {
83 0     0 1   my $self = shift;
84 0 0         my $resp = $self->{SERVER}->{ua}->get ($self->{SERVER}->{ADDRESS} . ($self->{NAME} eq '/'
85             ? ''
86             : '/catalogs' . $self->{NAME} ) . '/repositories');
87 0 0         die "protocol error: ".$resp->status_line unless $resp->is_success;
88 0           my $repo = from_json ($resp->content);
89             return
90 0           map { RDF::AllegroGraph::Repository4->new (%$_, CATALOG => $self) }
  0            
91 0           map { RDF::AllegroGraph::Utils::_hash_to_perl ($_) }
92             @$repo;
93             }
94              
95             =pod
96              
97             =item B
98              
99             I<$repo> = I<$cat>->repository (I<$repo_id> [, I<$mode> ])
100              
101             This method returns an L object for the repository with
102             the provided id. That id always has the form C.
103              
104             If that repository does not exist in the catalog, then an exception C will be
105             raised. That is, unless the optional I is provided having the POSIX value C. Then the
106             repository will be created.
107              
108             =cut
109              
110             sub repository {
111 0     0 1   my $self = shift;
112 0           my $id = shift;
113 0   0       my $mode = shift || O_RDONLY;
114              
115 0 0         if (my ($repo) = grep { $_->id eq $id } $self->repositories) {
  0 0          
116 0           return $repo;
117             } elsif ($mode == O_CREAT) {
118 0           my $uri;
119 0 0         if ($id =~ m{^(/[^/]+)$}) { # root catalog repo
    0          
120 0           my $repoid = $1;
121 0 0         die "do not want to open root catalog repository within non-root catalog" unless $self->{NAME} eq '/'; # we are not inside the root catalog?
122 0           $uri = $self->{SERVER}->{ADDRESS} . '/repositories' . $repoid; # create the uri for below
123             } elsif ($id =~ m{^(/[^/]+?)(/.+)$}) {
124 0           my $catid = $1;
125 0           my $repoid = $2;
126 0 0         die "do not want to open non-root repository in named catalog" unless $self->{NAME} eq $1;
127 0           $uri = $self->{SERVER}->{ADDRESS} . '/catalogs' . $catid . '/repositories' . $repoid;
128             } else {
129 0           die "cannot handle repository id '$id'";
130             }
131 15     15   109 use HTTP::Request;
  15         29  
  15         4294  
132 0           my $requ = HTTP::Request->new (PUT => $uri);
133 0           my $resp = $self->{SERVER}->{ua}->request ($requ);
134 0 0         die "protocol error: ".$resp->status_line unless $resp->code == RC_NO_CONTENT;
135 0           return $self->repository ($id); # recursive, but without forced create
136             } else {
137 0           die "cannot open repository '$id'";
138             }
139             }
140              
141             =pod
142              
143             =item B
144              
145             This method returns the protocol version the catalog supports.
146              
147             =cut
148              
149             sub protocol {
150 0     0 1   my $self = shift;
151 0 0         my $resp = $self->{SERVER}->{ua}->get ($self->{SERVER}->{ADDRESS} . ($self->{NAME} eq '/'
152             ? '/protocol'
153             : '/catalogs' . $self->{NAME} . '/protocol'));
154 0 0         die "protocol error: ".$resp->status_line unless $resp->is_success;
155 0   0       return $resp->content =~ m/^"?(.*?)"?$/ && $1;
156             }
157              
158             =pod
159              
160             =back
161              
162             =head1 AUTHOR
163              
164             Robert Barta, C<< >>
165              
166             =head1 COPYRIGHT & LICENSE
167              
168             Copyright 20(09|10|11) Robert Barta, all rights reserved.
169              
170             This program is free software; you can redistribute it and/or modify it under the same terms as Perl
171             itself.
172              
173             L
174              
175             =cut
176              
177             our $VERSION = '0.04';
178              
179             1;
180              
181             __END__