Sinatra 1.3 Streaming: Redirecting Processes

Process Management

In ruby, when you want to run and redirect process output, you have many options.
Here are some:

(1) result = `command`
# stdout, stderr if redirected. Buffered until completion.

(2) pipe = IO.popen("command", "r")
# stdout, stderr if redirected. Get chunks while running.

(3) Open3.popen3("command")
# all 3 streams (in, out, err). Get chunks while running.

Streaming with Sinatra

From 1.3.0, sinatra supports streaming. It gets really impressive on an evented server such as Thin, considering that such behavior was reserved either to dedicated frameworks such as cramp and goliath, or even nodejs.

CoffeeScript Fat Arrows

Lets look at the CoffeeScript usage of the fat arrow aka =>:

foo = (hello)=>

Which translates to this javascript code:

var foo;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
foo = __bind(function(hello) {
  return console.log(hello);
}, this);

Backbone Image Gallery - Diving In

I’d like to discuss some interesting issue that surfaced after I’ve published my previous tutorial. Most of these come from HN.

MVC and Selectedness

rgarcia asked:

Not a backbone expert, but how “correct” is it to have a presentation variable like “selected” tied to a backbone model? …

bricestacy said:

…Second, the author makes the claim that Backbone is an MVC and makes things simpler and yet violates that. His way you literally have to go through the view, the collection, the model, a trigger, and back to the view to select a new image.

Note that bricestacy perhaps unknowingly, summarized the way it should work in MVC. You should go to the view, collection, model, and then your view should react back to events from your model!

Image Gallery With Backbone

update: dive into some of the finer details regarding this post here: image gallery aftermath.

Backbone.js. By now you have probably already heard about it, and you’re assuming it can solve all of your problems.

Lets start by clearing up the dust. Backbone is a backbone. Backbone. It is a skeleton, an aggregation of best practices, a great MVC implementation, but at 4.6kb (min) it cannot possibly solve all of your problems.

Also, regarding some criticism with Knockout vs Backbone arguments, many Rails and Rubyists come to backbone - it doesn’t give you Rails-like productivity magically.

Win32 OpenSSL::SSL::SSLError at /auth/facebook/callback

If you’re getting this:

OpenSSL::SSL::SSLError at /auth/facebook/callback
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

It means that open-ssl cannot find your ca_certs (which live under /etc/ssl/certs), which is correct if you’re on windows anyway.

If in rails (using Windows), try disabling verification:

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :facebook, FACEBOOK_KEY, FACEBOOK_SECRET, {:client_options => {:ssl => {:verify => false}}}

You’re going to deploy on Linux any way (and you shouldn’t be developing on an env that isn’t identical to your prod env), but you can at least go on.

ExecJS, CoffeeScript and Ruby

Now that Rails 3.1 can love you back with CoffeeScript, you might wonder how it gets a streamlined ruby-esque experience without falling back to node.js being installed on your windows machine.

Using Rr With Rspec 2.0 and Rails 3.0

Here is a quick guide to get rr together with rspec 2.0 and Rails 3.0 (using Mongoid).

Add to Gemfile:

gem 'rr'


$ bundle install
$ cd vendor/plugins
$ git clone -b rspec2

in your spec_helper.rb:

require 'rspec/rr'
config.mock_with :rr

Now in your controller specs:

describe CommentsController do
  include Devise::TestHelpers
  def mock_comment(stubs={})
	@mock_comment ||= mock_model(Comment, stubs)

  describe "voting" do
	it "should up vote" do
	  @user = new_user
	  @user.aid = 'account'

	  sign_in @user
	  mock(Comment).find("4def8b7a53bfd22388000002") { mock_comment }
	  put :up, :id => "4def8b7a53bfd22388000002"

Vim on Windows

Getting a recent Vim build – some of the plugins require a more recently patched Vim (Command-T). For this, you can find a prebuilt one here.

One of the things people like about Vim is the fact that they customize and fit it to their specific needs. Vim gives you tons of control, at many levels of time investments on your part – be it a quick local vim script tweaking or a proper bundle written either in vimscript or ruby or python or whatever.

Using Graylog2 in Rails via Log4r

If you’re not familiar with graylog2 yet, from the graylog website:

Graylog2 is an open source log management solution that stores your logs in MongoDB. It consists of a server written in Java that accepts your syslog messages via TCP, UDP or AMQP and stores it in the database. The second part is a web interface that allows you to manage the log messages from your web browser.

Although many of the common uses for graylog are for error tracking – and you’ll find a rack adapter that can automatically push your errors to your graylog, I intend to share how i’m using graylog in a different way.

Beyond Tracking Exceptions

I wanted a way to track business events but in a way that wouldn’t require much infrastructure to be written, I also didn’t want the system to stop working in the event of that infrastructure to be down.

Using graylog, I got the server side with its analytics capable front-end for free. I also benefited from the graylog protocol being UDP to assure that my system and the business event tracking system don’t influence eachother.

Having this kind of front-end was certainly a big plus: