em_json_connection 0.0.1
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/lib/em_json_connection/version.rb +3 -0
- data/lib/em_json_connection.rb +63 -0
- data/spec/em_json_connection_spec.rb +99 -0
- metadata +78 -0
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'eventmachine'
|
2
|
+
require 'yajl'
|
3
|
+
|
4
|
+
module EM::JsonConnection; end
|
5
|
+
|
6
|
+
module EM::JsonConnection::Base
|
7
|
+
def post_init
|
8
|
+
init_parser
|
9
|
+
@encoder = Yajl::Encoder.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def init_parser
|
13
|
+
@parser = Yajl::Parser.new(:symbolize_keys => true)
|
14
|
+
@parser.on_parse_complete = method(:json_parsed)
|
15
|
+
end
|
16
|
+
|
17
|
+
def receive_data(data)
|
18
|
+
# p data
|
19
|
+
@parser << data
|
20
|
+
rescue Yajl::ParseError => error
|
21
|
+
puts "Yajl::ParseError: #{error}"
|
22
|
+
init_parser
|
23
|
+
end
|
24
|
+
|
25
|
+
def json_parsed(hash)
|
26
|
+
puts 'to be implemented in the subclass'
|
27
|
+
end
|
28
|
+
|
29
|
+
def send_data(data)
|
30
|
+
super @encoder.encode(data)
|
31
|
+
# rescue JSON::GeneratorError => error
|
32
|
+
# puts "JSON::GeneratorError: #{error}"
|
33
|
+
# @encoder = Yajl::Encoder.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module EM::JsonConnection::Server
|
38
|
+
include EM::JsonConnection::Base
|
39
|
+
|
40
|
+
def self.included(base)
|
41
|
+
base.extend ClassMethods
|
42
|
+
end
|
43
|
+
|
44
|
+
module ClassMethods
|
45
|
+
def start_at(host, port=nil)
|
46
|
+
EM.start_server(host, port, self)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module EM::JsonConnection::Client
|
52
|
+
include EM::JsonConnection::Base
|
53
|
+
|
54
|
+
def self.included(base)
|
55
|
+
base.extend ClassMethods
|
56
|
+
end
|
57
|
+
|
58
|
+
module ClassMethods
|
59
|
+
def connect_to(host, port=nil)
|
60
|
+
EM.connect(host, port, self)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require File.join( File.expand_path(File.dirname(__FILE__)), '../lib/em_json_connection' )
|
2
|
+
|
3
|
+
class ConnectionSuperClass
|
4
|
+
def send_data(d); end
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestConnection < ConnectionSuperClass
|
8
|
+
include EM::JsonConnection::Base
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestServer
|
12
|
+
include EM::JsonConnection::Server
|
13
|
+
end
|
14
|
+
class TestClient
|
15
|
+
include EM::JsonConnection::Client
|
16
|
+
end
|
17
|
+
|
18
|
+
describe EM::JsonConnection::Base do
|
19
|
+
before(:each) do
|
20
|
+
@connection = TestConnection.new(File.join( File.expand_path(File.dirname(__FILE__)), 'test_socket' ))
|
21
|
+
end
|
22
|
+
describe "#post_init" do
|
23
|
+
it "should initialize the parser" do
|
24
|
+
@connection.should_receive(:init_parser).once
|
25
|
+
@connection.post_init
|
26
|
+
end
|
27
|
+
it "should set the encoder" do
|
28
|
+
Yajl::Encoder.should_receive(:new).and_return(:some_encoder)
|
29
|
+
@connection.post_init
|
30
|
+
@connection.instance_variable_get("@encoder").should == :some_encoder
|
31
|
+
end
|
32
|
+
end
|
33
|
+
describe "#init_parser" do
|
34
|
+
before(:each) do
|
35
|
+
@parser_mock = mock(:some_parser, :on_parse_complete= => true)
|
36
|
+
Yajl::Parser.stub!(:new => @parser_mock)
|
37
|
+
end
|
38
|
+
it "should set the parser" do
|
39
|
+
Yajl::Parser.should_receive(:new).with(:symbolize_keys => true).and_return(@parser_mock)
|
40
|
+
@connection.init_parser
|
41
|
+
@connection.instance_variable_get("@parser").should == @parser_mock
|
42
|
+
end
|
43
|
+
it "should set the callback-method for the parser" do
|
44
|
+
@parser_mock.should_receive(:on_parse_complete=).with(@connection.method(:json_parsed))
|
45
|
+
@connection.init_parser
|
46
|
+
end
|
47
|
+
end
|
48
|
+
describe "#receive_data" do
|
49
|
+
before(:each) do
|
50
|
+
@parser_mock = mock(:some_parser, :on_parse_complete= => true)
|
51
|
+
Yajl::Parser.stub!(:new => @parser_mock)
|
52
|
+
@connection.init_parser
|
53
|
+
end
|
54
|
+
it "should feed the data to the parser" do
|
55
|
+
@parser_mock.should_receive(:<<).with('foo')
|
56
|
+
@connection.receive_data('foo')
|
57
|
+
end
|
58
|
+
describe "when a parsing error occurs" do
|
59
|
+
before(:each) do
|
60
|
+
@parser_mock.stub!(:<<).and_raise(Yajl::ParseError.new('some error'))
|
61
|
+
end
|
62
|
+
it "should rescue the error" do
|
63
|
+
lambda{ @connection.receive_data('foo') }.should_not raise_error
|
64
|
+
end
|
65
|
+
it "should re-initialize the parser" do
|
66
|
+
@connection.should_receive(:init_parser)
|
67
|
+
@connection.receive_data('foo')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
describe "#send_data" do
|
72
|
+
before(:each) do
|
73
|
+
@connection.post_init
|
74
|
+
end
|
75
|
+
it "should call super with the data encoded" do
|
76
|
+
@connection.instance_variable_get('@encoder').should_receive(:encode).with({:a => 1})
|
77
|
+
@connection.send_data({:a => 1})
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe EM::JsonConnection::Server do
|
83
|
+
describe ".start_at" do
|
84
|
+
it "should start an EM server" do
|
85
|
+
EM.should_receive(:start_server).with('foo', nil, TestServer)
|
86
|
+
TestServer.start_at 'foo'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
describe EM::JsonConnection::Client do
|
93
|
+
describe ".connect_to" do
|
94
|
+
it "should connect to an EM server" do
|
95
|
+
EM.should_receive(:connect).with('foo', nil, TestClient)
|
96
|
+
TestClient.connect_to 'foo'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em_json_connection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Niko Dittmann
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-13 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description: Adds a JSON layer to a plain Eventmachine socket connection
|
34
|
+
email: mail+git@niko-dittmann.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- lib/em_json_connection/version.rb
|
43
|
+
- lib/em_json_connection.rb
|
44
|
+
- spec/em_json_connection_spec.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/niko/em_json_connection
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: nowarning
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Adds a JSON layer to a plain Eventmachine socket connection
|
77
|
+
test_files:
|
78
|
+
- spec/em_json_connection_spec.rb
|