I’m new to Ruby (and Rails) and was hoping you could help clear up some confusion I’m experiencing.
I’m trying to integrate the Twitter gem into my website in order to get a user’s latest tweet, and grab the link to their profile picture. The gem works great up until (what I think is) the 100th API call in an hour, after which Twitter will cut you off.
From what I’ve gathered, I need to cache the result for ~1 minute using memcache. There was some great pseudocode here but unfortunately, it was a bit over my head. I was hoping I could get some more specifics.
At the moment, I’m unsure where would I place that code? I want to display the twitter information in the application layout view, so would it go into a method in the application_helper.rb file?
My best attempt at figuring this out resulted in the following code, which throws a “Missing Helper File” error.
module ApplicationHelper
require "memcache"
def twitter
cache = MemCache.new
twitter = cache.get("twitter").first
if twitter.nil?
begin
twitter = Twitter.user("TwitterName")
cache.set("twitter", twitter, :expires_in => 1.minute) if twitter
rescue
twitter = default
end
end
return twitter
end
end
First enable caching and memcache for your environment (e.g. config/environments/production.rb)
Then in the view you want to show tweets do something like this