Rails
ActiveRecord object caching in Ruby on Rails
Submitted by steven on Sat, 02/20/2010 - 17:03Yesterday I started playing with basic caching for PriceChirp and tried what I thought would be easy. Boy was I wrong. It turns out what I was attempting to do is not supported by :memory_store in the development environment. Before moving to :mem_cache_store, I was able to find a work around. The work around is outlined below for those who do not have the option of using memcached. However, if you can use memcached, it is by far the better route to take.
My goal was to cut down on the number of database hit by caching the resultant ActiveRecord object in :memory_store.
PriceChirp has improved wishlist support
Submitted by steven on Mon, 09/28/2009 - 03:41
![]()
This week I improved the Amazon wishlist support in PriceChirp. One of the cool features of PriceChirp from the beginning has been how easy it is to import an Amazon wishlist into PriceChirp. The only problem with this feature is it was an all or nothing proposition. Now, we have the ability to view our wishlists in PriceChirp and select which items we wish to import. The old feature of importing everything is still thee, but now we have options.
To see this feature in action, log into your PriceChirp account and do a wishlist search. This is done by searching for the email address associated to your Amazon wishlist. Once your wishlists are displayed, you can select "view wishlist" to get a listing of your items. From this page you can easily select which item to import into your personalized tracker.
Have fun!
Number to Currency Outside Rails
Submitted by steven on Fri, 09/18/2009 - 07:02I created a module I am also using both in rails and for some batch processing outside of rails. In order to ensure my data was formatted the same with both environments, I needed to figure out how to get access to the active_view helper number_to_currency outside of rails. Here is how I did it:
require 'rubygems' require 'action_view' def number_to_currency(number, options = {}) ActionView::Base.new.number_to_currency(number, options) end
Rescuing from open-uri timeouts
Submitted by steven on Sat, 09/12/2009 - 22:54Found an issue with open-uri because today Amazon's API is wonky. The same issue will occur when accessing any remote data via open-uri (like a RSS feed) if the data source is going to slow. The relevant part of my ruby code looks like:
begin doc = Nokogiri(open(api_url)) rescue print "Connection failed: #{$!}\n" next end
For some reason, the rescue does not catch a timeout error.
/usr/lib64/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error)
Explicitly catching the timeout error fixes the code.
begin doc = Nokogiri(open(api_url)) rescue Timeout::Error print "Timeout::Error: #{$!}\n" next rescue print "Connection failed: #{$!}\n" next end
Good to know, as you never know when your remote data provider will be slow.
Gravatars on Rails
Submitted by steven on Sat, 08/29/2009 - 22:07![]()
Gravatar is the globally recognized avatar run by the folks who run Wordpress.com.
Your Gravatar is an image that follows you from site to site appearing beside your name when you do things like comment or post on a blog. This is done by associating an image to the users email address. Adding Gravatar to PriceChirp seemed like a fun thing to do.
The API is very simple, so adding it to a Ruby on Rails site is easy.
First, add a few lines to your application-helper.rb:
def gravatar_url_for(email, options = {}) url_for({ :gravatar_id => Digest::MD5.hexdigest(email), :host => 'www.gravatar.com', :protocol => 'http://', :only_path => false, :skip_relative_url_root => true, :controller => '/avatar' }.merge(options) ) end
You may also need to add:
Upgrading PriceChirp from Rails 2.2.2 to Rails 2.3.3
Submitted by steven on Thu, 08/27/2009 - 04:50Today I updated PriceChirp from rails 2.2.2 to rails 2.3.3. It was easier than expected, but I ran into several gotchas along the way. Here's an outline of the major issues I encountered and how I addressed them. Depending on your code, this may or may not be any any use. Note: Several of these steps had to be performed on both the development and production boxes.
1) Updating gems
To upgrade the base system gems, as root I ran:
gem update
This upgraded the system gems such as rails, mysql, passenger, etc. The gems built into PriceChirp in the /vendor/gems directory had to be updated separately. I'll get to those in a few minutes.
2) Updating passenger
Generating Sitemaps in Rails
Submitted by steven on Fri, 03/27/2009 - 00:18Code updated 9/18/2009
This week I added sitemap files to my rails site PriceChirp. Sitemaps are used to help search engines to find all your content. They are especially helpful in enumerating pages that are difficult for web crawlers to discover, such as content from database searches.
The web is full of instructions on how to generate sitemaps on the fly using rxml templates. This does not scale well if your site has thousands of links. A better method is to periodically generate site maps and serve these cached files when requested.
www.fortytwo.gr has a good example for generating sitemaps with rails. I've taken his code and fixed/extended it to fit my needs.
Understanding Sitemaps
Basically, there are two types of sitemap files: