Toilet Vacant: A Rails app which notifies you via Slack
Tags: webdev, ruby, rails, slack
Published: 2019-02-15 11:06:17

---
title: Toilet Vacant: A Rails app which notifies you via Slack
published: true
tags: web-development,ruby,ruby-on-rails,slack
canonical_url: https://revs.runtime-revolution.com/toilet-vacant-a-rails-app-which-notifies-you-via-slack-f07d1484a83
---

![](https://cdn-images-1.medium.com/max/1024/0*bgo3ArdsI5ozVsWf)<figcaption>Photo by <a href="https://unsplash.com/@gabor?utm_source=medium&amp;utm_medium=referral">Gabor Monori</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

Every day, there’s that time when you need to go to the toilet. You get off your chair, start walking towards a door with a drawing of a gentleman or a lady and hope that it is vacant. If your prayers were answered, you can enter and do what you got to do.

But here, at our office, sometimes you see people (me included), walking around the office. They are not walking because it is healthy. No. They are walking because they are looking for a toilet that is vacant. And sometimes, in the worst case scenario, they go back to their place without doing their business.

It happens to me a lot and it really grinds my gears (insert Peter Griffin meme here)!

If only there was a way we could be notified on Slack that a toilet was vacated…

### **The Solution**

The true purpose of this post is not to present a complex solution to check if a real toilet is vacant. It is to show you how to use Slack’s Incoming Webhooks API on your Rails application, in a funny way.

The full app is available at [https://github.com/jpac-run/toilet-vacant-app](https://github.com/jpac-run/toilet-vacant-app) for you to checkout and see the application working. There is a README file with instructions on how to run it. Because of this, I won’t get into too much details for some parts. The only outstanding requirement you’ll need is to create a Slack application and permit the Incoming Webhooks. You can see how in this link: [https://api.slack.com/incoming-webhooks](https://api.slack.com/incoming-webhooks).

Ok, now for some code.

You know the drill: create a fresh Rails application and add httparty and dotenv-rails gems to your Gemfile.

Create a ToiletsController with the index method and set it to be the application root in routes.rb.

In the application root folder, create a file called .env. This file is used to store your environment variables.

After you go through the steps in the Slack Incoming Webhooks link mentioned above, you’ll now have a special URL to send a message via the API, that we will store in a .env file, along with the channel name, like this:

{% gist https://gist.github.com/jpac-run/1fd2fbc42e5bed1632c0f60f0be4aa80 %}

Again, inside your routes.rb, add two new routes:

{% gist https://gist.github.com/jpac-run/ff8d8863e0b8c8dd779ed43e14cb7889 %}

And define the index and update actions inside ToiletsController.

{% gist https://gist.github.com/jpac-run/491a5129bae6168f0c53858f2e732c6d.js %}

But wait… we need some toilets!!! So, on a command line, type:

```
rails g model Toilet toilet_number:integer, open:boolean
```

and after your toilet model is created, set the open column with a default value of true inside the migration.

With a toilet model, you can now create thrones — I mean, more toilets! You can either use the seeds.rb file or just run the Rails console and create some.

Create a view to index all the toilets and for each one add a button/link that will call either the get\_in or the get\_out actions, depending on the state of the toilet. Something like this:

{% gist https://gist.github.com/jpac-run/31ef5b7da9200794a0c2c3db3c4ab925 %}

Yeah, it’s not pretty, but neither is your toilet when you use it…

Ok, sorry. Each of the actions will change the state of the selected toilet (change the open value to true or false) and (here comes the cool part) it will trigger an event that takes care of sending a notification to the Slack channel.

To do this, we will use the ActiveSupport::Notifications to treat the events logic. Create an events.rb file inside config/initializers and subscribe your app to an event that will send the notification:

{% gist https://gist.github.com/jpac-run/a6120453d99096cb1221b2133ab91e16 %}

Every time the occupied.toilet event is triggered, the code inside the subscribe block is executed. This block uses the httparty gem to call the Slack API. Inside the body of the request, you have to specify the name of the channel and your username. The field text contains the message you want to appear on the channel. The :toilet: and :poop: strings will appear as an emoji!

![](https://cdn-images-1.medium.com/max/596/1*VId3W8CrS-0x32vuVmfGvw.png)

You can send a more custom message, just follow the API documentation.

Now try to use this same logic to create an event that notifies the channel when the toilet is vacant!

With this, I hope you got motivated to develop your own notifier for your office toilets and avoid getting up from your chair and go around the office looking for an empty little room to do what you got to do.

_I’m currently a Ruby-on-Rails developer at Runtime Revolution that still likes to play the good old Pokemon games for the GameBoy. Go check our_ [_website_](https://www.runtime-revolution.com/) _to know us more. Oh and we’re hiring!_

* * *