File Coverage

blib/lib/Net/Google/Drive/Simple/Item.pm
Criterion Covered Total %
statement 32 32 100.0
branch 16 18 88.8
condition 1 3 33.3
subroutine 7 8 87.5
pod 3 3 100.0
total 59 64 92.1


line stmt bran cond sub pod time code
1             ###########################################
2             ###########################################
3              
4             use strict;
5 10     10   177794 use warnings;
  10         23  
  10         245  
6 10     10   39  
  10         17  
  10         3321  
7             our $VERSION = '3.00';
8              
9             my ( $pkg, $data ) = @_;
10              
11 5     5 1 5198 die q[new is expecting one hashref] unless ref $data eq 'HASH';
12              
13 5 100       28 return bless { data => $data }, $pkg;
14             }
15 3         9  
16             my ($self) = @_;
17              
18             my $mimeType = $self->mimeType;
19 6     6 1 326 $mimeType = '' unless defined $mimeType;
20             return $mimeType eq 'application/vnd.google-apps.folder' ? 1 : 0;
21 6         28 }
22 6 100       13  
23 6 100       31 return $_[0]->is_folder ? 0 : 1;
24             }
25              
26              
27 3 100   3 1 9 our $AUTOLOAD;
28              
29             my $self = shift;
30       0     my $key = $AUTOLOAD;
31             $key =~ s/.*:://;
32              
33 25     25   7818 my $data = $self->{data};
34             die unless $data && ref $data;
35 25         35  
36 25         34 return $data->{$key} if exists $data->{$key};
37 25         111  
38             # catching typos :-)
39 25         48 my $lc_key = lc $key;
40 25 50 33     87 if ( $lc_key ne $key ) {
41             my $mapping = $self->_mapping();
42 25 100       95 return $data->{ $mapping->{$lc_key} } if exists $mapping->{$lc_key};
43             }
44              
45 6         11 die "Cannot find any attribute named '$key' for " . ref($self);
46 6 100       15 }
47 5         15  
48 5 50       31 my ($self) = @_;
49              
50             return $self->{_mapping} if defined $self->{_mapping};
51 1         10 $self->{_mapping} = { map { lc($_) => $_ } keys %{ $self->{data} } };
52              
53             return $self->{_mapping};
54             }
55 5     5   8  
56             1;
57 5 100       25  
58 2         3  
  43         95  
  2         12  
59              
60 2         12 =head1 NAME
61              
62             Net::Google::Drive::Simple::Item - Representation of a Google Drive File
63              
64             =head1 SYNOPSIS
65              
66             use feature 'say';
67             use Net::Google::Drive::Simple;
68              
69             # requires a ~/.google-drive.yml file with an access token,
70             # see description from Net::Google::Drive::Simple
71              
72             my $gd = Net::Google::Drive::Simple->new();
73             my $children = $gd->children( "/" ); # or any other folder /path/location
74              
75             foreach my $item ( @$children ) {
76             # $item is one Net::Google::Drive::Simple::Item object
77              
78             if ( $item->is_folder ) {
79             say "** ", $item->title, " is a folder";
80             } else {
81             say $item->title, " is a file ", $item->mimeType;
82             eval { # originalFilename not necessary available for all files
83             say $item->originalFilename(), " can be downloaded at ", $item->downloadUrl();
84             };
85             }
86             }
87              
88             =head1 DESCRIPTION
89              
90             Net::Google::Drive::Simple::Item provides a class to manipulate the
91             File methadata from Google Drive API.
92              
93             https://developers.google.com/drive/api/v3/reference/files
94              
95             =head2 GETTING STARTED
96              
97             This class should not be used directly, and you should use
98             L<Net::Google::Drive::Simple> as shown in the synopsis.
99              
100             =head1 METHODS
101              
102             =over 4
103              
104             =item C<new( $data )>
105              
106             Constructor, creates one object which hold on hash ref C<$data>
107             to manipulate the metadata for a file.
108             All the keys from the hash below as documented in L<Google Drive API Doc|https://developers.google.com/drive/api/v3/reference/files>
109             are mapped using AUTOLOAD so you can use helpers for every first level entry like these:
110              
111             $file->kind;
112             $file->id;
113             $file->name;
114             $file->mimeType;
115             ....
116              
117             {
118             "kind": "drive#file",
119             "id": string,
120             "name": string,
121             "mimeType": string,
122             "description": string,
123             "starred": boolean,
124             "trashed": boolean,
125             "explicitlyTrashed": boolean,
126             "trashingUser": {
127             "kind": "drive#user",
128             "displayName": string,
129             "photoLink": string,
130             "me": boolean,
131             "permissionId": string,
132             "emailAddress": string
133             },
134             "trashedTime": datetime,
135             "parents": [
136             string
137             ],
138             "properties": {
139             (key): string
140             },
141             "appProperties": {
142             (key): string
143             },
144             "spaces": [
145             string
146             ],
147             "version": long,
148             "webContentLink": string,
149             "webViewLink": string,
150             "iconLink": string,
151             "hasThumbnail": boolean,
152             "thumbnailLink": string,
153             "thumbnailVersion": long,
154             "viewedByMe": boolean,
155             "viewedByMeTime": datetime,
156             "createdTime": datetime,
157             "modifiedTime": datetime,
158             "modifiedByMeTime": datetime,
159             "modifiedByMe": boolean,
160             "sharedWithMeTime": datetime,
161             "sharingUser": {
162             "kind": "drive#user",
163             "displayName": string,
164             "photoLink": string,
165             "me": boolean,
166             "permissionId": string,
167             "emailAddress": string
168             },
169             "owners": [
170             {
171             "kind": "drive#user",
172             "displayName": string,
173             "photoLink": string,
174             "me": boolean,
175             "permissionId": string,
176             "emailAddress": string
177             }
178             ],
179             "teamDriveId": string,
180             "driveId": string,
181             "lastModifyingUser": {
182             "kind": "drive#user",
183             "displayName": string,
184             "photoLink": string,
185             "me": boolean,
186             "permissionId": string,
187             "emailAddress": string
188             },
189             "shared": boolean,
190             "ownedByMe": boolean,
191             "capabilities": {
192             "canAddChildren": boolean,
193             "canChangeCopyRequiresWriterPermission": boolean,
194             "canChangeViewersCanCopyContent": boolean,
195             "canComment": boolean,
196             "canCopy": boolean,
197             "canDelete": boolean,
198             "canDeleteChildren": boolean,
199             "canDownload": boolean,
200             "canEdit": boolean,
201             "canListChildren": boolean,
202             "canModifyContent": boolean,
203             "canMoveChildrenOutOfTeamDrive": boolean,
204             "canMoveChildrenOutOfDrive": boolean,
205             "canMoveChildrenWithinTeamDrive": boolean,
206             "canMoveChildrenWithinDrive": boolean,
207             "canMoveItemIntoTeamDrive": boolean,
208             "canMoveItemOutOfTeamDrive": boolean,
209             "canMoveItemOutOfDrive": boolean,
210             "canMoveItemWithinTeamDrive": boolean,
211             "canMoveItemWithinDrive": boolean,
212             "canMoveTeamDriveItem": boolean,
213             "canReadRevisions": boolean,
214             "canReadTeamDrive": boolean,
215             "canReadDrive": boolean,
216             "canRemoveChildren": boolean,
217             "canRename": boolean,
218             "canShare": boolean,
219             "canTrash": boolean,
220             "canTrashChildren": boolean,
221             "canUntrash": boolean
222             },
223             "viewersCanCopyContent": boolean,
224             "copyRequiresWriterPermission": boolean,
225             "writersCanShare": boolean,
226             "permissions": [
227             permissions Resource
228             ],
229             "permissionIds": [
230             string
231             ],
232             "hasAugmentedPermissions": boolean,
233             "folderColorRgb": string,
234             "originalFilename": string,
235             "fullFileExtension": string,
236             "fileExtension": string,
237             "md5Checksum": string,
238             "size": long,
239             "quotaBytesUsed": long,
240             "headRevisionId": string,
241             "contentHints": {
242             "thumbnail": {
243             "image": bytes,
244             "mimeType": string
245             },
246             "indexableText": string
247             },
248             "imageMediaMetadata": {
249             "width": integer,
250             "height": integer,
251             "rotation": integer,
252             "location": {
253             "latitude": double,
254             "longitude": double,
255             "altitude": double
256             },
257             "time": string,
258             "cameraMake": string,
259             "cameraModel": string,
260             "exposureTime": float,
261             "aperture": float,
262             "flashUsed": boolean,
263             "focalLength": float,
264             "isoSpeed": integer,
265             "meteringMode": string,
266             "sensor": string,
267             "exposureMode": string,
268             "colorSpace": string,
269             "whiteBalance": string,
270             "exposureBias": float,
271             "maxApertureValue": float,
272             "subjectDistance": integer,
273             "lens": string
274             },
275             "videoMediaMetadata": {
276             "width": integer,
277             "height": integer,
278             "durationMillis": long
279             },
280             "isAppAuthorized": boolean,
281             "exportLinks": {
282             (key): string
283             }
284             }
285              
286             =item C<$file-E<gt>is_folder>
287              
288             Return a boolean '1' or '0' to indicate if the current file
289             is one folder or not. (this is using the mimeType value).
290              
291             $iterm->is_folder
292              
293             =item C<$file-E<gt>is_file>
294              
295             Return a boolean '1' or '0' to indicate if the current file
296             is a regular file (not a folder).
297             (this is using the mimeType value).
298              
299             $iterm->is_file or ! $iterm->is_folder
300              
301             =item C<$file-E<gt>kind>
302              
303             Identifies what kind of resource this is. Value: the fixed string "drive#file".
304              
305             =item C<$file-E<gt>id>
306              
307             The ID of the file.
308              
309             =item C<$file-E<gt>name>
310              
311             The name of the file. This is not necessarily unique within a folder.
312             Note that for immutable items such as the top level folders of shared drives, My Drive root folder,
313             and Application Data folder the name is constant.
314              
315             =item C<$file-E<gt>mimeType>
316              
317             The MIME type of the file.
318             Google Drive will attempt to automatically detect an appropriate value from uploaded content if no value is provided. The value cannot be changed unless a new revision is uploaded.
319              
320             If a file is created with a Google Doc MIME type, the uploaded content will be imported if possible. The supported import formats are published in the About resource.
321              
322             =item C<$file-E<gt>description>
323              
324             A short description of the file.
325              
326             =item C<$file-E<gt>starred>
327              
328             boolean - Whether the user has starred the file.
329              
330             =item C<$file-E<gt>trashed>
331              
332             boolean - Whether the file has been trashed, either explicitly or from a trashed parent folder. Only the owner may trash a file, and other users cannot see files in the owner's trash.
333              
334             =item C<$file-E<gt>explicitlyTrashed>
335              
336             boolean - Whether the file has been explicitly trashed, as opposed to recursively trashed from a parent folder.
337              
338             =item C<$file-E<gt>version>
339              
340             A monotonically increasing version number for the file. This reflects every change made to the file on the server, even those not visible to the user.
341              
342             =item C<$file-E<gt>createdTime>
343              
344             The time at which the file was created (RFC 3339 date-time).
345              
346             =item C<$file-E<gt>modifiedTime>
347              
348             The last time the file was modified by anyone (RFC 3339 date-time).
349             Note that setting modifiedTime will also update modifiedByMeTime for the user.
350              
351             =item C<$file-E<gt>size>
352              
353             The size of the file's content in bytes. This is only applicable to files with binary content in Google Drive.
354              
355             =item C<$file-E<gt>capabilities>
356              
357             Capabilities the current user has on this file. Each capability corresponds to a fine-grained action that a user may take.
358              
359             =item more...
360              
361             Please refer to the official documentation
362              
363             https://developers.google.com/drive/api/v3/reference/files
364              
365             for more informations about the possible fields.
366              
367             =back
368              
369             =head1 Tips
370              
371             =head2 AUTOLOAD
372              
373             The implemented AUTOLOAD function is case insensitive.
374             The following functions C<$iterm->id>, C<$iterm->ID> and C<$iterm->Id>
375             for example are all equivalent and return the 'id' for the file.
376              
377              
378             When trying to access to an unknown field, the code will die/throw an exception.
379              
380             eval { $item->DoNotExist } and $@ =~ m{"Cannot find any attribute named 'DoNotExist'}
381              
382             =head1 AUTHOR
383              
384             2019, Nicolas R. <cpan@atoomic.org>