|
Lines 380-404
Returns a representation of the object, suitable for API output.
Link Here
|
| 380 |
=cut |
380 |
=cut |
| 381 |
|
381 |
|
| 382 |
sub to_api { |
382 |
sub to_api { |
| 383 |
my ( $self ) = @_; |
383 |
my ( $self, $embeds ) = @_; |
| 384 |
my $json_object = $self->TO_JSON; |
384 |
my $json_object = $self->TO_JSON; |
| 385 |
|
385 |
|
| 386 |
my $to_api_mapping = $self->to_api_mapping; |
386 |
my $to_api_mapping = $self->to_api_mapping; |
| 387 |
|
387 |
|
| 388 |
# Rename attributes if there's a mapping |
388 |
# Rename attributes if there's a mapping |
| 389 |
foreach my $column ( keys %{$to_api_mapping} ) { |
389 |
if ( $self->can('to_api_mapping') ) { |
| 390 |
my $mapped_column = $to_api_mapping->{$column}; |
390 |
foreach my $column ( keys %{ $self->to_api_mapping } ) { |
| 391 |
if ( exists $json_object->{$column} |
391 |
my $mapped_column = $self->to_api_mapping->{$column}; |
| 392 |
&& defined $mapped_column ) |
392 |
if ( exists $json_object->{$column} |
| 393 |
{ |
393 |
&& defined $mapped_column ) |
| 394 |
# key != undef |
394 |
{ |
| 395 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
395 |
# key != undef |
|
|
396 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
| 397 |
} |
| 398 |
elsif ( exists $json_object->{$column} |
| 399 |
&& !defined $mapped_column ) |
| 400 |
{ |
| 401 |
# key == undef |
| 402 |
delete $json_object->{$column}; |
| 403 |
} |
| 396 |
} |
404 |
} |
| 397 |
elsif ( exists $json_object->{$column} |
405 |
} |
| 398 |
&& !defined $mapped_column ) |
406 |
|
| 399 |
{ |
407 |
if ($embeds) { |
| 400 |
# key == undef |
408 |
foreach my $embed (@$embeds) { |
| 401 |
delete $json_object->{$column}; |
409 |
my ( $curr, $next ) = split /\s*\.\s*/, $embed, 2; |
|
|
410 |
my @nxembeds; |
| 411 |
|
| 412 |
@nxembeds = ($next) if $next; |
| 413 |
|
| 414 |
my $children = $self->$curr; |
| 415 |
if ( ref $children eq 'ARRAY' ) { |
| 416 |
my @list; |
| 417 |
my $pos = 0; |
| 418 |
foreach my $child (@$children) { |
| 419 |
my $res = $child->to_api( \@nxembeds ); |
| 420 |
$res = { $json_object->{$curr}->[$pos], $res } |
| 421 |
if defined $json_object->{$curr} |
| 422 |
&& defined $json_object->{$curr}->[$pos]; |
| 423 |
push @list, $res; |
| 424 |
$pos++; |
| 425 |
} |
| 426 |
$json_object->{$curr} = \@list; |
| 427 |
} |
| 428 |
else { |
| 429 |
my $res = $children->to_api( \@nxembeds ); |
| 430 |
$res = { $json_object->{$curr}, $res } |
| 431 |
if defined $json_object->{$curr}; |
| 432 |
$json_object->{$curr} = $res; |
| 433 |
} |
| 402 |
} |
434 |
} |
| 403 |
} |
435 |
} |
| 404 |
|
436 |
|