jstp 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *.DS_Store
@@ -0,0 +1,148 @@
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
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH << File.expand_path("../../../lib", __FILE__)
2
+
3
+ require 'jstp'
4
+
5
+ module Testing
6
+ def self.test_log
7
+ @test_log ||= []
8
+ end
9
+ end
10
+
11
+ World Testing
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.add_dependency 'uuid'
16
16
 
17
17
  gem.add_development_dependency 'rspec'
18
+ gem.add_development_dependency 'cucumber'
18
19
 
19
20
  gem.files = `git ls-files`.split($\)
20
21
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -14,6 +14,9 @@ require 'jstp/api'
14
14
  require 'jstp/base'
15
15
  require 'jstp/connector'
16
16
 
17
+ require 'jstp/engine'
18
+ require 'jstp/controller'
19
+
17
20
  require 'writer/jstp/connector'
18
21
  require 'reader/jstp/connector'
19
22
 
@@ -0,0 +1,14 @@
1
+ module JSTP
2
+ class Controller
3
+ include JSTP::API
4
+
5
+ def initialize message
6
+ @protocol = message["protocol"]
7
+ @method = message["method"]
8
+ @referer = message["referer"]
9
+ @timestamp = message["timestamp"]
10
+ @token = message["token"]
11
+ @resource = message["resource"]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ module JSTP
2
+ class Engine
3
+ include Singleton
4
+
5
+ # Processes the dispatch in the new REST engine way
6
+ def dispatch message
7
+ host, the_class, query = discover_resource message["resource"].clone
8
+ resource = the_class.new message
9
+ if query.empty? && (message["body"].nil? || message["body"].empty?)
10
+ resource.send message["method"].downcase.to_sym
11
+ else
12
+ resource.send(
13
+ message["method"].downcase.to_sym,
14
+ {
15
+ "body" => message["body"],
16
+ "query" => query
17
+ }
18
+ )
19
+ end
20
+ end
21
+
22
+ def discover_resource resource_stack
23
+ response = []
24
+ response << resource_stack.shift
25
+ class_stack = "::"
26
+ query = []
27
+ resource_stack.each do |item|
28
+ begin
29
+ eval(class_stack + item)
30
+ class_stack += "#{item}::"
31
+ rescue NameError, SyntaxError
32
+ query << item
33
+ end
34
+ end
35
+ response << eval(class_stack[0..-3])
36
+ response << query
37
+ return response
38
+ end
39
+ end
40
+ end
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module JSTP
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
@@ -14,7 +14,7 @@ module JSTP
14
14
 
15
15
  server.onmessage { |message|
16
16
  message = JSON.parse message
17
- Connector.instance.block.call(message,
17
+ JSTP::Engine.instance.dispatch(message,
18
18
  JSTP::WebSocket.instance.sockets.key(server))
19
19
  }
20
20
  }
@@ -17,7 +17,8 @@ module Reader
17
17
  @server = TCPServer.open @source.port.inbound
18
18
  loop {
19
19
  Thread.start(@server.accept) { |client|
20
- @source.block.call JSON.parse client.gets
20
+ binding.pry
21
+ ::JSTP::Engine.instance.dispatch JSON.parse client.gets
21
22
  }
22
23
  }
23
24
  end
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.0
4
+ version: 0.5.0
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-01-09 00:00:00.000000000 Z
14
+ date: 2013-01-23 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: em-websocket
@@ -109,6 +109,22 @@ dependencies:
109
109
  - - ! '>='
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: cucumber
114
+ requirement: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '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: '0'
112
128
  description: Reference implementation for the sketch protocol JSTP
113
129
  email:
114
130
  - xavierviacanel@gmail.com
@@ -122,11 +138,16 @@ files:
122
138
  - LICENSE
123
139
  - README.md
124
140
  - Rakefile
141
+ - features/map_a_la_rest.feature
142
+ - features/step_definitions/map_a_la_rest_steps.rb
143
+ - features/support/env.rb
125
144
  - jstp.gemspec
126
145
  - lib/jstp.rb
127
146
  - lib/jstp/api.rb
128
147
  - lib/jstp/base.rb
129
148
  - lib/jstp/connector.rb
149
+ - lib/jstp/controller.rb
150
+ - lib/jstp/engine.rb
130
151
  - lib/jstp/tcp.rb
131
152
  - lib/jstp/version.rb
132
153
  - lib/jstp/web_socket.rb
@@ -165,6 +186,9 @@ signing_key:
165
186
  specification_version: 3
166
187
  summary: Reference implementation for the sketch protocol JSTP
167
188
  test_files:
189
+ - features/map_a_la_rest.feature
190
+ - features/step_definitions/map_a_la_rest_steps.rb
191
+ - features/support/env.rb
168
192
  - spec/jstp/api_spec.rb
169
193
  - spec/jstp/base_spec.rb
170
194
  - spec/jstp/connector_spec.rb