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