Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Correctly using Lita::Room object and explicitly use MultiJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
brodock committed Aug 14, 2015
1 parent f91ce70 commit 78a2cfd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).

We follow [Keep a Changelog](http://keepachangelog.com/) format.

## 0.3.0 - 2015-07-19
### Modified
- Correctly using Lita::Room object
* create queues based on room ID
* display queue using room name metadata
* modified specs to catch this change

## 0.2.0 - 2015-06-21
### Added
- Respond to the following additional command:
Expand Down
14 changes: 9 additions & 5 deletions lib/lita/handlers/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ class Queue < Handler
# API

def fetch_queue(room)
serialized = redis.get(room)
raise ArgumentError, 'must be a Lita::Room object' unless room.is_a? Lita::Room

serialized = redis.get(room.id)

if serialized
JSON.parse(serialized)
MultiJson.load(serialized)
else
[]
end
end

def store_queue(room, queue)
redis.set room, queue.to_json
redis.set room.id, MultiJson.dump(queue)
end

# Commands
Expand Down Expand Up @@ -107,14 +109,16 @@ def queue_rotate(response)
private

def room_for(response)
response.message.source.room || '--global--'
response.message.source.room_object
end

def display_queue(queue, room)
log.debug "displaying info for queue: #{queue.inspect} at #{room.inspect}"

if queue.empty?
"Queue is empty!"
else
"Queue for #{room}: #{queue}"
"Queue for #{room.name}: #{queue}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lita-queue.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "lita", ">= 4.3"
spec.add_runtime_dependency "lita", ">= 4.4.3"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "pry-byebug"
Expand Down
60 changes: 30 additions & 30 deletions spec/lita/handlers/queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,64 @@
it { is_expected.to route_command("queue rotate!").to(:queue_rotate) }
#it { is_expected.to route_command("queue = [something,here]").to(:queue_recreate) }

let(:channel) { source.room || '--global--' }
let(:room) { Lita::Room.new(1, {name: 'my-channel'}) }

# Commands
describe "#queue_list" do
context "when queue is empty" do
before { subject.store_queue(channel, []) }
before { subject.store_queue(room, []) }

it "replies with an empty queue message" do
send_command("queue")
send_command("queue", from: room)
expect(replies.last).to include("Queue is empty")
end
end

context "when queue has elements" do
before { subject.store_queue(channel, [user1.mention_name, user2.mention_name]) }
before { subject.store_queue(room, [user1.mention_name, user2.mention_name]) }

it "replies with a list of queue users" do
send_command("queue")
send_command("queue", from: room)
expect(replies.last).to include(user1.mention_name, user2.mention_name)
end
end
end

describe "#queue_me" do
context "when I'm already queued" do
before { subject.store_queue(channel, [user.mention_name]) }
before { subject.store_queue(room, [user.mention_name]) }

it "replies with an error message" do
send_command("queue me")
send_command("queue me", from: room)
expect(replies.last).to include("already on queue")
end
end

context "when I'm not on queue" do
it "replies with a confirmation message" do
send_command("queue me")
send_command("queue me", from: room)
expect(replies.last).to include("#{user.name} have been added to queue")
expect(subject.fetch_queue(channel)).to include(user.mention_name)
expect(subject.fetch_queue(room)).to include(user.mention_name)
end
end
end

describe "#unqueue_me" do
context "when I'm already queued" do
before { subject.store_queue(channel, [user.mention_name]) }
before { subject.store_queue(room, [user.mention_name]) }

it "replies with a confirmation and remove from queue" do
send_command("unqueue me")
send_command("unqueue me", from: room)
expect(replies.last).to include("#{user.name} have been removed from queue")
expect(subject.fetch_queue(channel)).not_to include(user.mention_name)
expect(subject.fetch_queue(room)).not_to include(user.mention_name)
end
end

context "when I'm not on queue" do
before { subject.store_queue(channel, []) }
before { subject.store_queue(room, []) }

it "replies with an error message" do
send_command("unqueue me")
send_command("unqueue me", from: room)
expect(replies.last).to include("not on queue!")
end
end
Expand All @@ -81,26 +81,26 @@
describe "#queue_list_next" do
context "when queue is empty" do
it "replies with an error message" do
send_command("queue next?")
send_command("queue next?", from: room)
expect(replies.last).to include("Queue is empty")
end
end

context "when queue has only one element" do
before { subject.store_queue(channel, [user1.mention_name]) }
before { subject.store_queue(room, [user1.mention_name]) }

it "replies listing current user on queue and warning that's the last one" do
send_command("queue next?")
send_command("queue next?", from: room)
expect(replies.last).to include(user1.mention_name)
expect(replies.last).to include("is the last one on queue")
end
end

context "when queue has more than one elements" do
before { subject.store_queue(channel, [user1.mention_name, user2.mention_name]) }
before { subject.store_queue(room, [user1.mention_name, user2.mention_name]) }

it "replies listing the next one on the queue" do
send_command("queue next?")
send_command("queue next?", from: room)
expect(replies.last).to include(user2.mention_name)
end
end
Expand All @@ -109,29 +109,29 @@
describe "#queue_change_to_next" do
context "when queue is empty" do
it "replies with an error message" do
send_command("queue next!")
send_command("queue next!", from: room)
expect(replies.last).to include("Queue is empty")
end
end

context "when queue has enough elements" do
let(:queue) { [user1.mention_name, user2.mention_name] }
before { subject.store_queue(channel, queue) }
before { subject.store_queue(room, queue) }

it "remove the first element from the queue" do
send_command("queue next!")
expect(subject.fetch_queue(channel)).to eq([user2.mention_name])
send_command("queue next!", from: room)
expect(subject.fetch_queue(room)).to eq([user2.mention_name])
end

it "replies informing who was been removed and who is next" do
send_command("queue next!")
send_command("queue next!", from: room)
expect(replies.first).to include("#{user1.mention_name} have been removed from queue")
expect(replies[1]).to include("#{user2.mention_name} is the next")

end

it "informs the new queue after execution" do
send_command("queue next!")
send_command("queue next!", from: room)
expect(replies.last).to include(user2.mention_name)
expect(replies.last).not_to include(user1.mention_name)
end
Expand All @@ -140,7 +140,7 @@
let(:queue) { [user2.mention_name] }

it "replies with a notification message when removing the last element from queue" do
send_command("queue next!")
send_command("queue next!", from: room)
expect(replies.first).to include("#{user2.mention_name} have been removed from queue")
expect(replies.last).to include("Queue is empty")
end
Expand All @@ -150,15 +150,15 @@

describe "#queue_rotate" do
context "when queue has enough elements" do
before { subject.store_queue(channel, [user1.mention_name, user2.mention_name, user3.mention_name]) }
before { subject.store_queue(room, [user1.mention_name, user2.mention_name, user3.mention_name]) }

it "removes the first element and add to the end" do
send_command("queue rotate!")
expect(subject.fetch_queue(channel)).to eq([user2.mention_name, user3.mention_name, user1.mention_name])
send_command("queue rotate!", from: room)
expect(subject.fetch_queue(room)).to eq([user2.mention_name, user3.mention_name, user1.mention_name])
end

it "replies mentioning the next user on queue and notifing the rotated one" do
send_command("queue rotate!")
send_command("queue rotate!", from: room)
expect(replies.first).to include("#{user1.mention_name} has been moved to the end of the queue")
expect(replies[1]).to include("#{user2.mention_name} is the next")
end
Expand Down
29 changes: 29 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "pry"
require "simplecov"
require "coveralls"
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
Expand All @@ -12,3 +13,31 @@
# A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
# was generated with Lita 4, the compatibility mode should be left disabled.
Lita.version_3_compatibility_mode = false

# Backport / Monkey Patch Lita Spec Handler Helper until a new version > 4.4.3 is released.
# More info here: https://github.com/jimmycuadra/lita/pull/129
module Lita
module RSpec
module Handler
# Sends a message to the robot.
# @param body [String] The message to send.
# @param as [Lita::User] The user sending the message.
# @param as [Lita::Room] The room where the message is received from.
# @return [void]
def send_message(body, as: user, from: nil)
message = Message.new(robot, body, Source.new(user: as, room: from))

robot.receive(message)
end

# Sends a "command" message to the robot.
# @param body [String] The message to send.
# @param as [Lita::User] The user sending the message.
# @param as [Lita::Room] The room where the message is received from.
# @return [void]
def send_command(body, as: user, from: nil)
send_message("#{robot.mention_name}: #{body}", as: as, from: from)
end
end
end
end

0 comments on commit 78a2cfd

Please sign in to comment.