Browse files

Deserialize retrieved messages. Use redis-namespace to avoid collisions.

  • Loading branch information...
1 parent 80d3443 commit b3709a077ea19cef1b52c33b46b43f1cce11d506 @bemurphy bemurphy committed Sep 4, 2011
Showing with 25 additions and 21 deletions.
  1. +10 −10 Gemfile.lock
  2. +3 −2 cinch-memo.gemspec
  3. +6 −4 lib/cinch/plugins/memo/store/redis.rb
  4. +6 −5 test/store/redis_test.rb
View
20 Gemfile.lock
@@ -1,20 +1,23 @@
PATH
remote: .
specs:
- cinch-memo (0.1.0)
- cinch
+ cinch-memo (0.1.2)
+ cinch (~> 1.1.1)
json
redis
+ redis-namespace
GEM
remote: http://rubygems.org/
specs:
- cinch (1.1.1)
- json (1.5.1)
+ cinch (1.1.3)
+ json (1.5.4)
mocha (0.9.10)
rake
rake (0.8.7)
- redis (2.1.1)
+ redis (2.2.2)
+ redis-namespace (1.1.0)
+ redis (< 3.0.0)
riot (0.12.1)
rr
term-ansicolor
@@ -26,10 +29,7 @@ PLATFORMS
ruby
DEPENDENCIES
- cinch
cinch-memo!
- json
- mocha
- redis
+ mocha (~> 0.9.10)
riot (~> 0.12.0)
- timecop
+ timecop (~> 0.3.5)
View
5 cinch-memo.gemspec
@@ -18,10 +18,11 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
-
+
s.add_dependency 'cinch', '~>1.1.1'
s.add_dependency 'json'
- s.add_dependency 'redis'
+ s.add_dependency 'redis'
+ s.add_dependency 'redis-namespace'
s.add_development_dependency 'riot', '~>0.12.0'
s.add_development_dependency 'mocha', '~>0.9.10'
s.add_development_dependency 'timecop', '~>0.3.5'
View
10 lib/cinch/plugins/memo/store/redis.rb
@@ -1,22 +1,24 @@
require 'redis'
+require 'redis-namespace'
require 'json'
module Cinch
module Plugins
module Memo
class Redis
def initialize(host, port)
- @backend = ::Redis.new(:host => host, :port => port, :thread_safe => true)
+ redis = ::Redis.new(:host => host, :port => port, :thread_safe => true)
+ @backend = ::Redis::Namespace.new "cinch-memo", :redis => redis
end
-
+
def store(recipient, sender, message)
@backend.sadd recipient, [sender, message, Time.now.to_s].to_json
end
-
+
def retrieve(recipient)
messages = @backend.smembers recipient
@backend.del(recipient)
- messages
+ messages ? messages.collect {|m| JSON.parse(m) } : nil
end
end # Redis
View
11 test/store/redis_test.rb
@@ -3,7 +3,7 @@
context "Redis" do
dont_register!
setup { @bot = mock() }
-
+
helper :base do
Cinch::Plugins::Memo::Base.configure do |c|
c.store = :redis
@@ -22,7 +22,7 @@
context "#store" do
setup do
Timecop.freeze(Time.now)
- backend = mock() ; backend.expects(:sadd).with('bob', ['chris','yo yo', Time.now].to_json).returns(true)
+ backend = mock() ; backend.expects(:sadd).with('cinch-memo:bob', ['chris','yo yo', Time.now].to_json).returns(true)
::Redis.expects(:new).with(:host => 'localhost', :port => '6709', :thread_safe=>true).returns(backend)
Cinch::Plugins::Memo::Redis.new('localhost','6709')
end
@@ -32,11 +32,12 @@
context "#retrieve" do
setup do
Timecop.freeze(Time.now)
- backend = mock() ; backend.expects(:smembers).with("bob").returns("\[\"a\",\"b\",\"c\"\]")
- backend.expects(:del).with('bob').returns(true)
+ backend = mock() ; backend.expects(:smembers).with("cinch-memo:bob").returns(["\[\"a\",\"b\",\"c\"\]",
+ "\[\"d\",\"e\",\"f\"\]"])
+ backend.expects(:del).with('cinch-memo:bob').returns(true)
::Redis.expects(:new).with(:host => 'localhost', :port => '6709', :thread_safe=>true).returns(backend)
Cinch::Plugins::Memo::Redis.new('localhost','6709')
end
- asserts("that it retrieves message") { topic.retrieve('bob') }
+ asserts("that its retrieved messages") { topic.retrieve('bob') }.equals([%w[a b c], %w[d e f]])
end
end

0 comments on commit b3709a0

Please sign in to comment.