Skip to content

Commit

Permalink
Update delete dm command to take IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Apr 24, 2012
1 parent 064b28c commit 50ef3a8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 66 deletions.
15 changes: 7 additions & 8 deletions lib/t/delete.rb
Expand Up @@ -28,17 +28,16 @@ def block(screen_name, *screen_names)
say "Run `#{File.basename($0)} block #{screen_names.join(' ')}` to block."
end

desc "dm", "Delete the last Direct Message sent."
def dm
direct_message = client.direct_messages_sent(:count => 1, :include_entities => false).first
if direct_message
desc "dm [DIRECT_MESSAGE_ID] [DIRECT_MESSAGE_ID...]", "Delete the last Direct Message sent."
def dm(direct_message_id, *direct_message_ids)
direct_message_ids.unshift(direct_message_id)
direct_message_ids.each do |direct_message_id|
unless options['force']
direct_message = client.direct_message(direct_message_id, :include_entities => false)
return unless yes? "Are you sure you want to permanently delete the direct message to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\"? [y/N]"
end
direct_message = client.direct_message_destroy(direct_message.id, :include_entities => false)
say "@#{direct_message.sender.screen_name} deleted the direct message sent to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\""
else
raise Thor::Error, "Direct Message not found"
direct_message = client.direct_message_destroy(direct_message_id, :include_entities => false)
say "@#{@rcfile.default_profile[0]} deleted the direct message sent to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\""
end
end
map %w(m) => :dm
Expand Down
97 changes: 39 additions & 58 deletions spec/delete_spec.rb
Expand Up @@ -40,76 +40,57 @@
describe "#dm" do
before do
@delete.options = @delete.options.merge(:profile => fixture_path + "/.trc")
stub_delete("/1/direct_messages/destroy/1773478249.json").
with(:query => {:include_entities => "false"}).
to_return(:body => fixture("direct_message.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
context "not found" do
context ":force => true" do
before do
stub_get("/1/direct_messages/sent.json").
with(:query => {:count => "1", :include_entities => "false"}).
to_return(:body => "[]", :headers => {:content_type => "application/json; charset=utf-8"})
@delete.options = @delete.options.merge(:force => true)
end
it "should request the correct resource" do
@delete.dm("1773478249")
a_delete("/1/direct_messages/destroy/1773478249.json").
with(:query => {:include_entities => "false"}).
should have_been_made
end
it "should exit" do
lambda do
@delete.dm
end.should raise_error(Thor::Error, "Direct Message not found")
it "should have the correct output" do
@delete.dm("1773478249")
$stdout.string.chomp.should == "@testcli deleted the direct message sent to @pengwynn: \"Creating a fixture for the Twitter gem\""
end
end
context "found" do
context ":force => false" do
before do
stub_get("/1/direct_messages/sent.json").
with(:query => {:count => "1", :include_entities => "false"}).
to_return(:body => fixture("direct_messages.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_delete("/1/direct_messages/destroy/1773478249.json").
@delete.options = @delete.options.merge(:force => false)
stub_get("/1/direct_messages/show/1773478249.json").
with(:query => {:include_entities => "false"}).
to_return(:body => fixture("direct_message.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
context ":force => true" do
before do
@delete.options = @delete.options.merge(:force => true)
end
it "should request the correct resource" do
@delete.dm
a_get("/1/direct_messages/sent.json").
with(:query => {:count => "1", :include_entities => "false"}).
should have_been_made
a_delete("/1/direct_messages/destroy/1773478249.json").
with(:query => {:include_entities => "false"}).
should have_been_made
end
it "should have the correct output" do
@delete.dm
$stdout.string.chomp.should == "@sferik deleted the direct message sent to @pengwynn: \"Creating a fixture for the Twitter gem\""
end
it "should request the correct resource" do
$stdout.should_receive(:print).with("Are you sure you want to permanently delete the direct message to @pengwynn: \"Creating a fixture for the Twitter gem\"? [y/N] ")
$stdin.should_receive(:gets).and_return("yes")
@delete.dm("1773478249")
a_get("/1/direct_messages/show/1773478249.json").
with(:query => {:include_entities => "false"}).
should have_been_made
a_delete("/1/direct_messages/destroy/1773478249.json").
with(:query => {:include_entities => "false"}).
should have_been_made
end
context ":force => false" do
before do
@delete.options = @delete.options.merge(:force => false)
end
it "should request the correct resource" do
$stdout.should_receive(:print).with("Are you sure you want to permanently delete the direct message to @hurrycane: \"Sounds good. Meeting Tuesday is fine.\"? [y/N] ")
context "yes" do
it "should have the correct output" do
$stdout.should_receive(:print).with("Are you sure you want to permanently delete the direct message to @pengwynn: \"Creating a fixture for the Twitter gem\"? [y/N] ")
$stdin.should_receive(:gets).and_return("yes")
@delete.dm
a_get("/1/direct_messages/sent.json").
with(:query => {:count => "1", :include_entities => "false"}).
should have_been_made
a_delete("/1/direct_messages/destroy/1773478249.json").
with(:query => {:include_entities => "false"}).
should have_been_made
end
context "yes" do
it "should have the correct output" do
$stdout.should_receive(:print).with("Are you sure you want to permanently delete the direct message to @hurrycane: \"Sounds good. Meeting Tuesday is fine.\"? [y/N] ")
$stdin.should_receive(:gets).and_return("yes")
@delete.dm
$stdout.string.chomp.should == "@sferik deleted the direct message sent to @pengwynn: \"Creating a fixture for the Twitter gem\""
end
@delete.dm("1773478249")
$stdout.string.chomp.should == "@testcli deleted the direct message sent to @pengwynn: \"Creating a fixture for the Twitter gem\""
end
context "no" do
it "should have the correct output" do
$stdout.should_receive(:print).with("Are you sure you want to permanently delete the direct message to @hurrycane: \"Sounds good. Meeting Tuesday is fine.\"? [y/N] ")
$stdin.should_receive(:gets).and_return("no")
@delete.dm
$stdout.string.chomp.should be_empty
end
end
context "no" do
it "should have the correct output" do
$stdout.should_receive(:print).with("Are you sure you want to permanently delete the direct message to @pengwynn: \"Creating a fixture for the Twitter gem\"? [y/N] ")
$stdin.should_receive(:gets).and_return("no")
@delete.dm("1773478249")
$stdout.string.chomp.should be_empty
end
end
end
Expand Down

0 comments on commit 50ef3a8

Please sign in to comment.