Articles

A new option :dash for Shoes

Spreadsheet – rubygem

We can handle spreadsheets easily with the help of SPREADSHEET ruby gem.

Installation:

> gem install spreadsheet

Basic Code:

require ‘rubygems’
require ’spreadsheet’

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet  :name => (‘Names’)
sheet1[0,0] = ‘Raveendran’
sheet1[0,1] = ‘Age : 24′

sheet1[1,0] = ‘Jazzezravi’
sheet1[1,1] = ‘Age : 25′

sheet1[2,0] = ‘Jazzez’
sheet1[2,1] = ‘Age: 26′

book.write(‘F:\ravi\sample.xls’)

For more details –  http://spreadsheet.rubyforge.org/

3 years of freelancing fun

I realized a couple of days ago that it was January of 2007 when I dove into the crazy world of freelancing full-time. It has been quite an adventure. In that time I’ve worked on a number of fun projects with an amazing assortment of awesome clients, launched two different products (Catch the Best in 2007 and Rails Kits in 2008), and learned a whole lot about how to (and how not to) run a business. It’s been a blast.

Now sponsorsed by Ruby Row

I’m happy to announce that this blog is now sponsored by an exclusive ad network run by James Avery called Ruby Row. While I could have just slapped google adsense all over the blog, Ruby Row provides much more value to the visitors as the ads are primarily targeted at Ruby/Rails developers.

I still have a spot open for per post text advertising. So if you’re interested, drop me an email on pratiknaik gmail

The Ruby Show 105: Average Size

In this episode, Dan and Jason bring you a ton of news and keep it short and sweet.

Weekend Reading: RBP Chapter 1

If you’re reading this blog, you probably know that the Ruby Best Practices book exists. Even if you haven’t read it, you might have a sense for the sort of topics we cover based on the content you’ve seen on this blog. But now, everyone is going to get a chance to read RBP the way its meant to be read: as a conversation.

Present.ly Chrome Extension

We’re always looking for ways to make it easier to keep up to date with your co-workers using Present.ly. Recently I’ve been using the Mac Beta of Chrome and thought a Chrome Extension could be a great way to use Present.ly throughout the day. As of today it is available on Google’s Chrome Extension Directory.

Present.ly Chrome

rails-upgrade: Automating a portion of the Rails 3 upgrade process

If you’re looking for more info on upgrading, don’t miss out on my other posts on Rails 3 starting here.

NOTE: This is now an official, blessed plugin, so use that rather than this gem. More info here.

Cluster Monitoring with Ganglia & Ruby

A good monitoring solution can make or break an entire service - a well implemented one will enable you to forecast and plan ahead, as well as, quickly spot and debug problems when they arise. However, anyone that has worked with a cluster of machines will know that this is also a non-trivial problem.

MySQL slow query improvements

We’ve just pushed some updates to the MySQL slow query warning interface in the Brightbox control panel, which includes two main improvements.

Firstly, where the same (or very similar) query occurs more than once, they are aggregated together rather than being recorded as individual slow queries. The user interface displays the count so you can see exactly how many times this slow query occurred in the last 7 days.

Secondly, the full SQL query is now displayed, so customers now have more information with which to optimise their applications.

Active Resource in practice | Rails Fire

Active Resource in practice

Send to friend

I’m working on app to integrate Pivotal Tracker and Harvest. There’s a great ruby wrapper around Harvest’s API, but there isn’t a decent Ruby wrapper for Tracker’s v3 API, so I thought I would just build one as I needed it.

If this app were read only, I would probably use HTTParty and HappyMapper, but since I also want to be able to update timers and stories, Active Resource seemed like the right tool for the job. Active Resource in theory is great. Active Resource in practice is not so great. I’ve toyed around with it in the past, but using it for something real I found it…lacking.

Fortunately the Harvest gem had solved a lot of these problems. I write about them here in hopes that they will be useful to you.

Challenge 1: Headers don’t inherit

Pivotal Tracker uses a token for authentication and looks for it in a header called “X-TrackerToken”. It would be nice if you could just set this once, and all Active Resource classes would use it. But unfortunately, headers don’t inherit.

So the trick is to define a base class for all of your models to inherit from, and in that override how Active Resource treats headers.

module PivotalTracker
  class Resource < ActiveResource::Base
    Resource.site = "https://www.pivotaltracker.com/services/v3"

    class << self
      # If headers are not defined in a given subclass, then obtain
      # headers from the superclass.
      def headers
        if defined?(@headers)
          @headers
        elsif superclass != Object && superclass.headers
          superclass.headers
        else
          @headers ||= {}
        end
      end
  end

  class Project < Resource
  end
end

Now we can set our token once and subclasses will inherit it:

PivotalTracker::Resource.headers['X-TrackerToken'] = "mytoken"
projects = PivotalTracker::Project.find(:all)

Challenge 2: “Associations”

I find it strange that Active Resource doesn’t support associations. Rails has a standard way of defining embedded resources, so you would think that Active Resource would have a standard way of consuming them (I know, I should get off my lazy duff and contribute a patch, but it’s so much easier to just complain about it).

So for APIs that have nested resources like Pivotal Tracker’s, Active Resource forces you to hard code the parent resource id. If you want to get the iterations for a project, then you have to set the project_id on the Iteration resource.

PROJECT_ID = 1738

module PivotalTracker
  class Iteration < Resource
    self.prefix = "/services/v3/projects/#{PROJECT_ID}"
  end
end

This is just not a scalable solution. I’m going to need to be able to access multiple projects in the app that I’m working on. So the Harvest gem had a really clever (and evil) solution, which I’ve modified a bit here.

It basically involves creating an anonymous subclass of our resource, and setting the prefix just for that subclass.

module PivotalTracker
  class Project < Resource
    def iterations
      Iteration.build_subclass.tap do |iteration|
        iteration.prefix = "/services/v3/projects/#{self.id}"
      end
    end
  end
end

Now we can access iterations for any project.

iterations = Project.find(x).iterations.find(:all)

The #build_subclass method is defined on the base resource and just creates an anonymous subclass and copies some settings that don’t inherit.

Onward Ho!

I don’t have a lot built out yet for the new Pivotal Tracker wrapper, but you can check out the latest progress on GitHub. I feel like I’ve overcome most of the bit barriers, so it shouldn’t take much to finish it up.

Do you have any other tips or tricks for working with Active Resource?