redis-protocol 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/lib/redis-protocol.rb +142 -0
- data/spec/redis-protocol/request_spec.rb +54 -0
- data/spec/redis-protocol_spec.rb +1 -0
- data/spec/spec_helper.rb +12 -0
- metadata +160 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
redis-protocol
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem 'rake'
|
10
|
+
gem 'jeweler', '~> 1.8.4'
|
11
|
+
gem 'rspec', '~> 2.8.0'
|
12
|
+
gem 'yard', '~> 0.7'
|
13
|
+
gem 'rdoc', '~> 3.12'
|
14
|
+
gem 'bundler'
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.8.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rdoc
|
11
|
+
json (1.7.7)
|
12
|
+
rake (10.0.3)
|
13
|
+
rdoc (3.12.2)
|
14
|
+
json (~> 1.4)
|
15
|
+
rspec (2.8.0)
|
16
|
+
rspec-core (~> 2.8.0)
|
17
|
+
rspec-expectations (~> 2.8.0)
|
18
|
+
rspec-mocks (~> 2.8.0)
|
19
|
+
rspec-core (2.8.0)
|
20
|
+
rspec-expectations (2.8.0)
|
21
|
+
diff-lcs (~> 1.1.2)
|
22
|
+
rspec-mocks (2.8.0)
|
23
|
+
yard (0.8.6.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler
|
30
|
+
jeweler (~> 1.8.4)
|
31
|
+
rake
|
32
|
+
rdoc (~> 3.12)
|
33
|
+
rspec (~> 2.8.0)
|
34
|
+
yard (~> 0.7)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Jeong, Jiung
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= redis-protocol
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to redis-protocol
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2013 Jeong, Jiung. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "redis-protocol"
|
18
|
+
gem.homepage = "http://github.com/astral1/redis-protocol"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Redis protocol parser}
|
21
|
+
gem.description = gem.summary
|
22
|
+
gem.email = "ethernuiel@sanultari.com"
|
23
|
+
gem.authors = ["Jeong, Jiung"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'yard'
|
42
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# start with *, $, alpha-numeric, \r
|
2
|
+
|
3
|
+
module RedisProtocol
|
4
|
+
class Request
|
5
|
+
attr_reader :raw_data, :type, :component_count, :components
|
6
|
+
|
7
|
+
def initialize(data)
|
8
|
+
@raw_data = data
|
9
|
+
@type, @components, @component_count, @current = nil, [], 0, ''
|
10
|
+
@next_length = 0
|
11
|
+
parse
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def parse
|
17
|
+
@raw_data.chars.each do |c|
|
18
|
+
@type ||= c.eql?('*') ? :standard : :inline
|
19
|
+
|
20
|
+
send(@type, c)
|
21
|
+
end
|
22
|
+
|
23
|
+
complete_token
|
24
|
+
@component_count += @components.count if @type == :inline
|
25
|
+
end
|
26
|
+
=begin
|
27
|
+
Inline Parser
|
28
|
+
=end
|
29
|
+
def inline(char)
|
30
|
+
c = char.strip
|
31
|
+
|
32
|
+
if c.empty? and not @current.empty?
|
33
|
+
complete_token
|
34
|
+
else
|
35
|
+
@current += c
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
=begin
|
40
|
+
Standard Parser
|
41
|
+
=end
|
42
|
+
|
43
|
+
def standard(char)
|
44
|
+
@token_type ||= detect_token_type char
|
45
|
+
@doubt_str = ''
|
46
|
+
|
47
|
+
send(@token_type, char)
|
48
|
+
end
|
49
|
+
|
50
|
+
def args_counts(char)
|
51
|
+
return if char.eql? '*'
|
52
|
+
|
53
|
+
tokenize char do
|
54
|
+
@component_count = @current.to_i
|
55
|
+
clean_context
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def length(char)
|
60
|
+
return if char.eql? '$'
|
61
|
+
|
62
|
+
tokenize char do
|
63
|
+
@next_length = @current.to_i
|
64
|
+
clean_context
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def payload(char)
|
69
|
+
return if char.eql? '$'
|
70
|
+
|
71
|
+
@next_length = 0 if @current.length == @next_length
|
72
|
+
|
73
|
+
tokenize char do
|
74
|
+
@next_length = 0
|
75
|
+
complete_token
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def tokenize(char, &block)
|
80
|
+
reach_delimiter = consume_delimiter(char)
|
81
|
+
|
82
|
+
if reach_delimiter
|
83
|
+
if @doubt_str.empty?
|
84
|
+
block.call
|
85
|
+
end
|
86
|
+
return
|
87
|
+
end
|
88
|
+
|
89
|
+
@current += char
|
90
|
+
end
|
91
|
+
|
92
|
+
=begin
|
93
|
+
Common Utilities
|
94
|
+
=end
|
95
|
+
|
96
|
+
def complete_token
|
97
|
+
@components << @current
|
98
|
+
clean_context
|
99
|
+
end
|
100
|
+
|
101
|
+
def clean_context
|
102
|
+
@current = ''
|
103
|
+
@token_type = nil
|
104
|
+
end
|
105
|
+
|
106
|
+
def consume_delimiter(char)
|
107
|
+
return false if @next_length != 0
|
108
|
+
|
109
|
+
if @doubt and char.eql? "\n"
|
110
|
+
@doubt_str = ''
|
111
|
+
@doubt = false
|
112
|
+
return true
|
113
|
+
end
|
114
|
+
|
115
|
+
if not @doubt and char.eql? "\r"
|
116
|
+
@doubt_str = "\r"
|
117
|
+
@doubt = true
|
118
|
+
return true
|
119
|
+
end
|
120
|
+
|
121
|
+
@doubt = false
|
122
|
+
@current += @doubt_str unless @doubt_str.empty?
|
123
|
+
@doubt_str = ''
|
124
|
+
return false
|
125
|
+
end
|
126
|
+
|
127
|
+
def detect_token_type(char)
|
128
|
+
case char
|
129
|
+
when '*'
|
130
|
+
:args_counts
|
131
|
+
when '$'
|
132
|
+
:length
|
133
|
+
else
|
134
|
+
:payload
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
class Response
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe RedisProtocol::Request do
|
4
|
+
it 'initialize with data' do
|
5
|
+
RedisProtocol::Request.new 'ping'
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should get raw data as original' do
|
9
|
+
request = RedisProtocol::Request.new 'ping'
|
10
|
+
request.raw_data.should eql 'ping'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should parse inline command' do
|
14
|
+
request = RedisProtocol::Request.new 'ping'
|
15
|
+
request.components[0].should eql 'ping'
|
16
|
+
request.component_count.should be 1
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should parse inline command with parameter' do
|
20
|
+
request = RedisProtocol::Request.new 'keys *'
|
21
|
+
request.components[0].should eql 'keys'
|
22
|
+
request.component_count.should be 2
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should parse inline command with multi whitespaces' do
|
26
|
+
request = RedisProtocol::Request.new 'keys *'
|
27
|
+
request.components[0].should eql 'keys'
|
28
|
+
request.component_count.should be 2
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should parse standard command' do
|
32
|
+
request = RedisProtocol::Request.new "*1\r\n$4\r\nping"
|
33
|
+
request.components[0].should eql 'ping'
|
34
|
+
request.component_count.should be 1
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should parse standard command with parameter' do
|
38
|
+
request = RedisProtocol::Request.new "*2\r\n$4\r\nkeys\r\n$1\r\n*"
|
39
|
+
request.components[0].should eql 'keys'
|
40
|
+
request.component_count.should be 2
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should parse standard command with multiple parameters' do
|
44
|
+
request = RedisProtocol::Request.new "*3\r\n$3\r\nset\r\n$4\r\ntest\r\n$5\r\nhello"
|
45
|
+
request.components[0].should eql 'set'
|
46
|
+
request.component_count.should be 3
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should parse standard command containing delimiter' do
|
50
|
+
request = RedisProtocol::Request.new "*3\r\n$3\r\nset\r\n$4\r\ntest\r\n$12\r\nhello\r\nworld"
|
51
|
+
request.components[2].should eql "hello\r\nworld"
|
52
|
+
request.component_count.should be 3
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'redis-protocol'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-protocol
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeong, Jiung
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: jeweler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.8.4
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.8.4
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.8.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.8.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: yard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.7'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.7'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rdoc
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '3.12'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '3.12'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: bundler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Redis protocol parser
|
111
|
+
email: ethernuiel@sanultari.com
|
112
|
+
executables: []
|
113
|
+
extensions: []
|
114
|
+
extra_rdoc_files:
|
115
|
+
- LICENSE.txt
|
116
|
+
- README.rdoc
|
117
|
+
files:
|
118
|
+
- .document
|
119
|
+
- .rspec
|
120
|
+
- .ruby-gemset
|
121
|
+
- .ruby-version
|
122
|
+
- Gemfile
|
123
|
+
- Gemfile.lock
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.rdoc
|
126
|
+
- Rakefile
|
127
|
+
- VERSION
|
128
|
+
- lib/redis-protocol.rb
|
129
|
+
- spec/redis-protocol/request_spec.rb
|
130
|
+
- spec/redis-protocol_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
homepage: http://github.com/astral1/redis-protocol
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
none: false
|
141
|
+
requirements:
|
142
|
+
- - ! '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
segments:
|
146
|
+
- 0
|
147
|
+
hash: -1380845290125949891
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
none: false
|
150
|
+
requirements:
|
151
|
+
- - ! '>='
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
requirements: []
|
155
|
+
rubyforge_project:
|
156
|
+
rubygems_version: 1.8.25
|
157
|
+
signing_key:
|
158
|
+
specification_version: 3
|
159
|
+
summary: Redis protocol parser
|
160
|
+
test_files: []
|