Only show these results:

Calendar Events with Ruby

The Nylas Ruby SDK is the quickest way to integrate calendars into your app with Ruby and the Nylas Calendar API. Build your calendar integration in 15 minutes.

Python Calendar Create

Create Your Free Developer Account

Ready to build your email integration? Create your Nylas developer account to get started.

Ruby is ranked among the top ten programming languages in terms of growth and popularity worldwide. The Nylas Calendar API connects to all major providers including Google, Exchange, iCal, Outlook, and more.

This guide explains how to use the Nylas Ruby SDK and Calendar API to create events. It covers the following steps:

  1. Setting up your Nylas developer account and getting your API keys.
  2. Installing the Nylas Ruby SDK.
  3. Reading calendar events, creating events and sending notifications to participants, and RSVPing to events.
  4. Exploring the Nylas Calendar API.

Prerequisites

Before you can start using the Nylas Ruby SDK, you'll need to do the following:

  • Sign up for your developer account.
  • Get your developer keys. You need to have your:
    • CLIENT_ID - The Client ID found on the dashboard page for your Nylas application.
    • CLIENT_SECRET - The Client Secret found on the dashboard page for your Nylas application.
    • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas application.
  • Install gem on your development environment.

Install the Nylas Ruby SDK

Run either gem install nylas in your terminal.

Congratulations!

You’re now ready to write code with the Nylas Ruby SDK.

Configure the API Client

At its core, the Nylas Communication Platform is an API client that interfaces with all major email providers. You'll first need to require the nylas package. Create a new instance of this class and pass in the variables you gathered when you received your developer API keys. In the example shown below, replace CLIENT_ID, CLIENT_SECRET, and ACCESS_TOKEN with your values.

nylas = Nylas::API.new(
app_id: "CLIENT_ID",
app_secret: "CLIENT_SECRET",
access_token: "ACCESS_TOKEN"
)

Be Careful with Secrets
Make sure to follow best practices when including authentication secrets like this in your code. Increase security by storing these values as environment variables and accessing them with the dotenv gem.

Read Calendar Events

Many user accounts have access to multiple calendars which you can access using the nylas.calendars object. Available calendars include the default calendar, any calendars the user has created, and any calendars they've added, such as those for other users or for their teams.

The "Emailed Events" calendar is a unique calendar that includes all events the user receives through an email invite. This calendar will be important later when we RSVP to an event.

In the example shown below, nylas.calendars is called to return all calendars associated with the account, and logs each of the calendars’ name, id, and read_only values. The name and id values are useful for identifying and referring to specific calendars. The read_only value is a boolean that lets you know whether the user has write access to the calendar.

calendars = nylas.calendars
calendars.each{ |calendar|
puts(
"Id: #{calendar.id} | "\
"Name: #{calendar.name} | "\
"Description: #{calendar.description} | "\
"Read Only: #{calendar.read_only}"
)
}

Review the API documentation for the calendar endpoints to learn more about what you can do with user calendars and Nylas.

The nylas.events object makes it easy to access information about a user’s events. The example below will return the next five events after the current moment in time on a user’s calendar:

now = Time.now.to_i 
calendar = nylas.calendars.first()
events = nylas.events.where(calendar_id: calendar.id,starts_after: now).limit(5)
events.each{ |event|
puts(
"Title: #{event.title} | "\
"When: #{event.when.start_time} | "\
"Partcipants: #{event.participants.first.name}"
)
}

First, the script defines the variable now, which is a Unix Epoch timestamp represented in seconds. This is the time format Nylas uses for event objects. It also defines calendar as the first calendar that is returned for the account.

Then, .where() is called with a few filters:

  • starts_after - This selects events that occur after the specified time. This example passes the Unix epoch timestamp we stored as the constant now.
  • calendar_id - This is the id for one of the user’s calendars. In this example, the script passes the id for the first calendar that's found for the user account.

Next, the limit argument limits the number of events that are returned to five. Check out our API documentation on pagination to learn more about using limits and offsets to paginate results.

Finally, the script iterates through the list of events and logs the title, time, and list of participants for each of them. Take a look at the API documentation for the event endpoints to learn more about the attributes the event object contains.

Below is the full code example for reading calendars and events from a user account:

require 'nylas'
require 'date'

nylas = Nylas::API.new(
app_id: "CLIENT_ID",
app_secret: "CLIENT_SECRET",
access_token: "ACCESS_TOKEN"
)

# Most user accounts have multiple calendars where events are stored
calendars = nylas.calendars
calendars.each{ |calendar|
# Print the id, name and description of each calendar and whether or not the calendar is read only
puts(
"Id: #{calendar.id} | "\
"Name: #{calendar.name} | "\
"Description: #{calendar.description}"\
"Read Only: #{calendar.read_only}"
)
}

# Get today's date
now = Time.now.to_i
# Get a calendar whose events we want to inspect
calendar = nylas.calendars.first()
# Return the next 5 events on the user's calendar
events = nylas.events.where(calendar_id: calendar.id,starts_after: now).limit(5)
events.each{ |event|
puts(
"Title: #{event.title} | "\
"When: #{event.when.start_time} | "\
"Partcipants: #{event.participants.first.name}"
)
}

Create an Event and Send Invitations

Now, it’s time to create a new event and start adding content to it. An example is shown below:

title = "New Years Party!"
location = "My House!"
description = "We'll ring in the new year in style!"

This example creates new variables to hold the title, location, and description. For more about events object parameters, view our API documentation on events.

Next, let’s set a time for this event and add a participant:

start_time = 1653400800
end_time = 1653404400
participants = [{name: "My Friend", email: "[email protected]"}]

Participants are added as an array of participant subobjects and require an email field. The name field is optional.

Event times are set with UTC timestamps and can be one of four sub-objects that correspond to the time and duration of an event:

  • time
  • timespan
  • date
  • datespan

Check out our API documentation for event subobjects to learn more.

Time.local in Ruby

Time.local is a very effective way to convert UTC timestamps to a human readable format in Ruby.

For CALENDAR_ID, you'll need the ID for the calendar that the user has write access to. Take a look at our quickstart example for reading calendars and events to learn how to find a calendar ID. Then, replace CALENDAR_ID with the appropriate value.

Finally, we’ll save the event to the calendar and notify the participants with the code below:

calendar_id =  "CALENDAR_ID"

event = nylas.events.create(title: title, location: location, description: description,
when: { start_time: start_time, end_time: end_time },
calendar_id: calendar_id, notify_participants: true)

The notify_participants value is set to false by default. If you want to send email notifications to participants, set this to true.

The entire example is shown below:

require 'nylas'

nylas = Nylas::API.new(
app_id: "CLIENT_ID",
app_secret: "CLIENT_SECRET",
access_token: "ACCESS_TOKEN"
)

title = "New Years Party!"
location = "My House!"
description = "We'll ring in the new year in style!"
start_time = "1653400800"
end_time = "1653404400"

participants = [{name: "My Friend", email: "[email protected]"}]

calendar_id = "CALENDAR_ID"

event = nylas.events.create(title: title, location: location, description: description,
when: { start_time: start_time, end_time: end_time },
participants: participants, calendar_id: calendar_id,
notify_participants: true)

RSVP to Calendar Events

Now it’s time to take a look at how to RSVP to events. An example script is shown below:

calendar = nylas.calendars.select { |c| c.name == "Emailed events" }.first
event = calendar.events.where(title: "Party").first
event.rsvp(:yes, notify_participants: true)

This script uses .select to get the id of the Emailed Events calendar that contains all event invites the user has received through an email invite. This is the only calendar for which the RSVP function works.

It also uses .where() to return a filtered list of events, providing the following arguments:

  • title - This is the title of the event to RSVP to.

Check out our API documentation to learn about how filters work.

If you know the id of the specific event that you want to RSVP to, you can also use nylas.events.find({event_id}) to select a specific event, replacing {event_id} with the appropriate value.

Finally, event.rsvp() is called to send a 'yes' response, along with an RSVP message. You can also pass a 'no' or 'maybe' response to .rsvp(). Check out the API documentation for theRSVP endpoint to learn more.

The entire code example to RSVP to calendar events is below:

require 'nylas'

nylas = Nylas::API.new(
app_id: "CLIENT_ID",
app_secret: "CLIENT_SECRET",
access_token: "ACCESS_TOKEN"
)

calendar = nylas.calendars.select { |c| c.name == "Emailed events" }.first
event = calendar.events.where(title: "Party").first
event.rsvp(:yes, notify_participants: true)

Explore the Nylas Calendar API

If you’ve made it this far, congrats! You’ve integrated your first calendar with the Nylas Calendar API. There's plenty more that you can do with it. Take a look at the following resources to learn more about the Nylas Communications Platform capabilities: