How to manage calendar events with the Nylas Ruby SDK

How to manage calendar events with the Nylas Ruby SDK

7 min read

Digital calendars are essential for everyone. Whether it’s for scheduling, organizing, or attending events, calendars are critical for companies and individuals alike. Nylas makes it possible to bring calendaring features to your application with our universal Calendar API that works across most major providers.

To make things even easier for Ruby developers, we offer the Nylas Ruby SDK. Today we’re going to review how to manage calendar events with Ruby using the SDK.

Is your system ready?

If you already have the Nylas Ruby SDK installed and your environment is configured, go ahead and proceed to the next section.

Otherwise, I would recommend you to read the post How to Send Emails with the Nylas Ruby SDK where the basic setup is explained.

Reading calendars

Now we get to write some code! For my code editor, I’m going to use Geany, however, you can use the IDE of your choice.

Let’s start by reading a list of our calendars, looking at calendar id, name, and description. We’re going to call this file ReadCalendars.rb:

# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Access and print all calendars information
calendars = nylas.calendars
calendars.each{ |calendar|
    puts(
        "Id: #{calendar.id} | "\
        "Name: #{calendar.name} | "\
        "Description: #{calendar.description}"
        )
}

We can run this script from the terminal by using:

$ ruby ReadCalendars.rb
Reading Calendars

As we can see, we’re going to get back a list of all the available calendars for our account.

Note that each of the calendars returned has an id. You’ll need at least one of those ids in the following steps.

Reading calendar events

One advantage of being able to read a Calendar is being able to see events in our schedule, so that’s what we’re going to do now. We’re going to choose a Calendar and read its related events.

For that, we’re going to create a new file called ReadEvents.rb (be sure to add a calendar id from the previous section to the code below):

# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Access and print all calendars information
events = nylas.events.where(calendar_id: "<YOUR_CALENDAR_ID>")
events.each{ |event|
    if event.when.start_time
        print(
            "Title: #{event.title} | " \
            "Start: #{event.when.start_time} | " \
            "End: #{event.when.end_time} | " \
            "Participants: ["
            )
    else
        print(
	"Title: #{event.title} | " \
	"Date: #{event.when.date} | " \
	"Participants: ["
	)
    end
    event.participants.each{ |participant|
        print (
            "{comment: #{participant.comment}, " \
            "email: #{participant.email}, " \
            "name: #{participant.name}, " \
            "status: #{participant.status}}"
	)
        }
        puts("]\n")
}

We can run this script from the terminal by using:

$ ruby ReadEvents.rb
Reading Events

Reading events is important, but maybe there’s something more important, which is the ability to create events.

Creating calendar events

Let’s make a new script that creates a new event on our calendar. We’re going to name this file CreateEvent.rb (be sure to add your calendar id from the previous section, as well as your email address, to the code below):

# Import your dependencies
require 'dotenv/load'
require 'nylas'
require 'date'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Get today’s date
today = Date.today

# Access and print all calendars information
event = nylas.events.create(title: "Let's learn some Nylas Ruby API!", location: "Blag's Den!", when: { start_time: Time.local(today.year, today.month, today.day, 10, 0,0).strftime("%s"), end_time: Time.local(today.year, today.month, today.day, 12, 0,0).strftime("%s") }, participants: [{name: "Blag", email: "<your_email>"}], calendar_id: "<your_calendar_id>", notify_participants: true)

if event.id
	puts 'Event created successfully'
else
	puts 'There was an error creating the event'
end

We can run this script from the terminal by using:

$ ruby CreateEvent.rb

And we can see that the event was successfully created.

Event invitation

We will get an email notification and it will appear on our calendar as well.

Event Invitation on Calendar

We have reviewed how to create an event rather quickly.

Updating calendar events

Sometimes, we might realize that we forgot to add something to the invite, or maybe the time of our event has changed. We’ll need to update the event we just created.

In order to do that, we need to have the event id. If we modify our ReadEvents.rb script slightly, we can easily get the id:

# Import your dependencies
require 'dotenv/load'
require 'nylas'
require 'date'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Get today’s date
today = Date.today
# Today’s date at 12:00:00 am
_START = Time.local(today.year, today.month, today.day, 0, 0, 0).strftime("%s")
# Today’s date at 11:59:59 pm
_END = Time.local(today.year, today.month, today.day, 23, 59, 59).strftime("%s")

# Access and print all calendars information
events = nylas.events.where(calendar_id: "<your_calendar_id>", starts_after: _START,ends_before: _END)
events.each{ |event|
    if event.when.start_time
        print(
            "Id: #{event.id} | " \
            "Title: #{event.title} | " \
            "Start: #{event.when.start_time} | " \
            "End: #{event.when.end_time} | " \
            "Participants: ["
            )
    else
        print(
            "Id: #{event.id} | " \
	"Title: #{event.title} | " \
	"Date: #{event.when.date} | " \
	"Participants: ["
	)
    end
        event.participants.each{ |participant|
            print (
	    "{comment: #{participant.comment}, " \
	    "email: #{participant.email}, " \
	    "name: #{participant.name}, " \
	    "status: #{participant.status}}"
           )
        }
        puts("]\n")
}

We can run this script from the terminal by using:

$ ruby ReadEvents.rb
Reading Calendar Events with Id

Once we have the corresponding event id we can create a new script and call it UpdateEvent.rb, where we use the event id to update the event:

# Import your dependencies
require 'dotenv/load'
require 'nylas'
require 'date'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Get today’s date
today = Date.today
# Today’s date at 12:00:00 am
_START = Time.local(today.year, today.month, today.day, 12, 0, 0).strftime("%s")
# Today’s date at 11:59:59 pm
_END = Time.local(today.year, today.month, today.day, 14, 0, 0).strftime("%s")

# Get  the event information
event = nylas.events.find("<your_event_id>")

# Update the event information
event.update(when: { start_time: _START, 
		           end_time: _END })

if event.id
	puts 'Event updated successfully'
else
	puts 'There was an error creating the event'
end

We can run this script from the terminal by using:

$ ruby UpdateEvent.rb

We get an email about the event update:

Updating Calendar Event Invitation

And the event in the calendar is going to be updated as well:

Updated Event invitation on Calendar

As we’ve seen here, updating an event is very similar to creating one. The Nylas Ruby SDK is flexible enough to make working with events simple and straightforward.

Deleting events

Just for the sake of completion and taking advantage of the fact that we have the event id, let’s create a quick script to delete events.

We’re going to create this file called DeleteEvent.rb, using the event id:

# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Get the event information
event = nylas.events.find("<your_event_id")

# Delete the event
event.destroy

# Verify that the event was deleted
begin
	event = nylas.events.find("<your_event_id>")
rescue => error
	puts 'There was an error deleting the event'
else
	puts 'Event deleted successfully'
end

… and run the file:

$ ruby DeleteEvent.rb

We will get an email notification and the event will be removed from our calendar

Cancelling an Calendar Event invitation

And that’s it! As you can see, by using the Nylas Ruby SDK, working with calendar events becomes an easy task. If you want to learn more, visit our Documentation Page.

Related resources

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.

Beyond APIs: Designing elegant, smart Web Components

Dive into the world of smart Web Components for advanced API integration solutions. Design elegant, customizable elements to elevate user experiences.

How to integrate a React calendar component to streamline scheduling

Elevate your app’s scheduling with a React calendar component and seamlessly integrate with Nylas for efficient calendar management.