Google-RestApi

 view release on metacpan or  search on metacpan

lib/Google/RestApi/SheetsApi4/Spreadsheet.pm  view on Meta::CPAN

package Google::RestApi::SheetsApi4::Spreadsheet;

our $VERSION = '2.2.3';

use Google::RestApi::Setup;

use Cache::Memory::Simple ();

use aliased 'Google::RestApi::SheetsApi4';
use aliased 'Google::RestApi::SheetsApi4::Worksheet';
use aliased 'Google::RestApi::SheetsApi4::RangeGroup';
use aliased 'Google::RestApi::SheetsApi4::RangeGroup::Tie';

use parent "Google::RestApi::SheetsApi4::Request::Spreadsheet";

sub new {
  my $class = shift;

  my $qr_id = $Google::RestApi::SheetsApi4::Spreadsheet_Id;
  my $qr_uri = $Google::RestApi::SheetsApi4::Spreadsheet_Uri;
  # pass one of id/name/title/uri and this will work out the others.
  state $check = signature(
    bless => !!0,
    named => [
      sheets_api => HasApi,
      # https://developers.google.com/sheets/api/guides/concepts
      id         => StrMatch[qr/^$qr_id$/], { optional => 1 },
      name       => Str, { optional => 1 },
      title      => Str, { optional => 1 },
      uri        => StrMatch[qr|^$qr_uri/$qr_id/?|], { optional => 1 },
      cache_seconds => PositiveOrZeroNum, { default => 5 },
    ],
  );
  my $self = $check->(@_);

  $self = bless $self, $class;
  $self->{name} ||= $self->{title};
  delete $self->{title};

  $self->{id} || $self->{name} || $self->{uri} or LOGDIE "At least one of id, name, or uri must be specified";

  return $self;
}

# take the passed uri from worksheet/range/rangegroup etc, and tack on the spreadsheet id,
# then pass it up to G::R::SheetsApi4 which will tack on the endpoint.
sub api {
  my $self = shift;
  state $check = signature(
    bless => !!0,
    named => [
      uri     => Str, { default => '' },
      _extra_ => slurpy HashRef,             # we'll just pass the params/content etc up for processing.
    ],
  );
  my $p = named_extra($check->(@_));
  $p->{uri} = $self->spreadsheet_id() . $p->{uri};
  return $self->sheets_api()->api(%$p);
}

# work out the id from the uri or the name/title.
sub spreadsheet_id {
  my $self = shift;

  if (!$self->{id}) {
    if ($self->{uri}) {
      my $qr_id = $Google::RestApi::SheetsApi4::Spreadsheet_Id;
      my $qr_uri = $Google::RestApi::SheetsApi4::Spreadsheet_Uri;
      ($self->{id}) = $self->{uri} =~ m|^$qr_uri/($qr_id)|;   # can end with '/edit'
      LOGDIE "Unable to extract a sheet id from uri" if !$self->{id};
      DEBUG("Got sheet ID '$self->{id}' via URI '$self->{uri}'.");
    } else {
      my @spreadsheets = $self->sheets_api()->spreadsheets(name => $self->{name});
      LOGDIE "Sheet '$self->{name}' not found on Google Drive" unless @spreadsheets;
      LOGDIE "More than one spreadsheet found with name '$self->{name}'. Specify 'id' or 'uri' instead."
        if scalar @spreadsheets > 1;
      $self->{id} = $spreadsheets[0]->{id};
      DEBUG("Got sheet id '$self->{id}' via spreadsheet list.");
    }
  }

  return $self->{id};
}

# when 'api' is eventually called, id will be worked out if we don't already have it.
# the resolving of id/name/title/uri is deferred until the first api call.
sub spreadsheet_name {
  my $self = shift;
  $self->{name} ||= $self->properties('title')->{title}
    or LOGDIE "No properties title present in properties";
  return $self->{name};
}
sub spreadsheet_title { spreadsheet_name(@_); }

# see above routine.
sub spreadsheet_uri {
  my $self = shift;
  $self->{uri} ||= $self->attrs('spreadsheetUrl')->{spreadsheetUrl}
    or LOGDIE "No spreadsheet URI found from get results";
  $self->{uri} =~ s[/(edit|copy)$][]; # this isn't necessary but keeps things cleaner.
  return $self->{uri};
}

# return one of the attributes of the spreadsheet.
sub attrs {
  my $self = shift;



( run in 0.633 second using v1.01-cache-2.11-cpan-5a3173703d6 )