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