A Simple Sinatra Buzzer App

Flatiron School / 13 November 2013

The following is a guest post by Amanda Himmelstoss and originally appeared on her blog. Amanda is currently in the Ruby-003 class at The Flatiron School. You can follow her on Twitter here.

This weekend, I wrote a simple door buzzer app that requires authentication before execution. The point of this is to be able to buzz open our downstairs doors from anywhere (particularly useful when hosting airbnb guests). My boyfriend did all of the methanical work: we got a raspberry pi (which we turned into a server to host the app) and a relay to connect that to the door buzzer (there was sautering and wires involved; I just watched). Through that, it’s possible to trigger the buzzer from the command line of the raspberry pi. We (well, mostly my boyfriend; I, again, just really watched) wrote a simple C command to trigger the buzzer for 5 seconds, long enough for someone to get through both doors downstairs:

I ap­proached this pro­ject not ne­ces­sar­ily as a big chal­lenge, but as a way to drill in the skills we’ve been work­ing on this week into a cool, use­ful pro­ject. This is the second per­sonal pro­ject I’ve worked on dur­ing school, but one that was most re­ward­ing. To see all the com­pon­ents come to­gether to make something that is truly use­ful was super awe­some for me. I had been vaguely en­vi­sion­ing writ­ing a door buzzer app for quite some­time, but I didn’t have the skills yet to ac­com­plish that. It was an em­power­ing to real­ize I was at a point in my learn­ing where I could build something that 4 weeks ago, I wouldn’t have known how to even begin build­ing.

This was an ex­er­cise in Sinatra, Ruby, ERB/HTML, SQL­ite/Se­quel/mi­gra­tions, and gen­eral pro­ject or­gan­iz­a­tion. From scratch I had to set of the pro­gram en­vir­on­ment and config.​ru files, which I can al­ways use some prac­tice with. I won’t bore you with that part, but in­stead get to the meat of the ap­plic­a­tion.

I have only two mod­els in this ap­plic­a­tion: Buzzer and User.

Buzzer only knows how to do one thing right now, which is trig­ger the C com­mand that in turn trig­gers the buzzer it­self:

User is more ad­vanced, be­cause it in­ter­acts with the data­base, which I cre­ated with Se­quel and mi­gra­tion meth­ods. User could have used more ad­vanced au­then­tic­a­tion meth­ods, or even in­teg­rated the sinatra-au­then­tic­a­tion gem, but I thought it was best to keep it simple given the nature of the pro­ject.

The app re­quires you to log in in order to trig­ger the buzzer, but there’s no way to sign up to that. User­names and pass­words can only be entered manu­ally into the data­base. So there­fore, User’s only tasks are to know if in­puted user­names and pass­words cor­res­pond with that is in the data­base:

Like many of my classmates, I’m a bit in love with Sinatra. I know nothing else to compare it to, so it’s hard to give a solid argument why, but I love it for its simplicity and for its ability to teach me how websites are structured. BuzzerApp is rather simple, but it drives home web app structure for me, and gets the job done, which is great.

There are only three pages in the app: login, which authenticates the username and password input fields, success which lets you know you’re in, and failure, which lets you know you don’t have access. It’s simple for a reason: there’s no way for you to buzz in without entering a username and password, because a) there are no cookie sessions, and b) because an instance of Buzzer is only triggered through /login’s POST. The bulk of the app is within login:

Below are the pages rendered by the .erb templates, which could use some styling, although the cats obviously make up for that:

Soon I hope to expand on this project to be able to receive output from the buzzer (to be notified when someone buzzes and then be able to respond with triggering the buzzer). Additionally, I am working on an email notification feature that will notify the administrator each time the buzzer is triggered. My takeaway from this, besides a super useful application, is that sometimes the best tools for a project are the most simple ones. Also, that Sinatra is awesome. 🙂

A Look at Symbol#to_proc Previous Post Mta_status Gem Next Post