| 1 | package Google::RestApi::SheetsApi4; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '2.2.5'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Google::RestApi::Setup; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Module::Load qw( load ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Try::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use YAML::Any (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use aliased 'Google::RestApi::DriveApi3'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use aliased 'Google::RestApi::SheetsApi4::Spreadsheet'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | Readonly our $Sheets_Endpoint => "https://sheets.googleapis.com/v4/spreadsheets"; | |||||||||||||||||||||||||||
| 16 | 2 | Readonly our $Spreadsheet_Id => $Google::RestApi::DriveApi3::Drive_File_Id; | |||||||||||||||||||||||||||
| 17 | 2 | Readonly our $Spreadsheet_Uri => "https://docs.google.com/spreadsheets/d"; | |||||||||||||||||||||||||||
| 18 | 2 | Readonly our $Worksheet_Id => "[0-9]+"; | |||||||||||||||||||||||||||
| 19 | 2 | Readonly our $Worksheet_Uri => "[#&]gid=([0-9]+)"; | |||||||||||||||||||||||||||
| 20 | 2 | Readonly our $Spreadsheet_Filter => "mimeType = 'application/vnd.google-apps.spreadsheet'"; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 66 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 66 | my $class = shift; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 66 | state $check = signature( | |||||||||||||||||||||||||||
| 26 | bless => !!0, | ||||||||||||||||||||||||||||
| 27 | named => [ | ||||||||||||||||||||||||||||
| 28 | api => HasApi, # the G::RestApi object that will be used to send http calls. | ||||||||||||||||||||||||||||
| 29 | drive => HasMethods[qw(list)], { optional => 1 }, # a drive instnace, could be your own, defaults to G::R::DriveApi3. | ||||||||||||||||||||||||||||
| 30 | endpoint => Str, { default => $Sheets_Endpoint }, # this gets tacked on to the api uri to reach the sheets endpoint. | ||||||||||||||||||||||||||||
| 31 | ], | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | 66 | my $self = $check->(@_); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 66 | return bless $self, $class; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # this gets called by lower-level classes like worksheet and range objects. they | ||||||||||||||||||||||||||||
| 39 | # will have passed thier own uri with params and possible body, we tack on the | ||||||||||||||||||||||||||||
| 40 | # sheets endpoint and pass it up the line to G::RestApi to make the actual call. | ||||||||||||||||||||||||||||
| 41 | 262 | ▶ | sub api { | ||||||||||||||||||||||||||
|
Subroutine
api: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 262 | my $self = shift; | |||||||||||||||||||||||||||
| 43 | 262 | state $check = signature( | |||||||||||||||||||||||||||
| 44 | bless => !!0, | ||||||||||||||||||||||||||||
| 45 | named => [ | ||||||||||||||||||||||||||||
| 46 | uri => Str, { default => '' }, | ||||||||||||||||||||||||||||
| 47 | _extra_ => slurpy HashRef, # just pass through any extra params to G::RestApi::api call. | ||||||||||||||||||||||||||||
| 48 | ], | ||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | 262 | my $p = named_extra($check->(@_)); | |||||||||||||||||||||||||||
| 51 | 262 | my $uri = $self->{endpoint}; # tack on the uri endpoint and pass the buck. | |||||||||||||||||||||||||||
| 52 | 262 | ▶ | $uri .= "/$p->{uri}" if $p->{uri}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 262 | return $self->rest_api()->api(%$p, uri => $uri); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 25 | ▶ | sub create_spreadsheet { | ||||||||||||||||||||||||||
|
Subroutine
create_spreadsheet: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 25 | state $check = signature( | |||||||||||||||||||||||||||
| 60 | bless => !!0, | ||||||||||||||||||||||||||||
| 61 | named => [ | ||||||||||||||||||||||||||||
| 62 | title => Str, { optional => 1 }, | ||||||||||||||||||||||||||||
| 63 | name => Str, { optional => 1 }, | ||||||||||||||||||||||||||||
| 64 | _extra_ => slurpy HashRef, | ||||||||||||||||||||||||||||
| 65 | ], | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | 25 | my $p = named_extra($check->(@_)); | |||||||||||||||||||||||||||
| 68 | # we allow name and title to be synonymous for convenience. it's actuall title in the google api. | ||||||||||||||||||||||||||||
| 69 | 25 | ▶ | $p->{title} || $p->{name} or LOGDIE "Either 'title' or 'name' should be supplied"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$$p{"title"} or $$p{"name"}
MC/DC100%2/2
$$p{"title"} or $$p{"name"}
$$p{"title"}
$$p{"name"}
Truth table100%3/3
A or B
A= $$p{"title"}B= $$p{"name"}
| |||||||||||||||||||||||||||||
| 70 | 24 | ▶ | $p->{title} ||= $p->{name}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$$p{"title"} ||= $$p{"name"}
MC/DC0%0/2
$$p{"title"} ||= $$p{"name"}
$$p{"title"}
$$p{"name"}
Truth table66%2/3
A ||= B
A= $$p{"title"}B= $$p{"name"}
| |||||||||||||||||||||||||||||
| 71 | 24 | delete $p->{name}; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 24 | my $result = $self->api( | |||||||||||||||||||||||||||
| 74 | method => 'post', | ||||||||||||||||||||||||||||
| 75 | content => { properties => $p }, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | 24 | for (qw(spreadsheetId spreadsheetUrl properties)) { | |||||||||||||||||||||||||||
| 78 | 72 | ▶ | $result->{$_} or LOGDIE "No '$_' returned from creating spreadsheet"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | return $self->open_spreadsheet( | ||||||||||||||||||||||||||||
| 82 | id => $result->{spreadsheetId}, | ||||||||||||||||||||||||||||
| 83 | uri => $result->{spreadsheetUrl}, | ||||||||||||||||||||||||||||
| 84 | 24 | ); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 7 | ▶ | sub copy_spreadsheet { | ||||||||||||||||||||||||||
|
Subroutine
copy_spreadsheet: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 7 | my $id = $Spreadsheet_Id; | |||||||||||||||||||||||||||
| 90 | 7 | state $check = signature( | |||||||||||||||||||||||||||
| 91 | bless => !!0, | ||||||||||||||||||||||||||||
| 92 | named => [ | ||||||||||||||||||||||||||||
| 93 | spreadsheet_id => StrMatch[qr/$id/], | ||||||||||||||||||||||||||||
| 94 | _extra_ => slurpy HashRef, | ||||||||||||||||||||||||||||
| 95 | ], | ||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | 7 | my $p = named_extra($check->(@_)); | |||||||||||||||||||||||||||
| 98 | 7 | my $file_id = delete $p->{spreadsheet_id}; | |||||||||||||||||||||||||||
| 99 | 7 | my $file = $self->drive()->file(id => $file_id); | |||||||||||||||||||||||||||
| 100 | 7 | my $copy = $file->copy(%$p); | |||||||||||||||||||||||||||
| 101 | 7 | return $self->open_spreadsheet(id => $copy->file_id()); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 32 | ▶ | sub delete_spreadsheet { | ||||||||||||||||||||||||||
|
Subroutine
delete_spreadsheet: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 32 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 32 | my $id = $Spreadsheet_Id; | |||||||||||||||||||||||||||
| 107 | 32 | state $check = signature(positional => [StrMatch[qr/$id/]]); | |||||||||||||||||||||||||||
| 108 | 32 | my ($spreadsheet_id) = $check->(@_); | |||||||||||||||||||||||||||
| 109 | 32 | return $self->drive()->file(id => $spreadsheet_id)->delete(); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 25 | ▶ | sub delete_all_spreadsheets_by_filters { | ||||||||||||||||||||||||||
|
Subroutine
delete_all_spreadsheets_by_filters: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 25 | ▶ | state $check = signature(positional => [ArrayRef->plus_coercions(Str, sub { [$_]; })]); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | 25 | my ($filter) = $check->(@_); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 25 | my $count = 0; | |||||||||||||||||||||||||||
| 119 | 25 | foreach my $filter (@$filter) { | |||||||||||||||||||||||||||
| 120 | 25 | my @spreadsheets = $self->spreadsheets_by_filter(filter => $filter); | |||||||||||||||||||||||||||
| 121 | 25 | $count += scalar @spreadsheets; | |||||||||||||||||||||||||||
| 122 | 25 | DEBUG(sprintf("Deleting %d spreadsheets for filter '$filter'", scalar @spreadsheets)); | |||||||||||||||||||||||||||
| 123 | 25 | $self->delete_spreadsheet($_->{id}) foreach (@spreadsheets); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 25 | return $count; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub delete_all_spreadsheets { | ||||||||||||||||||||||||||
|
Subroutine
delete_all_spreadsheets: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | 0 | my @names = @_; | |||||||||||||||||||||||||||
| 131 | 0 | @names = map { "name = '$_'"; } @names; | |||||||||||||||||||||||||||
| 132 | 0 | return $self->delete_all_spreadsheets_by_filters(@names); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 32 | ▶ | sub spreadsheets_by_filter { | ||||||||||||||||||||||||||
|
Subroutine
spreadsheets_by_filter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 32 | my $self = shift; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 32 | state $check = signature( | |||||||||||||||||||||||||||
| 139 | bless => !!0, | ||||||||||||||||||||||||||||
| 140 | named => [ | ||||||||||||||||||||||||||||
| 141 | filter => Str, { optional => 1 }, | ||||||||||||||||||||||||||||
| 142 | max_pages => Int, { default => 0 }, | ||||||||||||||||||||||||||||
| 143 | page_callback => CodeRef, { optional => 1 }, | ||||||||||||||||||||||||||||
| 144 | params => HashRef, { default => {} }, | ||||||||||||||||||||||||||||
| 145 | ], | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | 32 | my $p = $check->(@_); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 32 | my $drive = $self->drive(); | |||||||||||||||||||||||||||
| 150 | 32 | my $filter = $Spreadsheet_Filter; | |||||||||||||||||||||||||||
| 151 | 32 | ▶ | $filter .= " and ($p->{filter})" if $p->{filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | return $drive->list( | ||||||||||||||||||||||||||||
| 153 | filter => $filter, | ||||||||||||||||||||||||||||
| 154 | max_pages => $p->{max_pages}, | ||||||||||||||||||||||||||||
| 155 | params => $p->{params}, | ||||||||||||||||||||||||||||
| 156 | 32 | ▶ | ($p->{page_callback} ? (page_callback => $p->{page_callback}) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 5 | ▶ | sub spreadsheets { | ||||||||||||||||||||||||||
|
Subroutine
spreadsheets: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 5 | state $check = signature( | |||||||||||||||||||||||||||
| 164 | bless => !!0, | ||||||||||||||||||||||||||||
| 165 | named => [ | ||||||||||||||||||||||||||||
| 166 | name => Str, { optional => 1 }, | ||||||||||||||||||||||||||||
| 167 | max_pages => Int, { default => 0 }, | ||||||||||||||||||||||||||||
| 168 | page_callback => CodeRef, { optional => 1 }, | ||||||||||||||||||||||||||||
| 169 | params => HashRef, { default => {} }, | ||||||||||||||||||||||||||||
| 170 | ], | ||||||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||||||
| 172 | 5 | my $p = $check->(@_); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 5 | my $name = delete $p->{name}; | |||||||||||||||||||||||||||
| 175 | 5 | ▶ | $p->{filter} = "name = '$name'" if $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 5 | return $self->spreadsheets_by_filter(%$p); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 71 | ▶ | sub drive { | ||||||||||||||||||||||||||
|
Subroutine
drive: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 71 | my $self = shift; | |||||||||||||||||||||||||||
| 181 | 71 | ▶ | $self->{drive} //= DriveApi3->new(api => $self->rest_api()); | ||||||||||||||||||||||||||
|
Condition66%2/3
$$self{"drive"} //= "Google::RestApi::DriveApi3"->new("api", $self->rest_api)
MC/DC0%0/2
$$self{"drive"} //= "Google::RestApi::DriveApi3"->new("api", $self->rest_api)
$$self{"drive"}
"Google::RestApi::DriveApi3"->new("api", $self->rest_api)
Truth table66%2/3
A //= B
A= $$self{"drive"}B= "Google::RestApi::DriveApi3"->new("api", $self->rest_api)
| |||||||||||||||||||||||||||||
| 182 | 71 | return $self->{drive}; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 31 | ▶ | sub open_spreadsheet { Spreadsheet->new(sheets_api => shift, @_); } | ||||||||||||||||||||||||||
|
Subroutine
open_spreadsheet: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub transaction { shift->rest_api()->transaction(); } | ||||||||||||||||||||||||||
|
Subroutine
transaction: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | 6 | ▶ | sub stats { shift->rest_api()->stats(); } | ||||||||||||||||||||||||||
|
Subroutine
stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 1 | ▶ | sub reset_stats { shift->rest_api->reset_stats(); } | ||||||||||||||||||||||||||
|
Subroutine
reset_stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | 308 | ▶ | sub rest_api { shift->{api}; } | ||||||||||||||||||||||||||
|
Subroutine
rest_api: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||