Permalink
Browse files

Added a Messages plugin

  • Loading branch information...
1 parent 6330c30 commit 97a69a8f849a067e62d1144b96e8910be54f9bce Christopher Warren committed Sep 9, 2011
Showing with 183 additions and 2 deletions.
  1. +2 −2 Rakefile
  2. +69 −0 lib/ceiling_cat/plugins/messages.rb
  3. +112 −0 spec/ceiling_cat/plugins/messages_spec.rb
View
@@ -84,8 +84,8 @@ describe "#{name.camelize}" do
token = "1234abcd"
plugins = [CeilingCat::Plugin::#{name.camelize}]
- FakeWeb.register_uri(:get, "https://\#{token}:X@\#{user}.campfirenow.com/rooms.json", :body => fixture('campfire/rooms.json'), :status => ["200"])
- FakeWeb.register_uri(:get, "https://\#{token}:X@\#{user}.campfirenow.com/users/me.json", :body => fixture('campfire/me.json'), :status => ["200"])
+ FakeWeb.register_uri(:get, "https://\#{token}:X@\#{subdomain}.campfirenow.com/rooms.json", :body => fixture('campfire/rooms.json'), :status => ["200"])
+ FakeWeb.register_uri(:get, "https://\#{token}:X@\#{subdomain}.campfirenow.com/users/me.json", :body => fixture('campfire/me.json'), :status => ["200"])
@connection = CeilingCat::Campfire::Connection.new(OpenStruct.new({:service => 'campfire', :subdomain => subdomain, :token => token, :room => 'Room 1', :plugins => plugins}))
@room = CeilingCat::Campfire::Room.new(:connection => @connection, :room_name => @connection.config.room)
@@ -0,0 +1,69 @@
+module CeilingCat
+ module Plugin
+ class Messages < CeilingCat::Plugin::Base
+ # See lib/ceiling_cat/plugins/base.rb for the methods available by default.
+ # Plugins are run in the order they are listed in the Chatfile.
+ # When a plugin returns anything other than nil the plugin execution chain is halted.
+ # If you want your plugin to do something but let the remaining plugins execute, return nil at the end of your method.
+
+ # handle manages a plugin's entire interaction with an event.
+ # If you only want to execute commands - "![command]" - leave handle alone (or remove it and define commands below)
+ def handle
+ if event.type == :entrance
+ messages = []
+ if messages_for_user = self.class.messages_for(user.name)
+ messages << "Hey #{user.name}! I have a message to deliver to you:"
+ messages += messages_for_user.collect{|message| "From #{message[:from]}: #{message[:body]}"}
+ reply messages
+ end
+ false
+ else
+ super
+ end
+ end
+
+ # If you want the plugin to run when certain text is sent use commands instead of handle.
+ # Ceiling Cat will watch for "![command]" or "[name]: [command" and execute the method for that command.
+ def self.commands
+ [{:command => "message for", :description => "Leave a message for someone. i.e. '!message for John Doe: You forgot to lock the door after work last night.'", :method => "save_message", :public => true}]
+ end
+
+ def self.description
+ "A plugin called Messages"
+ end
+
+ def self.name
+ "Messages"
+ end
+
+ def self.public?
+ false
+ end
+
+ def save_message
+ recipient,*body = body_without_nick_or_command("message for").split(":")
+ if room.users_in_room.any?{|user| user.name == recipient.strip}
+ reply "Why leave that messsage? #{recipient.strip} is here!"
+ else
+ self.class.add(:to => recipient.strip, :from => user.name, :body => body.join(":").strip)
+ reply "Message saved! I'll deliver it the next time #{recipient.strip} is around."
+ end
+ end
+
+ def self.add(opts={})
+ return false unless opts[:to] && opts[:from] && opts[:body]
+ store["messages"] ||= []
+ store["messages"] = (store["messages"] + [{:to => opts[:to], :from => opts[:from], :body => opts[:body]}]).uniq
+ end
+
+ def self.list
+ store["messages"] ||= []
+ end
+
+ def self.messages_for(name)
+ store["messages"] ||= []
+ store["messages"].find_all{ |message| message[:to] == name }
+ end
+ end
+ end
+end
@@ -0,0 +1,112 @@
+require 'spec_helper'
+require 'ceiling_cat/plugins/messages'
+
+describe "Messages" do
+ before(:each) do
+ CeilingCat::Storage::Hash.send "clear" # Clear the storage
+
+ subdomain = "subdomain"
+ token = "1234abcd"
+ plugins = [CeilingCat::Plugin::Messages]
+
+ FakeWeb.register_uri(:get, "https://#{token}:X@#{subdomain}.campfirenow.com/rooms.json", :body => fixture('campfire/rooms.json'), :status => ["200"])
+ FakeWeb.register_uri(:get, "https://#{token}:X@#{subdomain}.campfirenow.com/room/80749.json", :body => fixture('campfire/room.json'), :status => ["200"])
+ FakeWeb.register_uri(:get, "https://#{token}:X@#{subdomain}.campfirenow.com/users/me.json", :body => fixture('campfire/me.json'), :status => ["200"])
+
+ @connection = CeilingCat::Campfire::Connection.new(OpenStruct.new({:service => 'campfire', :subdomain => subdomain, :token => token, :room => 'Room 1', :plugins => plugins}))
+ @room = CeilingCat::Campfire::Room.new(:connection => @connection, :room_name => @connection.config.room)
+ end
+
+ describe "base methods" do
+ it "should save and list a message" do
+ CeilingCat::Plugin::Messages.add(:to => "Anna", :from => "Dutch", :body => "Get to the choppa!")
+ CeilingCat::Plugin::Messages.list.should include({:to => "Anna", :from => "Dutch", :body => "Get to the choppa!"})
+ end
+
+ it "should search messages" do
+ CeilingCat::Plugin::Messages.add(:to => "Anna", :from => "Dutch", :body => "Get to the choppa!")
+ CeilingCat::Plugin::Messages.add(:to => "Dutch", :from => "Predator", :body => "clikclikclikclikclik")
+ CeilingCat::Plugin::Messages.messages_for("Anna").should == [{:to => "Anna", :from => "Dutch", :body => "Get to the choppa!"}]
+ end
+ end
+
+ describe "commands" do
+ # Tests for commands
+ describe "from a guest user" do
+ before(:each) do
+ @guest_user = CeilingCat::User.new("Guest", :id => 12345, :role => "guest")
+ end
+
+ describe "calling the 'message for' command" do
+ it "should save a message if the receipient isn't around" do
+ event = CeilingCat::Event.new(@room,"!message for Chris Warren: You need to update Ceiling Cat.", @guest_user)
+ @room.should_receive(:say).with("Message saved! I'll deliver it the next time Chris Warren is around.")
+ CeilingCat::Plugin::Messages.new(event).handle
+ CeilingCat::Plugin::Messages.list.should include({:to => "Chris Warren", :from => "Guest", :body => "You need to update Ceiling Cat."})
+ end
+
+ it "should not save a message if the receipient is around" do
+ event = CeilingCat::Event.new(@room,"!message for Jane Doe: You need to update Ceiling Cat.", @guest_user)
+ @room.should_receive(:say).with("Why leave that messsage? Jane Doe is here!")
+ CeilingCat::Plugin::Messages.new(event).handle
+ CeilingCat::Plugin::Messages.list.should == []
+ end
+ end
+ end
+
+ describe "from a registered user" do
+ before(:each) do
+ @registered_user = CeilingCat::User.new("Member", :id => 12345, :role => "member")
+ end
+
+ describe "calling the 'message for' command" do
+ it "save a message if the recipient isn't around" do
+ event = CeilingCat::Event.new(@room,"!message for Chris Warren: You need to update Ceiling Cat.", @registered_user)
+ @room.should_receive(:say).with("Message saved! I'll deliver it the next time Chris Warren is around.")
+ CeilingCat::Plugin::Messages.new(event).handle
+ CeilingCat::Plugin::Messages.list.should include({:to => "Chris Warren", :from => "Member", :body => "You need to update Ceiling Cat."})
+ end
+
+ it "should not save a message if the receipient is around" do
+ event = CeilingCat::Event.new(@room,"!message for Jane Doe: You need to update Ceiling Cat.", @registered_user)
+ @room.should_receive(:say).with("Why leave that messsage? Jane Doe is here!")
+ CeilingCat::Plugin::Messages.new(event).handle
+ CeilingCat::Plugin::Messages.list.should == []
+ end
+ end
+ end
+ end
+
+ describe 'entrance and exit' do
+ # Tests for things to run when a user enters or exits the room
+ describe "entering the room" do
+ describe "as a guest" do
+ before(:each) do
+ CeilingCat::Plugin::Messages.add(:to => "Anna", :from => "Dutch", :body => "Get to the choppa!")
+ @guest_user = CeilingCat::User.new("Anna", :id => 12345, :role => "guest", :type => :entrance)
+ @event = CeilingCat::Event.new(@room, nil, @guest_user, :type => :entrance)
+ end
+
+ it "should deliver the message and return a response of false" do
+ @room.should_receive(:say).with(["Hey Anna! I have a message to deliver to you:", "From Dutch: Get to the choppa!"])
+ response = CeilingCat::Plugin::Messages.new(@event).handle
+ response.should == false
+ end
+ end
+
+ describe "as a member" do
+ before(:each) do
+ CeilingCat::Plugin::Messages.add(:to => "Anna", :from => "Dutch", :body => "Get to the choppa!")
+ @registered_user = CeilingCat::User.new("Anna", :id => 12345, :role => "member", :type => :entrance)
+ @event = CeilingCat::Event.new(@room, nil, @registered_user, :type => :entrance)
+ end
+
+ it "should deliver the message and return a response of false" do
+ @room.should_receive(:say).with(["Hey Anna! I have a message to deliver to you:", "From Dutch: Get to the choppa!"])
+ response = CeilingCat::Plugin::Messages.new(@event).handle
+ response.should == false
+ end
+ end
+ end
+ end
+end

0 comments on commit 97a69a8

Please sign in to comment.