Right now at Harvest we are baking a few pans of Twitter integration. We’re not sure if they’ll turn out moist and delicious, but hopefully they stop short of dry and forgettable. It’s been a fun, and somewhat frustrating, process getting this feature cooking.
The key interaction Harvest is using is Twitter’s direct message. Send Harvest a message and we’ll pick it up and do something with it. Through the Twitter API, one can grab all direct messages since a certain direct_message.id. In this case, direct_message.id is used as a chronology identifier - the larger the id, the newer the message.
In order to go out and grab a batch of messages, the previous max direct_message.id needed to be tracked somewhere. To me, this felt similar to Rails’ schema_info table - direct_message_info we’ll call it. So with all of those Rails opinions, how do you set up such a schema-tracking table? First, you migrate:
def self.up
create_table :direct_message_info, :id => false do |t|
t.column :last_id, :integer
end
ActiveRecord::Base.connection.insert("INSERT INTO direct_message_info VALUES(28700000)")
end
def self.down
drop_table :twitter_direct_message_info
end
end
This migration is fairly simple. When creating the table, we specify that there is no need for the Rails-default id column. Also, insert an initial starting value. I suggest playing with the API and selecting a value reasonably close to the latest id numbers for Twitter’s direct messages.
Then, throw your ActiveRecord model out the window for models/direct_message_info.rb:
class << self
def get_most_recent_id
ActiveRecord::Base.connection.select_one("SELECT last_id FROM direct_message_info")["last_id"].to_i
end
def set_recent_id(last_id)
ActiveRecord::Base.connection.update("UPDATE direct_message_info SET last_id = #{last_id}") if last_id
end
end
end
Two pretty simple class methods for getting and setting your new schema info value.
Technorati Tags: Rails, ActiveRecord







