Frank on Fire: Getting Started with Sinatra and Ember-CLI

Here at Bendyworks, we've noticed Ember.js's growing adoption in the front-end community and have found that it strikes a good balance between flexibility and adherence to battle tested architectural practices.

Ember-CLI takes things one step further by standardizing on a certain set of build tools and application structure, allowing you to get on with developing your application rather than writing boilerplate.And the announcement in the Roadmap to Ember 2.0 that Ember-CLI will become a "first class part of the Ember experience" is all the more reason to start using it now.

Although Ember is impressively backend agnostic, the de-facto choice for serving Ember apps and providing APIs has generally been nodeJS. Using one language on both client and server, but in this post I'm going to talk about a different option: using Ruby and Sinatra as the backend.

I won't spend too long arguing for the value of Sinatra vs Node for the backend, but I think there are some worthy points in Ruby's favor. First and foremost, Ruby has a diverse set of tools for interacting with databases of all kinds, and has particularly good support for SQL databases courtesy of Rails and ActiveRecord. While there are ORM options for node, none have the maturity (or, in my opinion, elegance and ease of use) of the options on the Ruby side. Second, Ruby makes it very easy to get up and running with a prototype application, and Sinatra takes this attitude and applies it to web development. It also has a huge ecosystem of mature and well supported libraries ("gems" for the non-rubyist) that can be used to quickly add features to any application, making prototyping even easier. In short, the language has incredible whipupitude, which is just what we need for building a simple backend for an Ember application.

Setup

Before getting started on our app, we'll need to install a few prerequisites:

Once these are set up, we'll be able to manage installing libraries and any future dependencies via Bower and npm.

Installing npm can be a little tricky, but since it comes with Node.js I'd suggest just installing that if you're unsure where to start.

Once you have npm, the rest of the dependencies are easy to install:

Bower

npm install -g bower

Ember-CLI

npm install -g ember-cli

Ruby

If you're following along on a Mac, you already have Ruby installed, so you can skip this step! If not, you have a few different options to install Ruby. One good choice is RVM, which can be installed along with the current stable version of MRI Ruby by running

\curl -sSL https://get.rvm.io | bash -s stable

Bundler

Bundler can be installed as a Ruby gem

gem install bundler

A Star Is Born - Generating a new Ember application

Once all the dependencies are installed, its time to generate a skeleton for our application! For the purposes of this tutorial, lets call our app newsboy_blues, and generate it using the "ember new" command

ember new newsboy_blues

Now you can switch into the new newsboy_blues directory and check your setup by running ember server and visiting your application at http://localhost:4200/. You should see "Welcome to Ember.js" if everything is set up properly.

Sinatra Takes The Stage - Setting up the server

Next we'll create a basic Sinatra application to serve up our application. For now, this may seem like extra work compared to using the node server that comes with ember-cli, but we'll begin to see the advantages of using Ruby on the server when it comes time to access the database.

and another file called Gemfile that we'll use to install Ruby libraries

# Gemfilesource'https://rubygems.org'gem'sinatra'

Then have bundler install Sinatra by running

bundle install

Finally, we'll need to compile the javascript for our application. Although our Sinatra application won't do this automatically, ember-cli includes a command to watch for changes to our project and automatically avenge when they happen. Open up a new terminal, and run

ember build --watch

Now all we have to do is start the application

ruby server.rb

And we can again see "Welcome to Ember.js" when visiting http://localhost:4567. Note the change of port; by default Sinatra runs on 4567.

Start Spreadin' The News - Building the client using Fixtures

Now that all the groundwork is laid, we can really begin building a news reader. In the end we'll be able to pull news from any RSS feed using our server, but to begin we'll just use fixture data so we can focus on building the interface.

To get started we can use ember-cli's generate command to generate scaffolds for the routes, models, and templates we'll be creating. First we generate a model we can use to represent a news story

ember generate model story

Next we generate a route which we'll use to link our story model to a view template

ember generate route stories

we'll also need to create our own application adapter to use fixture models

ember generate adapter application

and an index route so we can redirect to our main 'stories' route

ember generate route index

With all of this finished, we can fill in some simple behavior for our nascent news app. First we'll change app/adapters/application.js to tell ember to use the FixtureAdapter adapter for loading models

Last but certainly not least, we'll edit the 'stories' template in
app/templates/stories.hbs to define how our stories are displayed. Note the use of the 'triple handlebar' for story_content to prevent Handlebars from HTML escaping the contents of a story, which usually has HTML formatting. A word of warning though; mixing in content from unknown sources is a serious security issue. Eventually you'd want to sanitize the contents of each story on the server side, but since we're just using fixtures that we wrote there's no need to worry about it for now.

And now we can see our exciting news stories displayed at http://localhost:4567! If you don't see any change, make sure you're running ember build --watch, and that its output isn't showing any errors.

Something Wonderful - Using gems to make Database and RSS access a breeze

Up until this point we haven't been using our Sinatra app for anything other than serving up the main Ember application. Since we had to go through a few (small) contortions to get this right, you may be wondering where Sinatra adds value in this application. The answer is in Ruby's excellent library support for all sorts of server side operations. In this case, database access and RSS parsing.

For collecting news stories to display in our ember app, we'll use the Feedjira gem, which provides a simple interface for fetching and parsing RSS feeds. To store and persist our stories, we'll be using the sinatra-activerecord gem with sqlite. This will let us get up and running faster, but if we ever want to handle large numbers of stories or deploy our application to Heroku it will be fairly simple to switch to PostgreSQL.

To get started, add the 'feedjira' and 'sinatra-activerecord' gems to your Gemfile, as well as 'sqlite3' and 'rake' which are required by sinatra-activerecord.

Sinatra won't generate a model file for us like Rails does, so let's create an empty one in lib/models/story.rb.

# lib/models/story.rbclassStory<ActiveRecord::Baseend

You'll also need to load the model by adding require_relative 'lib/models/story' to your main server.rb file

Then run rake db:migrate, and we're ready to populate our stories table with data from an RSS feed! Although a fully featured news reader would probably have some concept of a "Feed" which could be used to organize stories, our simple ember app only models Stories, so we won't bother creating a model for Feeds.

Since we already have a Rakefile, lets just add another task to fetch all new stories from a feed for now.

Story The Iconic Madison – Free Icon Set added!
Story 2014 Rails Rumble added!
Story The Old and the New: SOAP and Ember.js added!
Story Two Keynote tips that everyone should know added!
Story Transducers: Clojure’s Next Big Idea added!
Story Why We Can’t Wait for Madison+ Ruby added!
Story BendyConf: Tech Education for the Rest of Us added!
Story Tessel: A First Look at JavaScript on Hardware added!
Story Externally Embedding Ember added!
Story BendyConf: A Paean To Plain Text added!

Indicating that everything worked, and some stories have been added to our database.

Can't We Be Friends? - Integrating server and client

All that's left to do now is set up our Ember application to fetch and display these stories. Because we've already developed the front end using fixtures, we don't need to worry about any more template changes or controller logic.

First, add a route to server.rb that will return the stories in our database

Note that this new route is "namespaced" under /api namespace, to avoid conflicting with the /stories route on the Ember side.

To get Ember to fetch data from the server instead of from the fixtures, just remove the fixtures, change the application adapter from a FixtureAdapter to the built in ActiveModelAdapter, and place the new adapter under the api namespace.

Now your stories will be loaded and displayed when you visit localhost:4567! (Don't forget to restart server.rb and re-run ember build)

Don't Like Goodbyes - What to work on next

Congratulations, you have a working Ember/Sinatra application that can fetch and display news stories from any RSS feed in the World! The finished code for this tutorial is also available on Github. But its not very feature rich, and it certainly won't be winning any design awards. Below are some ideas of where to go next in developing the app. Some may form the basis of future blog posts, and hopefully others will inspire readers to build their own amazing news reading tools.

Improve the interface

Our interface is about as bare bones as could be. We could start by adding a route and template to display individual stories at the very least, but adding some basic design touches or starting to style our page with Bootstrap would be a good goal.

Read and unread stories

Once you've read a story, it'd be nice to mark it as read and not see it again. The first step would be adding the necessary fields to the Stories table in our database, and the Story model in our Ember application. After that, Ember Data's ActiveModelAdapter will automatically generate a standard RESTful PUT request when we save a story model on the client side, and all we'll have to do is add a put "/stories" route to our Sinatra server.

Automatically fetch new stories

Having to run the rake fetch_stories command every time we want to get new stories from an RSS feed. You could just hard code a list of feed urls and fetch them each time, but a more robust solution would be to create a new "Feed" model and database table, using it to remember which feeds to update. You could then regularly schedule updates using cron or the excellent whenever gem.

Pagination

Once you get more than a few stories in your database, sending them all from the server and rendering each one in the DOM will waste resources, especially if you're only there to view the first few stories. Although Ember doesn't have any built in pagination functions, the ember-cli-pagination addon is worth investigating. I've also had luck with simply managing a page variable in the ApplicationController, and using the will_paginate gem to handle things on the server side.

Switching to Postgres and deploying to Heroku

We used sqlite for this tutorial since its easy to setup and use from Sinatra, but as the number of stories in our database grows we'll want to switch to a more performant SQL software. We're big fans of PostgreSQL here at Bendyworks, and switching to it from sqlite isn't particularly difficult once you have it installed and configured. Switching to Postgres gives us another unexpected benefit however; its the last hoop to jump through before we can easily deploy our application to Heroku using the Ember-CLI buildpack. Then you can read your own personalized news feed from any internet connected device! (although you may want to start thinking about authentication before adding features that update the database)

Take the news into your own hands

Although building a news reader with Ember-CLI has been a great way to learn more Ember and experiment with serving up the assets and API through Sinatra, my original motivation was at least in part to start building a news reader that won't get shut down or bought and integrated into an app I like less. Beyond that, I wanted the ability to score and sort stories by a user controlled 'interestingness' metric. I've also always wanted to build a news reader and call it Bottomless Soup Bowl in honor of this ig nobel prize. It's still pretty experimental, but if you'd like to see a demo of what the app could look like after some of the features discussed here are added, check out bsb.herokuapp.com. Any input, suggestions, or Pull Requests are also very welcome on the wstrinz/bsb Github repository.