The insider's guide to everything sparqcode
Header image

Streaming Data With Rails 3.1 or 3.2

Posted by Mike on February 4, 2012 in Uncategorized - (0 Comments)

We recently upgraded one of our services to use Rails 3.2. The downside I discovered pretty quickly is that rails broke support for the 3.0 way of streaming data.

The 3.0 way, simplified a bit:

self.response_body =  proc{ |response, output|
  @records.each do |record|
     output.write << CSV.generate_line(record.csv_values)
}

In >= 3.1, the proc method has been deprecated. What are we to do then? Well it's actually pretty simple if we go digging in the Rack source code.

      if body.respond_to? :to_str
        write body.to_str
      elsif body.respond_to?(:each)
        body.each { |part|
          write part.to_s
        }
      else
        raise TypeError, "stringable or iterable required"
      end

Basically, if we set response_body to something that responds to 'each' then rack will automatically send those chunks back to the browser without having to do anything special on our end. In my case I wrote a little streamer class that would take care of it.

class RecordCsvStreamer
   attr_reader :records

   def initialize(record_scope)
      @records = record_scope
   end

   def each
     records.each do |record|
        yield csv_row(record)
     end
   end

   def csv_row(record)
   # however you want to format it
   end
end

Then in my controller I do

class RecordController
  def index
    @records = Record.all

    self.response_body = new RecordCsvStreamer(@records)
  end
end

Happy streaming!

Publishing A Gem With Jewler

Posted by Mike on April 13, 2011 in Uncategorized - (0 Comments)

In order to do this you will need two things:

  1. the jeweler gem
  2. an account with rubygems.org

Jeweler is pretty useful utility for managing your gem’s lifecycle. To get the jeweler gem run

gem install jeweler

To create your project run

jeweler yournewgemname

It will create a new git repository called yournewgemname. You are basically ready to go at this point. You can add your source code files, test your gem, etc at this point.

Publishing

To publish your gem first you have to create a gem verison number

rake version:write

And then to publish your gem to rubygems.org

rake gemcutter:release