Skip to content

Commit

Permalink
Merge pull request #17 from techman83/folders
Browse files Browse the repository at this point in the history
Add create folder/folder objects methods + tests
  • Loading branch information
techman83 committed Sep 9, 2015
2 parents 31aa50f + 6497e13 commit f79dd78
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 13 deletions.
23 changes: 15 additions & 8 deletions lib/AWS/WorkDocs/Content.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package AWS::WorkDocs::Content;
use v5.010;
use strict;
use warnings;
use Moo;
use Method::Signatures;
use Scalar::Util::Reftype;
use experimental 'switch';
use Data::Dumper;
use Carp qw(croak);
use AWS::WorkDocs::User;
use Method::Signatures;
use Moo;
use experimental 'switch';
use namespace::clean;

# ABSTRACT: A WorkDocs Content Object

Expand All @@ -30,7 +30,7 @@ shared/unshared through similar API methods. API wise Folders
=cut

my $Ref = sub {
croak "auth isn't a 'AWS::WorkDocs::Auth' object!" unless reftype( $_[0] )->class eq "AWS::WorkDocs::Auth";
croak "auth isn't a 'AWS::WorkDocs::Auth' object!" unless $_[0]->DOES("AWS::WorkDocs::Auth");
};

# Authentication Object
Expand All @@ -43,13 +43,17 @@ has '_Type' => ( is => 'ro', builder => "_build_Type" );

method _build_content() {
my $content;
$content = $self->auth->api_get("/$self->{_type}/$self->{Id}");
$content = $self->auth->api_get("/".$self->_type."/".$self->Id);
$self->_map_keys($content);
}

method _map_keys($data) {
foreach my $key (keys %{ $data->{$self->{_Type}} }) {
$self->{$key} = $data->{$self->{_Type}}{$key};
foreach my $key (keys %{ $data->{$self->_Type} }) {
if ( $key eq 'Folders' && @{$data->{$self->_Type}{$key}}[0]) {
$self->_push_folders($data->{$self->_Type}{$key});
} else {
$self->{$key} = $data->{$self->_Type}{$key};
}
}
}

Expand Down Expand Up @@ -135,6 +139,7 @@ other acceptable values are 'CONTRIBUTOR', 'COOWNER' or 'OWNER'.
=cut

method user_share(:$users,:$access = "VIEWER", :$message = "") {
use AWS::WorkDocs::User;
$access = uc($access);
if ($access !~ /COOWNER|VIEWER|OWNER|CONTRIBUTOR/) {
croak("$access not valid, only [COOWNER, VIEWER, OWNER, CONTRIBUTOR] are valid types");
Expand Down Expand Up @@ -178,6 +183,7 @@ usernames.
=cut

method user_unshare(:$users) {
use AWS::WorkDocs::User;
my $result;
if ( reftype( $users )->array ) {
foreach my $user ( @{$users} ) {
Expand All @@ -202,6 +208,7 @@ Will return an array of L<AWS::WorkDocs::User> objects.
=cut

method shared_users() {
use AWS::WorkDocs::User;
$self->retrieve;

my @users;
Expand Down
79 changes: 75 additions & 4 deletions lib/AWS/WorkDocs/Content/Folder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use v5.010;
use strict;
use warnings;
use Method::Signatures;
use Scalar::Util::Reftype;
use Carp 'croak';
use AWS::WorkDocs::User;
use Moo;
use namespace::clean
use MooX::HandlesVia;
use namespace::clean;

extends 'AWS::WorkDocs::Content';

Expand All @@ -28,4 +26,77 @@ Extends L<AWS::WorkDocs::Content>, see that for all documentation.
=cut

has 'Folders' => (
is => 'rw',
handles_via => 'Array',
handles => {
push_folder => 'push',
clear_folders => 'clear',
first_folder => 'first_index'
},
default => sub { [ ] },
);

before '_map_keys' => sub {
my $self = shift;
$self->clear_folders;
};

method _push_folders($folders) {
foreach my $folder (@{$folders}) {
$self->_push_folder($folder->{Id});
}
return;
}

method _push_folder($Id) {
use AWS::WorkDocs::Content::Folder;
$self->push_folder(AWS::WorkDocs::Content::Folder->new( Id => $Id, auth => $self->auth ));
}

=method create_folder
$folder->create($name)
Will create a child folder underneath this folder.
=cut

method create_folder($name) {
my $body = {
FolderName => $name,
ParentFolderId => $self->Id,
};

my $result = $self->auth->api_post("/".$self->_type, $body);
$self->_push_folder($result->{Folder}{Metadata}{Id});
return $result->{Folder}{Metadata}{Id};
}

=method child_folder
$folder->child_folder(Id => $id)
Will return a folder object for the given $id or '0' if it doesn't
exist in '$folder->Folders'.
=cut

method child_folder(:$Id) {
my $index = $self->first_folder( sub { $_->Id eq $Id } );
return $index == '-1' ? 0 : @{$self->Folders}[$index];
}

=method remove
$folder->remove;
*HERE BE DRAGONS* - this will literally remove the folder of
the instantiated object.
=cut

method remove {
$self->auth->api_delete("/".$self->{_type}."/".$self->Id);
}

1;
30 changes: 29 additions & 1 deletion t/AWS/WorkDocs/Content/Folder.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ sub folder_testing {
isa_ok($folder, "AWS::WorkDocs::Content::Folder");

can_ok($folder, qw(retrieve user_share
user_unshare shared_users shared_usernames));
user_unshare shared_users shared_usernames
create_folder));
};

my $user = AWS::WorkDocs::User->new(
Expand All @@ -51,6 +52,20 @@ sub folder_testing {
$user->create();
}

subtest 'Folder Create/Delete/Find' => sub {
my $id = $folder->create_folder("TestFolder1");
my $created = AWS::WorkDocs::Content::Folder->new(
Id => $id,
auth => $auth,
);
print $id."\n";
is($folder->child_folder(Id => $id)->Id, $id, "Find folder in Array of Folders by Id");
is($folder->child_folder(Id => "notafolder"), 0, "Return 0 when folder not found");
$created->remove;
$folder->retrieve;
is($folder->child_folder(Id => $id), 0, "Folder is not present after removal");
};

subtest 'Sharing' => sub {

# Share to user
Expand Down Expand Up @@ -88,6 +103,12 @@ sub folder_testing {
)
} "user_share dies correctly with too many arguments";

dies_ok { $folder->child_folder(
Id => "1234",
"blarg" => "blarg",
)
} "child_folder dies correctly with invalid arguments";

# Unshare a user
$folder->user_unshare( users => $config->{username} );
@users = $folder->shared_usernames;
Expand Down Expand Up @@ -137,6 +158,13 @@ sub folder_testing {
dies_ok { $folder->shared_usernames('argument') } "method 'shared_usernames' doesn't accept arguments";
dies_ok { $folder->shared_users('argument') } "method 'shared_users' doesn't accept arguments";
dies_ok { $folder->user_unshare() } "'user_unshare' requires a named argument";
dies_ok { $folder->_push_folders() } "'_push_folders' requires an argument";
dies_ok { $folder->_push_folders("arg","arg") } "'_push_folders' only takes one argument";
dies_ok { $folder->_push_folder() } "'_push_folder' requires an argument";
dies_ok { $folder->_push_folder("arg","arg") } "'_push_folder' only takes one argument";
dies_ok { $folder->create_folder() } "'create_folder' requires an argument";
dies_ok { $folder->create_folder("arg","arg") } "'create_folder' only takes one argument";
dies_ok { $folder->remove("arg") } "'remove' takes no arguments";
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions t/lib/AWS/WorkDocs/Test/Content.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ get '/folder/:Id' => sub {
Id => param('Id'),
PermissionsGranted => $temp->{permissions},
},
Folders => [
{
Id => '987654321',
},
]
},
}
};
Expand All @@ -26,6 +31,16 @@ get '/document/:Id' => sub {
}
};

post '/folder' => sub {
{
Folder => {
Metadata => {
Id => 1234,
},
},
}
};

put '/resource/:Id/permissions' => sub {
my $data = from_json(request->body);
my $content;
Expand Down Expand Up @@ -57,6 +72,11 @@ put '/resource/:Id/permissions' => sub {
return $content;
};


del '/folder/:Id' => sub {
return;
};

del '/resource/:Id/permissions/:SId' => sub {
@{$temp->{permissions}} = grep !(defined && $_->{User}{Id} eq param('SId')),@{$temp->{permissions}};
return;
Expand Down

0 comments on commit f79dd78

Please sign in to comment.