Skip to content

Commit

Permalink
Feature #14: Queueing multiple songs from one result set
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Webber committed Dec 1, 2011
1 parent c7eac16 commit 070eb11
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
54 changes: 45 additions & 9 deletions lib/robut-rdio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,42 @@ def usage
]
end


#
# @param [String,Array] request that is being evaluated as a play request
# @return [Boolean]
#
def play_results_regex
/^(?:play)?\s?(?:result)?\s?((?:\d[\s,-]*)+)$/
end

#
# @param [String,Array] request that is being evaluated as a play request
# @return [Boolean]
#
def play?(request)
Array(request).join(' ') =~ /^(play)?\s?(result)?\s?\d/
Array(request).join(' ') =~ play_results_regex
end

#
# @param [Array,String] track_request the play request that is going to be
# parsed for available tracks.
#
# @return [Array] track numbers that were identified.
#
# @example Requesting multiple tracks
#
# "play 1"
# "play 1 2"
# "play 1,2"
# "play 1-3"
# "play 1, 2 4-6"
#
def parse_tracks_to_play(track_request)
Array(track_request).join(' ')[play_results_regex,-1].to_s.split(/(?:\s|,\s?)/).map do |track|
tracks = track.split("-")
(tracks.first.to_i..tracks.last.to_i).to_a
end.flatten
end

#
Expand Down Expand Up @@ -131,8 +161,8 @@ def handle(time, sender_nick, message)
if sent_to_me?(message)

if play?(words)
play_result(words.last.to_i)

play_result *parse_tracks_to_play(words)

elsif search_and_play?(words)

Expand Down Expand Up @@ -195,16 +225,22 @@ def format_results(results)
result_display
end

def play_result(number)
def play_result(*requests)

if !has_results?
reply("I don't have any search results") and return
end

if !has_result?(number)
reply("I don't have that result") and return

requests.flatten.each do |request|

if has_result?(request.to_i)
queue result_at(request.to_i)
else
reply("I don't have that result")
end

end

queue result_at(number)

end

def search_and_play_result(message)
Expand Down
22 changes: 19 additions & 3 deletions spec/robut-rdio_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@

end

describe "Routing Methods" do
describe "Routing Methods" do

describe "#play?", :method => :play? do

it_should_behave_like "a routing method"

let(:valid_requests) do
[
"play 0",
# play keyword
"play 0",
"play 999999",
[ "play", "0" ],
# result keyword
"result1",
"result 1",
[ "result 0"]
[ "result 0"],
# multiple tracks
"play 1, 2, 3",
"play 1 2 3",
"play 1 - 3",
"play 1-3"
]
end

Expand Down Expand Up @@ -166,6 +173,15 @@

end

context "when it is a multiple play request" do

let(:message) { "@dj play 1, 4 5-7" }

it_should_behave_like "a successfully routed action",
:route => :play?, :action => :play_result, :parameters => [1,4,5,6,7]

end

context "when it is a search and play request" do

let(:message) { "@dj play the misfits" }
Expand Down

0 comments on commit 070eb11

Please sign in to comment.