Skip to content

Commit

Permalink
Merge pull request #26 from ChrisThompsonTLDR/dev/calendar-create
Browse files Browse the repository at this point in the history
Adding calendar creation
  • Loading branch information
lanlin authored Nov 17, 2020
2 parents 21fa92d + 91fccda commit ef264ab
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Calendars/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nylas\Calendars;

use DateTimeZone;
use Nylas\Utilities\API;
use Nylas\Utilities\Helper;
use Nylas\Utilities\Options;
Expand Down Expand Up @@ -69,6 +70,33 @@ public function getCalendarsList(array $params = []): array

// ------------------------------------------------------------------------------

/**
* add calendar
*
* @param array $params
*
* @return array
*/
public function addCalendar(array $params): array
{
$rules = $this->addCalendarRules();

$accessToken = $this->options->getAccessToken();

V::doValidate(V::keySet(...$rules), $params);
V::doValidate(V::stringType()->notEmpty(), $accessToken);

$header = ['Authorization' => $accessToken];

return $this->options
->getSync()
->setFormParams($params)
->setHeaderParams($header)
->post(API::LIST['calendars']);
}

// ------------------------------------------------------------------------------

/**
* get calendar
*
Expand Down Expand Up @@ -109,4 +137,22 @@ public function getCalendar($calendarId): array
}

// ------------------------------------------------------------------------------

/**
* rules for add calendar
*
* @return array
*/
private function addCalendarRules(): array
{
return
[
V::key('name', V::stringType()->notEmpty()),
V::keyOptional('description', V::stringType()->notEmpty()),
V::keyOptional('location', V::stringType()->notEmpty()),
V::keyOptional('timezone', V::in(DateTimeZone::listIdentifiers())),
];
}

// ------------------------------------------------------------------------------
}
24 changes: 24 additions & 0 deletions tests/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,28 @@ public function testGetCalendar(): void
}

// ------------------------------------------------------------------------------

public function testAddCalendar(): void
{
$params = $this->getCalendarInfo();

$data = $this->client->Calendars()->Calendar()->addCalendar($params);

$this->assertArrayHasKey('id', $data);
}

// ------------------------------------------------------------------------------

/**
* @return array
*/
private function getCalendarInfo(): array
{
return [
'name' => 'Test Calendar',
'description' => 'This is a test calendar.',
'timezone' => 'America/New_York',
'location' => 'Front Conference Room',
];
}
}

0 comments on commit ef264ab

Please sign in to comment.