Neo4j/Node.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Neo4j/Types.pm ↑ Summary Neo4j/Relationship.pm »
1 package Neo4j::Node;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Neo4j::Relationship;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use Neo4j::REST::Client;
10  
11 use Data::Printer;
12 use JSON;
13  
14 has 'id' => (
15 isa => 'Num',
16 required => 0,
17 is => 'rw',
18 );
19  
20 has 'client' => (
21 required => 1,
22 isa => 'Neo4j::REST::Client',
23 is => 'ro',
24 );
25  
26 has '_self_endpoint' => (
27 isa => 'Str',
28 is => 'rw',
29 required => 0,
30 );
31  
32 with 'Neo4j::Role::REST', 'Neo4j::Role::Data';
33  
34 # with every new Neo4j::Node, we create it on the database
35 sub BUILD {
36 my $self = shift;
37 if ( defined $self->id ) {
38 # I have an ID already! I exist! just retrieve me!
39 $self->get( $self->id );
40  
41 } elsif ( defined $self->_self_endpoint ) {
42 $self->_self_endpoint;
43 $self->get( ( split( '/', $self->_self_endpoint ) )[-1] );
44  
45 } else {
46  
47 $self->create;
48  
49 # have to create one from the provided data
50 }
51 }
52  
53 sub get {
54 my $self = shift;
55 my $id = shift;
56  
57 my $url = sprintf( '%s/%s', $self->client->root_endpoint('node'), $id );
58  
59 $self->client->GET($url);
60  
61 for ( $self->client->status ) {
62 when ('200') {
63 $self->_endpoints( decode_json( $self->client->content ) );
64 $self->_self_endpoint( $self->_endpoints->{self} );
65 $self->data( $self->_endpoints->{data} );
66 return $self;
67 }
68 when ('404') {
69  
70 # not found
71 return undef;
72 }
73 default {
74  
75 # ooops something's wrong!
76 }
77 }
78 }
79  
80 sub create {
81 my $self = shift;
82 $self->client->POST( $self->client->root_endpoint('node'),
83 encode_json( $self->data ) );
84  
85 for ( $self->client->status ) {
86 when ('201') {
87  
88 # all good
89 $self->_endpoints( decode_json( $self->client->content ) );
90 $self->_self_endpoint( $self->_endpoints->{self} );
91 $self->id( ( split( '/', $self->_self_endpoint ) )[-1] );
92 $self->data( $self->_endpoints->{data} );
93 return $self;
94 }
95 default {
96  
97 # should throw exception
98 }
99 }
100 }
101  
102 sub save {
103 my $self = shift;
104 $self->client->PUT( $self->endpoint('properties'), encode_json( $self->data ) );
105  
106 for ( $self->client->status ) {
107 when (201) {
108  
109 # all good
110 $self->_endpoints( decode_json( $self->client->content ) );
111 $self->data( $self->_endpoints->{data} );
112 return $self;
113 }
114 default {
115  
116 # should throw exception
117 }
118 }
119 }
120  
121 sub clear_properties {
122 my $self = shift;
123 $self->data({});
124 return $self;
125 }
126  
127 sub reload {
128 my $self = shift;
129 return $self->get($self->id);
130 }
131  
132 sub delete {
133 my $self = shift;
134 $self->client->DELETE( $self->_self_endpoint );
135  
136 for ( $self->client->status ) {
137 when ('204') {
138  
139 # all good
140 return undef;
141 }
142 when ('409') {
143  
144 # should throw a conflict exception,
145 # can't delete a node in a relation
146 }
147 default {
148 # should throw exception
149 }
150 }
151 }
152  
153 sub relationships {
154 my $self = shift;
155 my $args = shift;
156  
157 my $url;
158  
159 if(defined $args->{direction}) {
160 for($args->{direction}) {
161 when('all') {
162 $url = $self->endpoint('all_relationships');
163 }
164 when('in'){
165 $url = $self->endpoint('incoming_relationships');
166 }
167 when('out'){
168 $url = $self->endpoint('outgoing_relationships');
169 }
170 default{
171 #all
172 $url = $self->endpoint('all_relationships');
173 }
174 }
175 } else {
176 $url = $self->endpoint('all_relationships');
177  
178 }
179  
180 if(defined $args->{type}) {
181 $url .= '/' . join('&', $args->{type})
182 }
183  
184 $self->client->GET($url);
185  
186 for ( $self->client->status ) {
187 when ('200') {
188 my @processed_payload = map { Neo4j::Relationship->new({ client => $self->client, _self_endpoint => $_->{self} })} @{ decode_json( $self->client->content ) };
189 return [ @processed_payload ];
190 }
191 when ('404') {
192  
193 # not found
194 return undef;
195 }
196 default {
197  
198 # ooops something's wrong!
199 }
200 }
201  
202 }
203  
204 sub incoming_relationships {
205  
206 }
207  
208 sub outgoing_relationships {
209  
210 }
211  
212 1;
213  
214 =cut
215 _endpoints {
216 all_relationships "http://localhost:7474/db/data/node/124/relationships/all",
217 all_typed_relationships "http://localhost:7474/db/data/node/124/relationships/all/{-list|&|types}",
218 create_relationship "http://localhost:7474/db/data/node/124/relationships",
219 data var{Zeta}{data},
220 extensions {},
221 incoming_relationships "http://localhost:7474/db/data/node/124/relationships/in",
222 incoming_typed_relationships "http://localhost:7474/db/data/node/124/relationships/in/{-list|&|types}",
223 outgoing_relationships "http://localhost:7474/db/data/node/124/relationships/out",
224 outgoing_typed_relationships "http://localhost:7474/db/data/node/124/relationships/out/{-list|&|types}",
225 paged_traverse "http://localhost:7474/db/data/node/124/paged/traverse/{returnType}{?pageSize,leaseTime}",
226 properties "http://localhost:7474/db/data/node/124/properties",
227 property "http://localhost:7474/db/data/node/124/properties/{key}",
228 self "http://localhost:7474/db/data/node/124",
229 traverse "http://localhost:7474/db/data/node/124/traverse/{returnType}"
230 =cut
231  
« Neo4j/Types.pm ↑ Summary Neo4j/Relationship.pm »