Group scheduling — control your events participation with Ruby

Group scheduling — control your events participation with Ruby

8 min read

You’re running an event, but there is limited capacity. You want people to sign up but you don’t want anyone to try to sign up just to realize the event is already fully booked. Group scheduling is the perfect way to control your event participation and using the Nylas Ruby SDK makes it possible.

All you need to do is to create an event, specify its capacity, and let people signing up know how many spots are left for them to join.

Is your system ready?

If you already have the Nylas Ruby SDK installed and your Python environment configured, then continue along with the blog.

Otherwise, I recommend that you read the post How to Send Emails with the Nylas Ruby SDK where we explain the basic setup clearly.

What are we going to talk about?

What’s the application look like?

Group scheduling with available spots

We have the title, date and time of the event. We also have the number of available spots, and we can enter a name and an email address.

Group scheduling adding a participant

When you press submit, you’re registered for the event, and see a confirmation message:

Participant confirmation

A few moments later, you get an invitation in your mailbox:

Event invitation sent

We need all of the information you can provide when you register, so it’s essential to fill out all fields. Let’s make sure we have some form validation in our group scheduling:

Registration validation

When there are no more available spots, registration fails:

Group scheduling with no more available spots

And just to make it clear what happened, we’ll add a confirmation message:

Confirmation of no available spots

Installing the Sinatra package

As we want to create a Ruby web application, our best option is to use Sinatra, one of the most popular Micro Frameworks in Ruby. To install it, run the following commands:

$ gem install sinatra
$ gem install webrick
$ gem install sinatra-flash

Also, we need to install an additional package called Rice Cream. This package will help us get a better print output.

$ gem install ricecream

Once installed, we’re ready to go.

Creating an Event Generator

As we need to create the event, we’re going to create a file called event_generator.rb:

# frozen_string_literal: true

# Load your env variables
require 'dotenv/load'

# Import your dependencies
require 'nylas'
require 'date'
require 'ricecream'

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

# Schedule start time for today at
START_TIME = Time.local(today.year, today.month, today.day, 11, 0, 0)
# Schedule end time for today at
END_TIME = Time.local(today.year, today.month, today.day, 12, 0, 0)

# Create the event
event = nylas.events.create(title: 'Zumba Class with Nyla', location: "Nylas' Office",
                            when: { start_time: START_TIME,
                                    end_time: END_TIME },
                                    calendar_id: ENV['CALENDAR_ID'],
                                    capacity: 3,
                                    notify_participants: true)

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

We can run this file by typing the following on the terminal:

$ ruby event_generator.rb

And the terminal output looks like this:

Running our Group scheduling with RUby

It’s important that we copy the id of the event because we’re going to need it later. Open up your .env file and add the following:

CLIENT_ID = "YourClientId"
CLIENT_SECRET = "YourClientSecret"
ACCESS_TOKEN = "YourAccessToken"
CALENDAR_ID = "YourCalendarId"
EVENT_ID = "YourEventId"

Now, we can also check our calendar to see that the event was generated successfully:

Even generated

Creating the Ruby Group Scheduling project

First, we’re going to create a folder called GroupScheduling, and inside we’re going to create another folder called views and another one called public.

Create a file called group_scheduling.rb with the following source code:

# frozen_string_literal: true

# Import your dependencies
require 'sinatra'
require 'dotenv/load'
require 'nylas'
require 'sinatra/flash'
require 'ricecream'

# Use sessions
enable :sessions

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

# Main page with form
get '/' do
  # Read session variables
  @name = session[:name]
  @email = session[:email]
  event = nylas.events.find(ENV['EVENT_ID'])
  capacity = event.capacity - event.participants.length()
  if capacity < 0
	capacity = 0
  end
  erb :main, layout: :layout, locals: { title: event.title,
                                        day: event.when.start_time.strftime('%A %d, %Y'),
                                        _from: event.when.start_time.strftime('%H:%M %p'),
                                        _to: event.when.end_time.strftime('%H:%M %p'),
                                        capacity: capacity }
end

# When we submit the information
post '/' do
  # Set session variables
  session[:name] = params[:name]
  session[:email] = params[:email]
  # Make sure all fields are filled
  if params[:name] == '' || params[:email] == ''
    flash[:error] = 'You must specify all fields'
    redirect to('/')
  else
	# Get event details
    event = nylas.events.find(ENV['EVENT_ID'])
    # Array for participants
    participants = []
    # Get participants and add the new one
    event.participants.each do |participant|
      participants.push({name: "#{participant.name}", email: "#{participant.email}"})
    end
    participants.push({name: "#{params[:name]}", email: "#{params[:email]}"})
    # Clear session variables
	session[:name] = ""
	session[:email] = ""	    
    begin
	  # Try to update event	
      event.update(participants: participants)
    rescue StandardError => e
      # Capacity has been reached
      erb :group_full, layout: :layout, locals: { title: event.title,
                                                  name: params[:name],
                                                  email: params[:email] }
    else
      # Successfully updated event
      erb :group_confirmation, layout: :layout, locals: { title: event.title,
                                                          name: params[:name],
                                                          email: params[:email] }
    end
  end
end

In the views folder, we need to create four different files. Let’s start with layout.erb:

<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
<title>Zumba Class with Nyla</title>
</head>
<body>
<% flash.keys.each do |type| %>
  <div class="flash bg-green-300 border-green-600 border-b p-4 m-4 rounded grid place-items-center text-red-600 font-bold">
    <%= flash[type] %>
  </div>
<% end %>	
<%= yield %>
</body>
</html>

Then main.erb:

<div class="bg-[#315acb] border-green-600 border-b p-4 m-4 rounded grid place-items-center">
<p class="text-6xl text-center"><%= title %></p>
<p class="text-4xl text-center"><%= day %></p>
<p class="text-4xl text-center">From <%= _from %> to <%= _to %></p>
<br>
<img src="Nyla.png">
<br>
<p class="text-2xl text-center">Currently we have <%= capacity %> available spots.</p>
<br>
<form method="post">
<label for="subject" class="font-bold">Name: </label>
<input type="text" name="name" value="<%= @name %>" ></input>
<br><br>
<label for="body" class="font-bold">Email: </label>
<input type="text" name="email" value="<%= @email %>" ></input>
<br><br>
<button type="submit" class="block bg-blue-500 hover:bg-blue-700 text-white text-lg mx-auto py-2 px-4 rounded-full">Submit</button>
</form>
</div>

We need a confirmation page, so we’ll call it group_confirmation.erb:

<div class="bg-[#315acb] border-green-600 border-b p-4 m-4 rounded grid place-items-center">
	<h1 class="text-3xl">Thanks <%= name %> - <%= email %></h1>
	<p class="font-semibold">You have successfully registered for <%= title %></p>
	<p class="font-semibold">You can now go <a href="/" class="text-green-600">back</a></p>
</div>

Finally, group_full.erb as we need a way to let people know that the event is full:

<div class="bg-[#315acb] border-green-600 border-b p-4 m-4 rounded grid place-items-center">
	<h1 class="text-3xl">Sorry <%= name %> - <%= email %></h1>
	<p class="font-semibold">Sadly, there's no space left for you to join <%= title %></p>
	<p class="font-semibold">You can now go <a href="/" class="text-green-600">back</a></p>
</div>

The last step will be adding an image to the folder public. Here’s the one we’re using:

Nyla being cool

And that’s it! We’re ready to roll.

Running our Group Scheduling application

To run our Ruby Group Scheduling application, we just need to type the following on the terminal window:

$ ruby group_scheduling.rb

The application runs on port 4567 of localhost, so we just need to open our favourite browser and go to the following address:

http://localhost:4567

If you want to learn more about our Calendar APIs, check out the Nylas documentation for the Calendar API Overview.

What’s Next?

Group scheduling with Ruby is fun, but you can do more with the Nylas APIs.

You can sign up Nylas for free and start building!

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.