Only show these results:

Events with Ruby

The Ruby Python 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 10 on most of the indices that measure the growth and popularity of programming languages worldwide. The Nylas Calendar API connects to all major providers including Google, Exchange, iCal, Outlook, and more. Also, our Ruby SDK makes it simple to create events in user accounts.

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

  1. Setup your Nylas developer account and get your API keys
  2. Install the Nylas Ruby SDK
  3. Read calendar events, create an event and send notifications to participants, and RSVP to events
  4. Explore the Nylas Calendar API

Prerequisites

Before you can start using the Nylas Ruby SDK, make sure you have done 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 App.
    • CLIENT_SECRET - The CLIENT SECRET found on the dashboard page for your Nylas App.
    • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas App.
  • Ensure that gem installed on your development environment.
  • Create a virtual environment to install Nylas.

Install the Nylas Ruby SDK

On the terminal window, run gem install nylas.

Congrats! 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 of the major email providers. First, require the nylas package, and create a new instance of this class, passing the variables you gathered when you got your developer API keys. In the following example, 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"
)

Take care with your secrets
It’s not best practice to include secrets like this in your code. A more secure way to provide these values is to store them as environment variables and access them with the dotenv gem.

Read Calendar Events

Many user accounts have access to multiple calendars; you can access these using the nylas.calendars object. Available calendars include the default calendar, any calendars the user has created, and any calendars they have 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 by email invite; this calendar will be important later when we RSVP to an event.

In the following example, nylas.calendars is called to return all calendars associated with the account, and log each of the calendars’ id, name, description, and read_only values. The name and id values are useful for identifying and referring to specific calendars, and read_only 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 reference for the calendar endpoints to learn more about what Nylas enables you to do with user calendars.

The nylas.events object makes it easy to access information about a user’s events. The next example will return the next 5 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. If also defines calendar as the first calendar that is returned for the account.

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

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

Next, the limit argument limits the number of events that are returned to 5. Check out the API reference for 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 reference for the event endpoints to learn more about the attributes the event object contains.
Here 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:

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

This example creates a new variables to hold the title, location, and description. For more information about events object parameters, view the Events API reference.

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

start_time = 1653400800
end_time = 1653404400
participants = [{name: "My Friend", email: "my.friend@example.com"}]

Participants are added as an array of participant subobjects and require an email field, while name 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, or datespan. Take a look at the reference documentation for event subobjects to learn more. Pro tip: Time.local is a very effective Ruby way for converting UTC timestamps to a human readable format.

Finally, we’ll create the event to the calendar and notify the participants. For this, you will need the ID for the calendar that the user has write access to. Take a look at the quickstart example for reading calendars and events to learn how to find a calendar ID. Then, in the following example, replace CALENDAR_ID with the appropriate value.

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, this must be set to true.

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: "my.friend@example.com"}]

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)

Respond to Event RSVP

Now it’s time to take a look at how to RSVP to events.

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 by email. 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 - the title of the event to RSVP to.

Head to the API reference 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 'no' or 'maybe' to .rsvp(). Check out the API reference for the RSVP endpoint to learn more.
Here is the entire code example to RSVP to calendar events.

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 created your very first calendar event with the Nylas Calendar API! There is plenty more that you can do with it; take a look at the following resources to learn more about the Nylas Communications Platform capabilities.