eventmachine-redis 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown ADDED
@@ -0,0 +1,49 @@
1
+ # eventmachine-redis
2
+
3
+ An [EventMachine](http://github.com/eventmachine/eventmachine/) based Redis client.
4
+
5
+ ## Example
6
+
7
+ EM.run do
8
+ Redis = EM::Protocols::Redis.connect
9
+
10
+ puts "Waiting for item on 'queue'..."
11
+
12
+ Redis.shift('queue', :timeout => 0) do |key, item|
13
+ puts "Item: #{item}"
14
+
15
+ EM.stop
16
+ end
17
+ end
18
+
19
+ ## Warning
20
+
21
+ This was written as a learning exercise and is currently very incomplete.
22
+
23
+ ## Credits
24
+
25
+ Based on [em-redis](http://github.com/madsimian/em-redis) by [Jonathan Broad](http://github.com/madsimian).
26
+
27
+ ## License
28
+
29
+ The MIT License
30
+
31
+ Copyright (c) 2010 Tristan Dunn
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining a copy
34
+ of this software and associated documentation files (the "Software"), to deal
35
+ in the Software without restriction, including without limitation the rights
36
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37
+ copies of the Software, and to permit persons to whom the Software is
38
+ furnished to do so, subject to the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be included in
41
+ all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
49
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ Spec::Rake::SpecTask.new do |task|
5
+ task.spec_opts = %w(--color --format specdoc)
6
+ end
7
+
8
+ begin
9
+ require 'jeweler'
10
+
11
+ Jeweler::Tasks.new do |gemspec|
12
+ gemspec.name = 'eventmachine-redis'
13
+ gemspec.summary = 'An EventMachine based Redis client.'
14
+ gemspec.description = 'An EventMachine based Redis client.'
15
+ gemspec.email = 'hello@tristandunn.com'
16
+ gemspec.homepage = 'http://github.com/tristandunn/eventmachine-redis'
17
+ gemspec.authors = ['Tristan Dunn']
18
+ end
19
+ rescue LoadError
20
+ end
21
+
22
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{eventmachine-redis}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tristan Dunn"]
12
+ s.date = %q{2010-03-28}
13
+ s.description = %q{An EventMachine based Redis client.}
14
+ s.email = %q{hello@tristandunn.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "eventmachine-redis.gemspec",
23
+ "examples/push-shift.rb",
24
+ "lib/eventmachine-redis.rb",
25
+ "spec/redis_spec.rb",
26
+ "spec/spec_helper.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/tristandunn/eventmachine-redis}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.6}
32
+ s.summary = %q{An EventMachine based Redis client.}
33
+ s.test_files = [
34
+ "spec/redis_spec.rb",
35
+ "spec/spec_helper.rb",
36
+ "examples/push-shift.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
49
+
@@ -0,0 +1,16 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+ require 'eventmachine-redis'
5
+
6
+ EM.run do
7
+ Redis = EM::Protocols::Redis.connect
8
+
9
+ puts "Waiting for item on 'queue'..."
10
+
11
+ Redis.shift('queue', :timeout => 0) do |key, item|
12
+ puts "Item: #{item}"
13
+
14
+ EM.stop
15
+ end
16
+ end
@@ -0,0 +1,113 @@
1
+ require 'eventmachine'
2
+
3
+ module EventMachine::Protocols::Redis
4
+ include EM::Deferrable
5
+
6
+ DELIMETER = "\r\n".freeze
7
+ TYPE_BULK = '$'.freeze
8
+ TYPE_SINGLE = '+'.freeze
9
+ TYPE_INTEGER = ':'.freeze
10
+ TYPE_MULTIPLE = '*'.freeze
11
+
12
+ MULTILINE_COMMANDS = %w().freeze
13
+
14
+ def self.connect(host = 'localhost', port = 6379)
15
+ EM.connect(host, port, self)
16
+ end
17
+
18
+ def initialize
19
+ @buffer = ''
20
+ @values = []
21
+ @handlers = []
22
+ @expected_values = 0
23
+ end
24
+
25
+ def connection_completed
26
+ succeed
27
+ end
28
+
29
+ def receive_data(data)
30
+ @buffer << data
31
+
32
+ while index = @buffer.index(DELIMETER)
33
+ response = @buffer.slice!(0, index + 2)
34
+
35
+ begin
36
+ process_response(response)
37
+ rescue IOError
38
+ @buffer[0...0] = response
39
+ break
40
+ end
41
+ end
42
+ end
43
+
44
+ def shift(key, options = {}, &block)
45
+ if options[:timeout]
46
+ command('BLPOP', key, options[:timeout], &block)
47
+ else
48
+ command('LPOP', key, &block)
49
+ end
50
+ end
51
+
52
+ protected
53
+
54
+ def command(*arguments, &block)
55
+ @handlers << block
56
+
57
+ callback do
58
+ command = arguments.shift
59
+ multiline = MULTILINE_COMMANDS.include?(command)
60
+ data = arguments.pop if multiline
61
+
62
+ command << " #{arguments.join(' ')}" if arguments.length > 0
63
+ command << " #{data.size}#{DELIMETER}#{data}" if multiline
64
+ command << DELIMETER
65
+
66
+ send_data(command)
67
+ end
68
+ end
69
+
70
+ def handler(*arguments)
71
+ handler = @handlers.shift
72
+ handler.call(*arguments) if handler
73
+ end
74
+
75
+ def process_response(response)
76
+ type = response[0]
77
+ arguments = response.slice(1..-3)
78
+
79
+ case type
80
+ when TYPE_BULK
81
+ length = arguments.to_i
82
+
83
+ if length > -1
84
+ if @buffer.size >= length + 2
85
+ value = @buffer.slice!(0, length)
86
+
87
+ @buffer.slice!(0, 2)
88
+ else
89
+ raise IOError
90
+ end
91
+ end
92
+
93
+ if @expected_values > 0
94
+ @values << value
95
+
96
+ if @values.size == @expected_values
97
+ handler(@values)
98
+
99
+ @values = []
100
+ @expected_values = 0
101
+ end
102
+ else
103
+ handler(value)
104
+ end
105
+ when TYPE_SINGLE
106
+ handler(arguments)
107
+ when TYPE_INTEGER
108
+ handler(arguments.to_i)
109
+ when TYPE_MULTIPLE
110
+ @expected_values = arguments.to_i
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe EventMachine::Protocols::Redis do
4
+ describe 'module' do
5
+ include EventMachine::Protocols
6
+
7
+ it 'should define delimiter constant' do
8
+ Redis::DELIMETER.should == "\r\n"
9
+ end
10
+
11
+ it 'should define type bulk constant' do
12
+ Redis::TYPE_BULK.should == '$'
13
+ end
14
+
15
+ it 'should define type single constant' do
16
+ Redis::TYPE_SINGLE.should == '+'
17
+ end
18
+
19
+ it 'should define type integer constant' do
20
+ Redis::TYPE_INTEGER.should == ':'
21
+ end
22
+
23
+ it 'should define type multiple constant' do
24
+ Redis::TYPE_MULTIPLE.should == '*'
25
+ end
26
+
27
+ it 'should define multiline commands constant' do
28
+ Redis::MULTILINE_COMMANDS.should == %w()
29
+ end
30
+
31
+ it 'should connect via EventMachine when calling Redis.connect' do
32
+ EM.should_receive(:connect).with('localhost', 6379, Redis)
33
+ Redis.connect
34
+ end
35
+ end
36
+
37
+ describe 'instance' do
38
+ before do
39
+ @instance = FakeRedis.new
40
+ @completed = false
41
+ end
42
+
43
+ it 'should succeed when the connection is completed' do
44
+ @instance.should_receive(:succeed)
45
+ @instance.connection_completed
46
+ end
47
+
48
+ it 'should be able to parse inline response' do
49
+ send_command_and_expect('OK')
50
+ @instance.receive_data("+OK\r\n")
51
+ @completed.should be_true
52
+ end
53
+
54
+ it 'should be able to parse integer responses' do
55
+ send_command_and_expect(0)
56
+ @instance.receive_data(":0\r\n")
57
+ @completed.should be_true
58
+ end
59
+
60
+ it 'should be able to parse bulk response' do
61
+ send_command_and_expect('foo')
62
+ @instance.receive_data("$3\r\n")
63
+ @instance.receive_data("foo\r\n")
64
+ @completed.should be_true
65
+ end
66
+
67
+ it 'should be able to handle nil in bulk response' do
68
+ send_command_and_expect(nil)
69
+ @instance.receive_data("$-1\r\n")
70
+ @completed.should be_true
71
+ end
72
+
73
+ it 'should be able to parse multiple bulk response' do
74
+ send_command_and_expect(['a', 'yo', 'foo'])
75
+ @instance.receive_data("*3\r\n")
76
+ @instance.receive_data("$1\r\na\r\n")
77
+ @instance.receive_data("$2\r\nyo\r\n")
78
+ @instance.receive_data("$3\r\nfoo\r\n")
79
+ @completed.should be_true
80
+ end
81
+
82
+ it 'should be able to handle nil in mulitple bulk response' do
83
+ send_command_and_expect(['a', nil, 'foo'])
84
+ @instance.receive_data("*3\r\n")
85
+ @instance.receive_data("$1\r\na\r\n")
86
+ @instance.receive_data("$-1\r\n")
87
+ @instance.receive_data("$3\r\nfoo\r\n")
88
+ @completed.should be_true
89
+ end
90
+
91
+ describe '#shift' do
92
+ it 'should generate LPOP command' do
93
+ @instance.shift('key')
94
+ @instance.sent_data.should == "LPOP key\r\n"
95
+ end
96
+
97
+ it 'should generate BLPOP command given a timeout' do
98
+ @instance.shift('key', :timeout => 0)
99
+ @instance.sent_data.should == "BLPOP key 0\r\n"
100
+ end
101
+ end
102
+
103
+ protected
104
+
105
+ def send_command_and_expect(expected, command = 'FAKE')
106
+ @instance.__send__(:command, command) do |response|
107
+ response.should == expected
108
+ @completed = true
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,19 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'eventmachine-redis'
4
+
5
+ class FakeRedis
6
+ include EventMachine::Protocols::Redis
7
+
8
+ def callback(&block)
9
+ block.call
10
+ end
11
+
12
+ def send_data(data)
13
+ sent_data << data
14
+ end
15
+
16
+ def sent_data
17
+ @buffer ||= ''
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventmachine-redis
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Tristan Dunn
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-28 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: An EventMachine based Redis client.
22
+ email: hello@tristandunn.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.markdown
29
+ files:
30
+ - README.markdown
31
+ - Rakefile
32
+ - VERSION
33
+ - eventmachine-redis.gemspec
34
+ - examples/push-shift.rb
35
+ - lib/eventmachine-redis.rb
36
+ - spec/redis_spec.rb
37
+ - spec/spec_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/tristandunn/eventmachine-redis
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: An EventMachine based Redis client.
68
+ test_files:
69
+ - spec/redis_spec.rb
70
+ - spec/spec_helper.rb
71
+ - examples/push-shift.rb