Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created October 13, 2010 19:09
Show Gist options
  • Save ashaw/624669 to your computer and use it in GitHub Desktop.
Save ashaw/624669 to your computer and use it in GitHub Desktop.
HASH_COMMAND_MATCHER = /(#d(?:\s|$|([0-9]+))(?:\s|$))/
# hash commands for twitter:
# `#d` : delete the last twitter post in the wire
# `#d35235` : delete tweet with id 35235 from the wire
def process_hash_commands
cmds = []
@payload.each do |tweet|
if Update.find_by_native_item_id(tweet.id).nil?
tweet.text =~ HASH_COMMAND_MATCHER
if $1
cmds << $1.strip
# keep the command tweet around and
# hidden, so we don't process it again
Update.create({:native_item_id => tweet.id, :hide => true})
end
end
end
cmds.each do |cmd|
if cmd == "#d"
u = Update.find(:first, :conditions => {:via => "twitter", :hide => false}, :order => "item_create_time DESC")
else #d with tweet_id
tweet_id = cmd.sub(/#d/,'').to_i
u = Update.find(:first, :conditions => {:native_item_id => tweet_id})
end
if u
u.hide = true
u.save
end
end
end
it "should match correct hash commands" do
re = MultiWire::Consumer::HASH_COMMAND_MATCHER
"delete the last tweet please #d" =~ re
$1.strip.should eql("#d") if $1
"delete the #d tweet please " =~ re
$1.strip.should eql("#d") if $1
"delete some tweet #d26015886424 thanks" =~ re
$1.strip.should eql("#d26015886424") if $1
"don't delete anything, this is just a plain old tweet" =~ re
$1.should be_nil
"#destroyer of worlds" =~ re
$1.should be_nil
"check out this sweet #d2d software" =~ re
$1.should be_nil
end
it "should delete stuff from the wire according to twitter hash commands" do
#most recent tweet
Update.find(:first, :conditions => {:native_item_id => 26550410216}).hide.should_not be_true
#random tweet
Update.find(:first, :conditions => {:native_item_id => 26015886424}).hide.should_not be_true
fake_command_tweets = [
{:text => "delete the last tweet please #d"},
{:text => "delete some tweet #d26015886424 thanks"},
{:text => "don't delete anything, this is just a plain old tweet"}
].collect {|q| Hashie::Mash.new(q) }
m = MultiWire::Consumer.new
m.instance_variable_set(:@payload, fake_command_tweets)
m.process_hash_commands()
#most recent tweet
Update.find(:first, :conditions => {:native_item_id => 26550410216}).hide.should be_true
#random tweet killed by id
id_based_twitter_entry = Update.find(:first, :conditions => {:native_item_id => 26015886424}).hide.should be_true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment