| 1 | package WWW::Asana::Role::NewFromResponse; | ||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | $WWW::Asana::Role::NewFromResponse::AUTHORITY = 'cpan:GETTY'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | { | ||||||||||||||
| 6 | 1 | $WWW::Asana::Role::NewFromResponse::VERSION = '0.003'; | |||||||||||||
| 7 | } | ||||||||||||||
| 8 | # ABSTRACT: Role which implements new_from_response for Asana classes | ||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | ▶ | use MooX::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use DateTime::Format::ISO8601; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use Class::Load ':all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | |||||||||||||||
| 15 | 0 | ▶ | sub new_from_response { | ||||||||||||
|
Subroutine
new_from_response: not called CC=16 SCAR=56.1
Pod
documented
| |||||||||||||||
| 16 | 0 | my ( $class, $data ) = @_; | |||||||||||||
| 17 | 0 | ▶ | die "First parameter to new_From_response need to be a HashRef" unless ref $data eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 18 | 0 | my %data = %{$data}; | |||||||||||||
| 19 | 0 | my %multi_mapping = ( | |||||||||||||
| 20 | followers => 'WWW::Asana::User', | ||||||||||||||
| 21 | workspaces => 'WWW::Asana::Workspace', | ||||||||||||||
| 22 | projects => 'WWW::Asana::Project', | ||||||||||||||
| 23 | tags => 'WWW::Asana::Tag', | ||||||||||||||
| 24 | ); | ||||||||||||||
| 25 | 0 | my @needs_workspace = qw( projects tags ); | |||||||||||||
| 26 | 0 | my %single_mapping = ( | |||||||||||||
| 27 | assignee => 'WWW::Asana::User', | ||||||||||||||
| 28 | workspace => 'WWW::Asana::Workspace', | ||||||||||||||
| 29 | created_by => 'WWW::Asana::User', | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | 0 | my %new = %data; | |||||||||||||
| 32 | # single mapping before multi mapping so that workspace is already there | ||||||||||||||
| 33 | 0 | for my $key (keys %single_mapping) { | |||||||||||||
| 34 | 0 | ▶ | if (exists $data{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 0 | ▶ | if ($data{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 0 | my $target_class = $single_mapping{$key}; | |||||||||||||
| 37 | 0 | ▶ | load_class($target_class) unless is_class_loaded($target_class); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | $new{$key} = $target_class->new_from_response({ | ||||||||||||||
| 39 | 0 | ▶ | %{$data{$key}}, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | defined $data{client} ? ( client => $data{client} ) : (), | ||||||||||||||
| 41 | response => $data{response}, | ||||||||||||||
| 42 | 0 | }); | |||||||||||||
| 43 | } else { | ||||||||||||||
| 44 | 0 | delete $new{$key}; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | 0 | for my $key (keys %multi_mapping) { | |||||||||||||
| 49 | 0 | ▶ | if (exists $data{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 0 | $new{$key} = []; | |||||||||||||
| 51 | 0 | my $target_class = $multi_mapping{$key}; | |||||||||||||
| 52 | 0 | for (@{$data{$key}}) { | |||||||||||||
| 53 | 0 | ▶ | load_class($target_class) unless is_class_loaded($target_class); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 0 | push @{$new{$key}}, $target_class->new_from_response({ | |||||||||||||
| 55 | 0 | ▶ | %{$_}, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 56 | defined $data{client} ? ( client => $data{client} ) : (), | ||||||||||||||
| 57 | 0 | (grep { $_ eq $key } @needs_workspace) ? ( workspace => $new{workspace} ) : (), | |||||||||||||
| 58 | response => $data{response}, | ||||||||||||||
| 59 | 0 | }); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | 0 | for my $key (qw( completed_at modified_at created_at due_on )) { | |||||||||||||
| 64 | 0 | ▶ | if (exists $data{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 0 | ▶ | if ($data{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | $new{$key} = DateTime::Format::ISO8601->parse_datetime($data{$key}); | |||||||||||||
| 67 | } else { | ||||||||||||||
| 68 | 0 | delete $new{$key}; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | 0 | return $class->new(%new); | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 1 | 1; | |||||||||||||
| 76 | __END__ | ||||||||||||||