I 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:
Found 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.
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:
Now you can add the gravatar to your views like this:
# standard gravatar url
<%= gravatar_url_for 'user@gmail.com' %>
# gravatar url with a rating threshold
<%= gravatar_url_for 'user@gmail.com', { :rating => 'R' } %>
# show the avatar
<%= image_tag(gravatar_url_for 'email@gmail.com')%>
# change the size of the avatar
<%= image_tag(gravatar_url_for('email@gmail.com', { :size => 30 })) %>
Note: Gravatar is case sensitive on the user’s email address. They will match against an all lower case version of the email address. If you need to allow uppercase characters in your email addresses, you will need to .downcase the email address in the view or helper. I do not have this requirement, so I fix the case in the user.rb model at save time:
before_save :fix_email_case
protected
def fix_email_case
if !self.email.nil?
self.email.downcase!
end
end