From 78a2cfded3137bb80d5f80f78e5895888c46e466 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Thu, 13 Aug 2015 22:49:37 -0300 Subject: [PATCH] Correctly using Lita::Room object and explicitly use MultiJSON --- CHANGELOG.md | 7 ++++ lib/lita/handlers/queue.rb | 14 +++++--- lita-queue.gemspec | 2 +- spec/lita/handlers/queue_spec.rb | 60 ++++++++++++++++---------------- spec/spec_helper.rb | 29 +++++++++++++++ 5 files changed, 76 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66bb2ed..c0970ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/lib/lita/handlers/queue.rb b/lib/lita/handlers/queue.rb index c3a8262..639aa26 100644 --- a/lib/lita/handlers/queue.rb +++ b/lib/lita/handlers/queue.rb @@ -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 @@ -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 diff --git a/lita-queue.gemspec b/lita-queue.gemspec index 4ee78ad..5ec0438 100644 --- a/lita-queue.gemspec +++ b/lita-queue.gemspec @@ -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" diff --git a/spec/lita/handlers/queue_spec.rb b/spec/lita/handlers/queue_spec.rb index 004d1dc..1c14459 100644 --- a/spec/lita/handlers/queue_spec.rb +++ b/spec/lita/handlers/queue_spec.rb @@ -15,24 +15,24 @@ 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 @@ -40,39 +40,39 @@ 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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 20334f2..e00c185 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,4 @@ +require "pry" require "simplecov" require "coveralls" SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ @@ -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