Feed-icon-14x14

Using Amazon S3 in Rails Applications

Posted on April 03, 2010 at 04:15 pm

After reviewing Vim for Rails Development screencast some time ago, I received another, the second screencast of Codeulate Screencasts which is titled: Using Amazon S3 in Rails Applications. This screencast presents how to build a file upload application with Rails and Amazon S3 including some tips and gems you may find useful while building that functionality.

Here I will review the screencast, which you may want to buy for $9.

Before watching the screencast I would say that I haven’t got any experience using Amazon S3 previously. So, I guessed that if I need to build a Rails application using Amazon S3, I would have to spend some time researching the tools that I need, then read documentations on how stuff works, then maybe google for some approaches and finally implement the requested functionality. And, all the way along fight with all those little details that can really consume some time while figuring them out.

But, it’s all here, well packed in 37 minutes long screencast. After wathing it, I learned all details to start coding an application right now and upload data on Amazon S3.

Another thing I like about the screencast is that, it feels that the knowledge in the screencast is extracted from building a real world application using Rails and Amazon S3. There are tips on having good design all the way along while building the application, including gotchas using Amazon S3 and other details.

Screencast begins with an overview of Amazon S3 (Simple Storage Service), where you can actually CRUD (Create Read Update and Delete) unlimited number of files from 1 byte to 5 gigabytes of data each. Amazon S3 has it’s own terminology of buckets and objects for something like folders and files in the file system terminology. And, it gives you appropriate urls from where you can access the uploaded files.

Then screencast goes into details on how to use AWS::S3 gem written by Marcel Molina which abstracts all API details using REST architecture of Amazon S3, and gives back ruby objects which are easy to work with in ruby code. It presents how to create new buckets and add new objects to those buckets using script/console.

Main part of the screencast is building the actual file upload application in TDD (Test Driven Development) style using fakeweb and mocha gems in the testing environment. After implementing the file upload to Rails application and then to Amazon S3, the author presents how to improve the usability of the application by using Tobias Lütke’s gem delayed_job. This gem uploads the files in background process not leaving the user to wait while file uploads finishes (wait two times, once to the Rails application and another time to Amazon S3).

The screencast finishes with additional useful tips including:

  • using BitTorrent sharing protocol to download files and save bandwidth if you happen to host some big files on Amazon S3
  • how to configure DNS by adding CNAME record in the DNS control panel so that downloads appear to be coming from your own domain
  • using the paperclip gem as an easy way to get your files on Amazon S3

Summary: must have screencast for building an Amazon S3 application in Rails, including lots of tips on the side you will surely find useful.

Vim for Rails Development

Posted on February 28, 2010 at 07:48 pm

Update 04/27/2010: There are new updated vimfiles from Akita on Rails.

Few days ago I received a free copy of Vim for Rails Development screencast from Ben Orenstein of Codeulate Screencasts and we will be showing it at MKRUG meetup this Thursday (04.03.2010). So, I thought it would be good to write a new blog post about where to start and how to learn Vim for Rails development by referencing some books/screencasts. At the end I will also highlight some details of “Vim for Rails Development” screencast.

But first, why use Vim!? In short: it’s cross platform, open source and free text editor that lets you efficiently edit text. Other choices could be Textmate (only on Mac and paid), or Emacs (I won’t go into details why I prefer Vim, but you can read more about this editor war at wikipedia).

Learning process of any new tool starts with learning the basics, so I found A Byte of Vim to be short, strict and well organized free book to learn the basics of vi/vim. One of the most important things that I learned from that book is how to properly position hands on the keyboard (ASDF/JKL:). It took me some time while I get comfortable, but it is very important step toward using the keyboard more effectively.

Then, for developing Ruby on Rails applications you have to setup your Vim installation with plugins that will make your life much easier. And, instead of doing that on your own, it’s better choice to clone an already prepared installation which you can also customize if you want to. About a year ago Akita on Rails released a free Vim screencast Rails on Vim where he goes into details how to install Vim editor on Linux / Mac / Windows platforms with already installed plugins for Rails development. In that screencast he also talks about basics of using Vim and reviews some plugins like : NERDTree, FuzzyFinder and NERD snippets.

Here are instructions that I use for installing Vim with plugins for Rails development on Ubuntu:

  sudo apt-get install vim-gnome
  sudo apt-get install ctags
  sudo apt-get install ncurses-term

  git clone git://github.com/akitaonrails/vimfiles.git ~/.vim
  cd ~/.vim
  git submodule init
  git submodule update
  # add "source ~/.vim/vimrc" to ~/.vimrc file

  # Command-T
  cd ~/.vim/bundle/Command-T/ruby/command-t
  ruby extconf.rb
  make

Having Vim environment for Rails development setup, it’s time for some more advanced screencast.

“Vim for Rails Development” is a professional, well-recorded, 37 minutes long, $9 screencast which you can buy from Codeulate Screencasts. It’s recorded from a person who uses Vim on daily basis for writing Rails code, so the content of the screencast is more advanced since he describes his daily workflow.

It starts by giving advice on how to type faster, produce code faster and that way you don’t interrupt your thinking process. Then it goes into details about using the Rails.vim plugin from Tim Pope, for quick navigation in a Rails project, switching between files, working in parallel with vertical/horizontal splits, etc. He also reviews Snipmate plugin from Michael Sanders and demonstrates how to write your own snippets.

What I personally learned from this screencast was Exuberant Ctags for looking method definitions in Rails source code in much much quicker way than I was doing it previously using a file browser. Also, I learned about using Ack.vim plugin for better searching text through source code files than I was previously, using grep.

The screencast ends with few tips on fast text editing in situations that we frequently meet while editing code.

If you use Vim for Rails development and you just think about how much time you save by consuming all this digested material, instead of spending time digging into documentations, those kind of screencasts are worth buying.

Quick Javascript Development Tips

Posted on February 04, 2010 at 11:45 pm

Recently I have written lots of Javascript and jQuery code working on a live betting system that is build by consuming two APIs (Search and POS API) and has pretty complex functionality. So, time has come to open the Javascript tag on my blog with two quick tips about developing Javascript code.

The first tip is about benchmarking and second about debugging Javascript code using the console functionality. Did I mentioned that I use Firefox and Firebug for web development?

Benchmarking Javascript code using Firebug is really easy, You just drop console.time at the beginning and console.timeEnd at the end of the code snippet, and then you get result about how that code is performing:

console.time('BEFORE');
var ar = [];
for (i = 0; i < 10000; i++) {
  ar.push("text");
}
ar.join("");
console.timeEnd('BEFORE');

console.time('AFTER');
var str = "";
for (i = 0; i < 10000; i++) {
  str += "text"
}
console.timeEnd('AFTER');

The results are:

BEFORE: 16ms
AFTER: 13ms

Second tip is about cross-browser Javascript debugging by logging debug messages. I get this tip from Secrets of the JavaScript Ninja book, but improved it a little bit by encapsulating it in a module using the Javascript module pattern and adding some JS Lint code quality check.

/*global console */
/*global opera */

"use strict";

/**
 * Logger functionality
 * @constructor
 */
var LOGGER = (
  function () {

    /**
     * Writes log messages
     * 
     */
    function log() {
        try {
          console.log.apply(console, arguments);
        } catch (e) {
          try {
            opera.postError.apply(opera, arguments);
          } 
          catch (e2) {
          alert( Array.prototype.join.call( arguments, " " ) );
          }
        }
    }

    /**
     * Public interface
     * 
     */
    return {
      log: log
    };
  }()
);

Then we can use this module and log some messages:

LOGGER.log("message 1")
LOGGER.log("message 2")

If you like more fancy way of logging messages you may want to check the Blackbird library.

Next on Javascript I will be writing on some Javascript tools for: code quality check, documenting code, code compression, etc.

Ruby Singleton Pattern Again

Posted on January 24, 2010 at 05:26 pm

After giving a bad advice on using Ruby Singleton Pattern in my previous post, I decided to remove it, and write new one. Destroy. Erase. Improve.

Singleton is perhaps the most hated of all programming patterns. You can read some of the reasons for this in Why Singletons are Evil post pointed out by Tom Ward previously. However, it has some good usages, so I will first start by describing what a singleton pattern is, some classic and alternative ways of implementing it in Ruby, and point to it’s proper usage in Rails at the end. The whole post will be kind of a short summary of Singleton Pattern from Design Patterns in Ruby book.

Singleton is a design pattern that restricts instantiation of a class to only one instance and that instance is globally available. It is useful in situations when you need that instance to be accessible in different parts of the application, usually for logging functionality, communication with external systems, database access, etc. There are few ways of implementing singleton pattern in Ruby, starting with the classic one:

  • Single Instance of a class
class Logger
  def initialize
    @log = File.open("log.txt", "a")
  end
  
  @@instance = Logger.new

  def self.instance
    return @@instance
  end
  
  def log(msg)
    @log.puts(msg)
  end
  
  private_class_method :new
end

Logger.instance.log('message 1')

In this code example, inside class Logger we create instance of the very same class Logger and we can access that instance with class method Logger.instance whenever we need to write something to the log file using the instance method “log”. In the initialize method we just opened a log file for appending, and at the end of Logger class, we made method “new” private so that we cannot create new instances of class Logger. And, that is Singleton Pattern: only one instance, globally available.

  • Ruby Singleton module

Ruby Standard Library has a Singleton module which implements the Singleton pattern. Previous example when using the Singleton module would translate to:

require 'singleton'

class Logger
  include Singleton
  
  def initialize
    @log = File.open("log.txt", "a")
  end
  
  def log(msg)
    @log.puts(msg)
  end
end

Logger.instance.log('message 2')

Here we require and include Singleton module inside Logger class, define “initialize” method which opens the log file for appending and instance method “log” for writing to that log file. It’s worth pointing out that Ruby Singleton module also does lazy instantiation (creates instance from Logger class at the moment when we call Logger.instance method) and not during load time (like in the previous example). Also, Ruby Singleton module makes “new” method private, so we don’t have to call private_class_method.

Now, we can look at some of the alternative ways of implementing similar functionality to Singleton Pattern in Ruby:

  • Global variable

Because singleton instance has global access, we can actually create a global variable $logger and use it throughout application. But, this is really bad idea, as for all global variables, the problem is that it can be very easy redefined to point to some other object without noticing that.

  $logger = Logger.new
  $logger.log("message 3")
  • Constant

Constant solves the previous problem with redefining (it cannot be redefined without getting any warning), but here the problem is that we cannot have lazy instantiation.

  LOGGER = Logger.new
  LOGGER.log("message 4")
  • Class

We can also implement singleton functionality with class methods and class variables. This way we are sure that we cannot create another instance, but we have a problem with lazy initialization (log file will be opened at the time when this Logger class gets loaded and not at the time when we call “log” method).

class Logger
  @@log = File.open("log.txt", "a")

  def self.log(msg)
    @@log.puts(msg)
  end
end

Logger.log('message 5')
  • Module

This implementation is very same with previous, with the advantage that modules can’t be instantiated. Also, lazy instantiation is still problem here.

module Logger
  @@log = File.open("log.txt", "a")

  def self.log(msg)
    @@log.puts(msg)
  end
end

Logger.log('message 6')

Update: Actually, as Tammer Saleh pointed out in the comments we can achieve lazy instantiation here with the following:

module Logger
  def self.log(msg)
    @@log ||= File.open("log.txt", "a")
    @@log.puts(msg)
  end
end

Logger.log('message 7')

Same for the previous implementation with class method.

- Unit testing Singleton problem

It’s not possible to write correct unit tests for singleton. Using the same instance in all tests, means that its state could change in one of the tests and the following would not start with a known state which is critical requirement for good unit test. So, the workaround here is to actually write tests for instance that is not a singleton (instance of Logger), and in the application use Singleton instance (instance of SingletonLogger) which inherits from Logger and includes Singleton module, so that way it becomes singleton:

require 'singleton'

class Logger
  # Logger implementation
end

class SingletonLogger < Logger
  include Singleton
end

- Example of Singleton Pattern in Rails

One of good usages of singleton pattern with Ruby, would be the implementation of class Inflections in Rails. It is a single instance (Inflections.instance) that gives global access to all inflection rules used in different parts of Rails. Here are blank definitions:

module ActiveSupport
  module Inflector
    class Inflections
      def self.instance
        @__instance__ ||= new
      end

      attr_reader :plurals, :singulars, :uncountables, :humans

      def initialize
        @plurals, @singulars, @uncountables, @humans = [], [], [], []
      end

      def plural(rule, replacement)
      end

      def singular(rule, replacement)
      end

      def irregular(singular, plural)
      end

      def uncountable(*words)
      end

      def human(rule, replacement)
      end

      def clear(scope = :all)
      end
    end

    # Yields a singleton instance of Inflector::Inflections 
    # so you can specify additional inflector rules.
    def inflections
      if block_given?
        yield Inflections.instance
      else
        Inflections.instance
      end
    end

    def pluralize(word)
    end

    def singularize(word)
    end

    def humanize(lower_case_and_underscored_word)
    end

    def titleize(word)
    end

    def tableize(class_name)
    end

    def classify(table_name)
    end
  end
end

ActiveSupport::Inflector::Inflections class implements the singleton pattern. Singleton instance is returned or yielded by inflections method (defined in ActiveSupport::Inflector module) depending if it is called with a block or not. Methods like: pluralize, singularize, humanize, titleize, tableize and classify defined in ActiveSupport::Inflector module calls inflections method to get singleton instance and implement theirs functionality, and also inflections method is called in other parts of Rails like in config/initializers/inflections.rb with a block for adding other inflections that are not added by default:

ActiveSupport::Inflector.inflections do |inflect|
  #   inflect.plural /^(ox)$/i, '\1en'
  #   inflect.singular /^(ox)en/i, '\1'
  #   inflect.irregular 'person', 'people'
  #   inflect.uncountable %w( fish sheep )
end

Anyone to point to other good usages of Singleton Pattern in Ruby libraries?

Premature end of script headers - Ruby on Rails with Phusion Passenger on Apache error

Posted on November 11, 2009 at 01:17 am

If you are experiencing the following problems:

  • your application works great in development environment, but once you deploy it to production server using Phusion Passenger on Apache you get the “Error 500 – Internal server error” error
  • and, when you check the Apache error log /var/log/apache2/error.log you find the actual error which says: “Premature end of script headers”

Maybe this short write up will clear up things and solve your problem too, because I found Google results a little bit missleading on this one.

“Premature end of script headers” error basically means that script stopped for whatever reason before it returned any output to the web server. And, the problem in my case was that Apache user did not have read/write access to the directory where Rails application is located. So, when it tries to write to the log file (for example: paperclip gem logs image resizes or restrul_authentication gem logs wrong logins), the application fails on those form submits.

All you have to do is change the privilegies of Apache user (www-data on Debian/Ubuntu) and Apache group (www-data) to the folder where rails app is located:

chown -R www-data:www-data /var/rails

Update: As Hongli Lai notes in the comments, this is actually the case when app/apache is run as root user, and user switching is turned on (the default).