jstp 0.5.7 → 1.0.2
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.md +26 -3
- data/jstp.gemspec +2 -1
- data/lib/jstp.rb +23 -11
- data/lib/jstp/configuration.rb +68 -0
- data/lib/jstp/controller.rb +49 -11
- data/lib/jstp/dispatch.rb +106 -0
- data/lib/jstp/engine.rb +77 -13
- data/lib/jstp/version.rb +1 -1
- data/lib/reader/jstp/dispatch.rb +36 -0
- data/lib/reader/jstp/engine.rb +66 -0
- data/lib/writer/jstp/dispatch.rb +95 -0
- data/samples/api-1.5/modular_style.rb +22 -0
- data/samples/api-1.5/references.rb +12 -0
- data/samples/api-1.5/sinatra_style.rb +18 -0
- data/samples/api-2.0/a_la_rack.rb +22 -0
- data/samples/api-2.0/clearer_sample.rb +12 -0
- data/samples/api-2.0/middleware.rb +23 -0
- data/samples/api-2.0/references.rb +11 -0
- data/samples/diff.rb +12 -0
- data/samples/hooks.rb +9 -0
- data/samples/micro.rb +6 -0
- data/samples/new_api.rb +62 -0
- data/samples/websocket.rb +11 -0
- data/spec/jstp/engine_spec.rb +39 -0
- data/spec/spec_helper.rb +0 -1
- metadata +40 -32
- data/features/map_a_la_rest.feature +0 -148
- data/features/step_definitions/map_a_la_rest_steps.rb +0 -19
- data/features/support/env.rb +0 -11
- data/lib/jstp/api.rb +0 -25
- data/lib/jstp/base.rb +0 -7
- data/lib/jstp/connector.rb +0 -17
- data/lib/jstp/tcp.rb +0 -12
- data/lib/jstp/web_socket.rb +0 -45
- data/lib/reader/jstp/connector.rb +0 -31
- data/lib/writer/jstp/connector.rb +0 -46
- data/spec/jstp/api_spec.rb +0 -79
- data/spec/jstp/base_spec.rb +0 -5
- data/spec/jstp/connector_spec.rb +0 -16
- data/spec/jstp/tcp_spec.rb +0 -7
- data/spec/jstp/web_socket_spec.rb +0 -78
- data/spec/reader/jstp/connector_spec.rb +0 -32
- data/spec/writer/jstp/connector_spec.rb +0 -71
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe JSTP::Engine do
|
4
|
+
describe '#dispatch' do
|
5
|
+
context 'a valid Dispatch aimed at this host' do
|
6
|
+
context 'the target class and method exist' do
|
7
|
+
before do
|
8
|
+
JSTP.config do |c|
|
9
|
+
c.hostname = 'rspec.tests'
|
10
|
+
c.logger = Logger.new $stdout
|
11
|
+
c.logger.level = Logger::FATAL
|
12
|
+
c.environment :production
|
13
|
+
end
|
14
|
+
|
15
|
+
@dispatch = JSTP::Dispatch.new :get, "rspec.tests/User"
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should call the method in the target class' do
|
19
|
+
class Eng < JSTP::Engine
|
20
|
+
class User < JSTP::Controller
|
21
|
+
def get
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Eng::User.any_instance.should_receive :get
|
27
|
+
|
28
|
+
eng = Eng.new :test
|
29
|
+
dummy_client = stub 'client'
|
30
|
+
eng.dispatch @dispatch, dummy_client
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'the target class does not exist' do
|
35
|
+
it 'should run the method in the engine'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jstp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-02-05 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: em-websocket
|
@@ -94,14 +94,14 @@ dependencies:
|
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
- !ruby/object:Gem::Dependency
|
97
|
-
name:
|
97
|
+
name: oj
|
98
98
|
requirement: !ruby/object:Gem::Requirement
|
99
99
|
none: false
|
100
100
|
requirements:
|
101
101
|
- - ! '>='
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
|
-
type: :
|
104
|
+
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
none: false
|
@@ -109,6 +109,22 @@ dependencies:
|
|
109
109
|
- - ! '>='
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: rspec
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: 2.1.0
|
120
|
+
type: :development
|
121
|
+
prerelease: false
|
122
|
+
version_requirements: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 2.1.0
|
112
128
|
- !ruby/object:Gem::Dependency
|
113
129
|
name: cucumber
|
114
130
|
requirement: !ruby/object:Gem::Requirement
|
@@ -138,29 +154,30 @@ files:
|
|
138
154
|
- LICENSE
|
139
155
|
- README.md
|
140
156
|
- Rakefile
|
141
|
-
- features/map_a_la_rest.feature
|
142
|
-
- features/step_definitions/map_a_la_rest_steps.rb
|
143
|
-
- features/support/env.rb
|
144
157
|
- jstp.gemspec
|
145
158
|
- lib/jstp.rb
|
146
|
-
- lib/jstp/
|
147
|
-
- lib/jstp/base.rb
|
148
|
-
- lib/jstp/connector.rb
|
159
|
+
- lib/jstp/configuration.rb
|
149
160
|
- lib/jstp/controller.rb
|
161
|
+
- lib/jstp/dispatch.rb
|
150
162
|
- lib/jstp/engine.rb
|
151
|
-
- lib/jstp/tcp.rb
|
152
163
|
- lib/jstp/version.rb
|
153
|
-
- lib/jstp/
|
154
|
-
- lib/reader/jstp/
|
155
|
-
- lib/writer/jstp/
|
156
|
-
-
|
157
|
-
-
|
158
|
-
-
|
159
|
-
-
|
160
|
-
-
|
161
|
-
-
|
164
|
+
- lib/reader/jstp/dispatch.rb
|
165
|
+
- lib/reader/jstp/engine.rb
|
166
|
+
- lib/writer/jstp/dispatch.rb
|
167
|
+
- samples/api-1.5/modular_style.rb
|
168
|
+
- samples/api-1.5/references.rb
|
169
|
+
- samples/api-1.5/sinatra_style.rb
|
170
|
+
- samples/api-2.0/a_la_rack.rb
|
171
|
+
- samples/api-2.0/clearer_sample.rb
|
172
|
+
- samples/api-2.0/middleware.rb
|
173
|
+
- samples/api-2.0/references.rb
|
174
|
+
- samples/diff.rb
|
175
|
+
- samples/hooks.rb
|
176
|
+
- samples/micro.rb
|
177
|
+
- samples/new_api.rb
|
178
|
+
- samples/websocket.rb
|
179
|
+
- spec/jstp/engine_spec.rb
|
162
180
|
- spec/spec_helper.rb
|
163
|
-
- spec/writer/jstp/connector_spec.rb
|
164
181
|
homepage: https://github.com/Fetcher/jstp
|
165
182
|
licenses: []
|
166
183
|
post_install_message:
|
@@ -181,19 +198,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
198
|
version: '0'
|
182
199
|
requirements: []
|
183
200
|
rubyforge_project:
|
184
|
-
rubygems_version: 1.8.
|
201
|
+
rubygems_version: 1.8.25
|
185
202
|
signing_key:
|
186
203
|
specification_version: 3
|
187
204
|
summary: Reference implementation for the sketch protocol JSTP
|
188
205
|
test_files:
|
189
|
-
-
|
190
|
-
- features/step_definitions/map_a_la_rest_steps.rb
|
191
|
-
- features/support/env.rb
|
192
|
-
- spec/jstp/api_spec.rb
|
193
|
-
- spec/jstp/base_spec.rb
|
194
|
-
- spec/jstp/connector_spec.rb
|
195
|
-
- spec/jstp/tcp_spec.rb
|
196
|
-
- spec/jstp/web_socket_spec.rb
|
197
|
-
- spec/reader/jstp/connector_spec.rb
|
206
|
+
- spec/jstp/engine_spec.rb
|
198
207
|
- spec/spec_helper.rb
|
199
|
-
- spec/writer/jstp/connector_spec.rb
|
@@ -1,148 +0,0 @@
|
|
1
|
-
Feature: Map á la REST
|
2
|
-
|
3
|
-
Scenario: Map a really simple dispatch to the correct class
|
4
|
-
Given the class:
|
5
|
-
"""
|
6
|
-
class User < JSTP::Controller
|
7
|
-
def get
|
8
|
-
Testing.test_log << "hola"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
"""
|
12
|
-
When I send the dispatch:
|
13
|
-
"""
|
14
|
-
{
|
15
|
-
"resource":["localhost", "User"],
|
16
|
-
"method": "GET"
|
17
|
-
}
|
18
|
-
"""
|
19
|
-
Then I should have 'hola' in the test log
|
20
|
-
|
21
|
-
Scenario: A dispatch with a body
|
22
|
-
Given the class:
|
23
|
-
"""
|
24
|
-
class Article < JSTP::Controller
|
25
|
-
def post params
|
26
|
-
Testing.test_log << params["body"]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
"""
|
30
|
-
When I send the dispatch:
|
31
|
-
"""
|
32
|
-
{
|
33
|
-
"resource": ["localhost", "Article"],
|
34
|
-
"method": "POST",
|
35
|
-
"body": "some text"
|
36
|
-
}
|
37
|
-
"""
|
38
|
-
Then I should have 'some text' in the test log
|
39
|
-
|
40
|
-
Scenario: A compliant dispatch
|
41
|
-
Given the class:
|
42
|
-
"""
|
43
|
-
class ForDispatch < JSTP::Controller
|
44
|
-
def get params
|
45
|
-
Testing.test_log.concat [
|
46
|
-
params["body"],
|
47
|
-
@token,
|
48
|
-
@timestamp,
|
49
|
-
@referer,
|
50
|
-
@protocol,
|
51
|
-
@resource,
|
52
|
-
@method
|
53
|
-
]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
"""
|
57
|
-
When I send the dispatch:
|
58
|
-
"""
|
59
|
-
{
|
60
|
-
"protocol": ["JSTP", "0.1"],
|
61
|
-
"method": "GET",
|
62
|
-
"resource": ["localhost", "ForDispatch"],
|
63
|
-
"token": "523asdf243",
|
64
|
-
"timestamp": 15453242542245,
|
65
|
-
"referer": ["localhost", "Test"],
|
66
|
-
"body": {
|
67
|
-
"data": "some data"
|
68
|
-
}
|
69
|
-
}
|
70
|
-
"""
|
71
|
-
Then I should have '["JSTP", "0.1"]' in the test log
|
72
|
-
Then I should have 'GET' in the test log
|
73
|
-
Then I should have '["localhost", "Test"]' in the test log
|
74
|
-
Then I should have 15453242542245 in the test log
|
75
|
-
Then I should have '523asdf243' in the test log
|
76
|
-
Then I should have '{"data": "some data"}' in the test log
|
77
|
-
Then I should have '["localhost", "ForDispatch"]' in the test log
|
78
|
-
|
79
|
-
Scenario: A dispatch with query arguments
|
80
|
-
Given the class:
|
81
|
-
"""
|
82
|
-
class Argumented < JSTP::Controller
|
83
|
-
def put params
|
84
|
-
Testing.test_log << params["query"]
|
85
|
-
end
|
86
|
-
end
|
87
|
-
"""
|
88
|
-
When I send the dispatch:
|
89
|
-
"""
|
90
|
-
{
|
91
|
-
"method": "PUT",
|
92
|
-
"resource": ["localhost", "Argumented", "querydata"]
|
93
|
-
}
|
94
|
-
"""
|
95
|
-
Then I should have '["querydata"]' in the test log
|
96
|
-
|
97
|
-
Scenario: Mixed arguments in the query
|
98
|
-
Given the class:
|
99
|
-
"""
|
100
|
-
class Mixed
|
101
|
-
class Argument < JSTP::Controller
|
102
|
-
def delete params
|
103
|
-
Testing.test_log << params["query"]
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
"""
|
108
|
-
When I send the dispatch:
|
109
|
-
"""
|
110
|
-
{
|
111
|
-
"method": "DELETE",
|
112
|
-
"resource": ["localhost", "Mixed", "54s3453", "Argument", "35asdf"]
|
113
|
-
}
|
114
|
-
"""
|
115
|
-
Then I should have '["54s3453", "35asdf"]' in the test log
|
116
|
-
|
117
|
-
Scenario: A complex example just for the fun
|
118
|
-
Given the class:
|
119
|
-
"""
|
120
|
-
class Deeper
|
121
|
-
class Inside
|
122
|
-
class Klass < JSTP::Controller
|
123
|
-
def get params
|
124
|
-
Testing.test_log << params["query"]
|
125
|
-
Testing.test_log << params["body"]
|
126
|
-
Testing.test_log << @protocol
|
127
|
-
Testing.test_log << @token
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
"""
|
133
|
-
When I send the dispatch:
|
134
|
-
"""
|
135
|
-
{
|
136
|
-
"protocol": ["JSTP", "0.1"],
|
137
|
-
"resource": ["localhost", "Deeper", "52345", "Inside", "Klass", "53afas", "54234"],
|
138
|
-
"method": "GET",
|
139
|
-
"token": ["25353"],
|
140
|
-
"body": {
|
141
|
-
"id": 20
|
142
|
-
}
|
143
|
-
}
|
144
|
-
"""
|
145
|
-
Then I should have '["JSTP", "0.1"]' in the test log
|
146
|
-
Then I should have '["52345", "53afas", "54234"]' in the test log
|
147
|
-
Then I should have '["25353"]' in the test log
|
148
|
-
Then I should have '{"id": 20}' in the test log
|
@@ -1,19 +0,0 @@
|
|
1
|
-
Given /^the class:$/ do |code|
|
2
|
-
eval code
|
3
|
-
end
|
4
|
-
|
5
|
-
When /^I send the dispatch:$/ do |dispatch|
|
6
|
-
JSTP::Engine.instance.dispatch JSON.parse dispatch
|
7
|
-
end
|
8
|
-
|
9
|
-
Then /^I should have '(.+?)' in the test log$/ do |text|
|
10
|
-
begin
|
11
|
-
Testing.test_log.should include JSON.parse text
|
12
|
-
rescue JSON::ParserError => e
|
13
|
-
Testing.test_log.should include text
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
Then /^I should have (\d+) in the test log$/ do |number|
|
18
|
-
Testing.test_log.should include number.to_i
|
19
|
-
end
|
data/features/support/env.rb
DELETED
data/lib/jstp/api.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
module JSTP
|
3
|
-
module API
|
4
|
-
def dispatch *args, &block
|
5
|
-
if args.empty?
|
6
|
-
if block
|
7
|
-
Connector.instance.block = block
|
8
|
-
Connector.instance.from.send Connector.instance.strategy.inbound
|
9
|
-
else
|
10
|
-
Connector.instance
|
11
|
-
end
|
12
|
-
else
|
13
|
-
Connector.instance.to.send Connector.instance.strategy.outbound, args.first
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def port data
|
18
|
-
Connector.instance.port = SymbolMatrix data
|
19
|
-
end
|
20
|
-
|
21
|
-
def strategy data
|
22
|
-
Connector.instance.strategy = SymbolMatrix data
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
data/lib/jstp/base.rb
DELETED
data/lib/jstp/connector.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
module JSTP
|
3
|
-
|
4
|
-
# Handles the connection functionality for this JSTP reference implementation
|
5
|
-
class Connector
|
6
|
-
include Singleton
|
7
|
-
include Discoverer::Writer
|
8
|
-
include Discoverer::Reader
|
9
|
-
|
10
|
-
attr_accessor :port, :strategy, :block
|
11
|
-
|
12
|
-
def initialize
|
13
|
-
@port = SymbolMatrix inbound: 33333, outbound: 33333
|
14
|
-
@strategy = SymbolMatrix inbound: :tcp, outbound: :tcp
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
data/lib/jstp/tcp.rb
DELETED
data/lib/jstp/web_socket.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
module JSTP
|
2
|
-
class WebSocket
|
3
|
-
include Singleton
|
4
|
-
|
5
|
-
def sockets
|
6
|
-
@sockets ||= {}
|
7
|
-
end
|
8
|
-
|
9
|
-
def server_setup
|
10
|
-
@server_setup ||= proc { |server|
|
11
|
-
server.onopen {
|
12
|
-
JSTP::WebSocket.instance.sockets[UUID.new.generate] = server
|
13
|
-
}
|
14
|
-
|
15
|
-
server.onmessage { |message|
|
16
|
-
message = JSON.parse message
|
17
|
-
JSTP::Engine.instance.dispatch(message,
|
18
|
-
JSTP::WebSocket.instance.sockets.key(server))
|
19
|
-
}
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
def event_machine
|
24
|
-
@event_machine ||= proc {
|
25
|
-
::EM::WebSocket.start host: "0.0.0.0",
|
26
|
-
port: Connector.instance.port.inbound,
|
27
|
-
&WebSocket.instance.server_setup
|
28
|
-
}
|
29
|
-
end
|
30
|
-
|
31
|
-
def event_on_message
|
32
|
-
@event_on_message ||= proc { |message|
|
33
|
-
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
def event_on_open
|
38
|
-
end
|
39
|
-
|
40
|
-
def client resource
|
41
|
-
::EM::WebSocketClient
|
42
|
-
.connect "ws://#{resource.first}:#{Connector.instance.port.outbound}/#{resource[1..-1].join('/')}"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|