jstp 0.5.7 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.md +26 -3
  2. data/jstp.gemspec +2 -1
  3. data/lib/jstp.rb +23 -11
  4. data/lib/jstp/configuration.rb +68 -0
  5. data/lib/jstp/controller.rb +49 -11
  6. data/lib/jstp/dispatch.rb +106 -0
  7. data/lib/jstp/engine.rb +77 -13
  8. data/lib/jstp/version.rb +1 -1
  9. data/lib/reader/jstp/dispatch.rb +36 -0
  10. data/lib/reader/jstp/engine.rb +66 -0
  11. data/lib/writer/jstp/dispatch.rb +95 -0
  12. data/samples/api-1.5/modular_style.rb +22 -0
  13. data/samples/api-1.5/references.rb +12 -0
  14. data/samples/api-1.5/sinatra_style.rb +18 -0
  15. data/samples/api-2.0/a_la_rack.rb +22 -0
  16. data/samples/api-2.0/clearer_sample.rb +12 -0
  17. data/samples/api-2.0/middleware.rb +23 -0
  18. data/samples/api-2.0/references.rb +11 -0
  19. data/samples/diff.rb +12 -0
  20. data/samples/hooks.rb +9 -0
  21. data/samples/micro.rb +6 -0
  22. data/samples/new_api.rb +62 -0
  23. data/samples/websocket.rb +11 -0
  24. data/spec/jstp/engine_spec.rb +39 -0
  25. data/spec/spec_helper.rb +0 -1
  26. metadata +40 -32
  27. data/features/map_a_la_rest.feature +0 -148
  28. data/features/step_definitions/map_a_la_rest_steps.rb +0 -19
  29. data/features/support/env.rb +0 -11
  30. data/lib/jstp/api.rb +0 -25
  31. data/lib/jstp/base.rb +0 -7
  32. data/lib/jstp/connector.rb +0 -17
  33. data/lib/jstp/tcp.rb +0 -12
  34. data/lib/jstp/web_socket.rb +0 -45
  35. data/lib/reader/jstp/connector.rb +0 -31
  36. data/lib/writer/jstp/connector.rb +0 -46
  37. data/spec/jstp/api_spec.rb +0 -79
  38. data/spec/jstp/base_spec.rb +0 -5
  39. data/spec/jstp/connector_spec.rb +0 -16
  40. data/spec/jstp/tcp_spec.rb +0 -7
  41. data/spec/jstp/web_socket_spec.rb +0 -78
  42. data/spec/reader/jstp/connector_spec.rb +0 -32
  43. data/spec/writer/jstp/connector_spec.rb +0 -71
@@ -1,31 +0,0 @@
1
- module Reader
2
- module JSTP
3
- class Connector
4
- attr_accessor :source
5
-
6
- def initialize the_source
7
- @source = the_source
8
- end
9
-
10
- # Start the server with the websocket strategy
11
- def websocket
12
- ::EventMachine.run &::JSTP::WebSocket.instance.event_machine
13
- end
14
-
15
- # Start the server with the TCP strategy
16
- def tcp
17
- @server = TCPServer.open @source.port.inbound
18
- loop {
19
- Thread.start(@server.accept) { |client|
20
- begin
21
- ::JSTP::Engine.instance.dispatch JSON.parse client.gets
22
- rescue Exception => e
23
- puts "\e[31m#{e.message}\e[0m"
24
- puts e.backtrace
25
- end
26
- }
27
- }
28
- end
29
- end
30
- end
31
- end
@@ -1,46 +0,0 @@
1
- module Writer
2
- module JSTP
3
- class Connector
4
- attr_accessor :source
5
-
6
- def initialize source
7
- @source = source
8
- end
9
-
10
- # Dispatch this applying the websocket strategy
11
- def websocket message
12
- begin
13
- this_client = ::JSTP::WebSocket.instance
14
- .client message["resource"]
15
- json = message.to_json
16
- this_client.callback do
17
- this_client.send_msg json
18
- this_client.close_connection_after_writing
19
- end
20
- rescue RuntimeError => e
21
- EM.run {
22
- this_client = ::JSTP::WebSocket.instance
23
- .client message["resource"]
24
- json = message.to_json
25
-
26
- this_client.callback do
27
- this_client.send_msg json
28
- this_client.close_connection_after_writing
29
- end
30
-
31
- this_client.disconnect do
32
- EM::stop_event_loop
33
- end
34
- }
35
- end
36
- end
37
-
38
- # Dispatch thid applying the TCP strategy
39
- def tcp message
40
- client = TCPSocket.open message["resource"].first, @source.port.outbound
41
- client.puts message.to_json
42
- client.close
43
- end
44
- end
45
- end
46
- end
@@ -1,79 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
-
4
- describe JSTP::API do
5
- describe '#strategy' do
6
- it 'should set the strategy' do
7
- o = Object.new
8
- o.extend JSTP::API
9
-
10
- o.strategy inbound: :websocket, outbound: :tcp
11
-
12
- JSTP::Connector.instance.strategy.inbound.should == :websocket
13
- JSTP::Connector.instance.strategy.outbound.should == :tcp
14
- end
15
- end
16
-
17
- describe '#dispatch' do
18
- before do
19
- JSTP::Connector.instance.strategy = SymbolMatrix inbound: :tcp, outbound: :websocket
20
- end
21
-
22
- context 'a block is passed' do
23
- it 'should register the block and send JSTP::Server to the EventMachine reactor' do
24
- block = proc { "lalal" }
25
- reader = stub 'reader'
26
-
27
- JSTP::Connector.instance.should_receive(:block=)
28
- .with block
29
-
30
- JSTP::Connector.instance.should_receive(:from)
31
- .and_return reader
32
- reader.should_receive :tcp
33
-
34
- o = Object.new
35
- o.extend JSTP::API
36
- o.dispatch &block
37
- end
38
- end
39
-
40
- context 'an argument is passed' do
41
- it 'should dispatch the message via the Connector' do
42
- message = stub 'message'
43
- writer = stub 'writer'
44
-
45
- JSTP::Connector.instance.should_receive(:to)
46
- .and_return writer
47
-
48
- writer.should_receive(:websocket)
49
- .with message
50
-
51
- o = Object.new
52
- o.extend JSTP::API
53
- o.dispatch message
54
- end
55
- end
56
-
57
- context 'no argument, no block' do
58
- it 'should return the JSTP::Connector.instance' do
59
- o = Object.new
60
- o.extend JSTP::API
61
- o.dispatch.should == JSTP::Connector.instance
62
- end
63
- end
64
- end
65
-
66
- describe '#port' do
67
- it 'should configure the port number' do
68
- message = {
69
- "resource" => ["localhost"]
70
- }
71
- o = Object.new
72
- o.extend JSTP::API
73
- o.port inbound: 3000, outbound: 4000
74
-
75
- JSTP::Connector.instance.port.inbound.should == 3000
76
- JSTP::Connector.instance.port.outbound.should == 4000
77
- end
78
- end
79
- end
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe JSTP::Base do
4
-
5
- end
@@ -1,16 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
-
4
- describe JSTP::Connector do
5
- it 'should be a singleton' do
6
- JSTP::Connector.ancestors.should include Singleton
7
- end
8
-
9
- it 'should include a discoverer for Writer' do
10
- JSTP::Connector.ancestors.should include Discoverer::Writer
11
- end
12
-
13
- it 'should include a discoverer for Reader' do
14
- JSTP::Connector.ancestors.should include Discoverer::Reader
15
- end
16
- end
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe JSTP::TCP do
4
- it 'should be a singleton' do
5
- JSTP::TCP.ancestors.should include Singleton
6
- end
7
- end
@@ -1,78 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe JSTP::WebSocket do
4
- it 'should be a singleton' do
5
- JSTP::WebSocket.ancestors.should include Singleton
6
- end
7
-
8
- describe '#event_machine' do
9
- before do
10
- JSTP::Connector.instance.port = SymbolMatrix inbound: 33333, outbound: 44444
11
- end
12
-
13
- it 'should start a websocket server in 33333' do
14
- JSTP::WebSocket.instance.server_setup
15
- .should_receive :call
16
-
17
- EventMachine::WebSocket.should_receive(:start)
18
- .with({ host: '0.0.0.0', port: JSTP::Connector.instance.port.inbound },
19
- &JSTP::WebSocket.instance.server_setup)
20
-
21
- JSTP::WebSocket.instance.event_machine.call
22
- end
23
- end
24
-
25
- describe '#server_setup' do
26
- it 'should set #event_on_message to onmessage' do
27
- pending 'Outdated example'
28
-
29
- server = stub 'server'
30
- JSTP::WebSocket.instance.event_on_message.should_receive :call
31
-
32
- server.should_receive(:onmessage)
33
- .with &JSTP::WebSocket.instance.event_on_message
34
-
35
- JSTP::WebSocket.instance.server_setup.call server
36
- end
37
- end
38
-
39
- describe '#event_on_message' do
40
- it 'should call the registered block with the parsed message' do
41
- pending 'Outdated example'
42
-
43
- @the_proc = stub 'the proc'
44
- @message = stub 'message'
45
- @parsed_message = stub 'parsed message'
46
-
47
- JSON.should_receive(:parse).with(@message)
48
- .and_return @parsed_message
49
-
50
- JSTP::Connector.instance.should_receive(:block)
51
- .and_return @the_proc
52
-
53
- @the_proc.should_receive(:call)
54
- .with @parsed_message
55
-
56
- JSTP::WebSocket.instance.event_on_message.call @message
57
- end
58
- end
59
-
60
- describe '#client' do
61
- context 'there is no connection for that address' do
62
- before do
63
- @web_socket_client = stub 'web socket client'
64
- @resource = ["localhost", "hola", "quetal"]
65
- end
66
-
67
- it 'should initialize the websocket client' do
68
- EventMachine::WebSocketClient.should_receive(:connect)
69
- .with("ws://#{@resource.first}:#{JSTP::Connector.instance.port.outbound}/#{@resource[1..-1].join('/')}")
70
- .and_return @web_socket_client
71
-
72
- JSTP::WebSocket.instance
73
- .client(@resource)
74
- .should == @web_socket_client
75
- end
76
- end
77
- end
78
- end
@@ -1,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Reader::JSTP::Connector do
4
- describe '#websocket' do
5
- it 'should run EventMachine with the JSTP::EventMachine proc' do
6
- JSTP::WebSocket.instance.event_machine.should_receive :call
7
-
8
- EventMachine.should_receive(:run)
9
- .with &JSTP::WebSocket.instance.event_machine
10
-
11
- reader = Reader::JSTP::Connector.new stub 'connector'
12
- reader.websocket
13
- end
14
- end
15
-
16
- describe '#tcp' do
17
- it 'should create the server and start it' do
18
- pending 'I still can test infinite loops'
19
-
20
- tcp_server = stub 'tcp server'
21
-
22
- TCPServer.should_receive(:open)
23
- .with(JSTP::Connector.instance.port.inbound)
24
- .and_return tcp_server
25
-
26
- tcp_server.should_receive(:accept)
27
-
28
- reader = Reader::JSTP::Connector.new stub 'connector'
29
- reader.tcp
30
- end
31
- end
32
- end
@@ -1,71 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Writer::JSTP::Connector do
4
- describe '#websocket' do
5
- before do
6
- @connector = stub 'connector'
7
- @message = { "resource" => ["lala"] }
8
- @message_json = stub 'message_json'
9
- @web_socket_client = stub 'web socket client'
10
- end
11
-
12
- it 'should get the client from the pool and set the callback' do
13
- @writer = Writer::JSTP::Connector.new @connector
14
-
15
- JSTP::WebSocket.instance.should_receive(:client)
16
- .with(@message["resource"])
17
- .and_return @web_socket_client
18
-
19
- @web_socket_client.should_receive(:callback)
20
-
21
- @message.should_receive(:to_json).and_return @message_json
22
-
23
- @writer.websocket @message
24
- end
25
-
26
- it 'should send the message from the callback' do
27
- pending "I'm unable to test this properly since providing a Proc will make me lose scope for the client"
28
- @writer = Writer::JSTP::Connector.new @connector
29
-
30
- JSTP::WebSocket.should_receive(:client)
31
- .with(@message["resource"])
32
- .and_return @web_socket_client
33
-
34
- @web_socket_client.should_receive(:callback)
35
-
36
- @message.should_receive(:to_json).and_return @message_json
37
- @web_socket_client.should_receive(:send_msg)
38
- .with(@message_json)
39
-
40
- @writer.websocket @message
41
- end
42
- end
43
-
44
- describe '#tcp' do
45
- it 'should get the resource and use it as host, then send the dispatch in JSON' do
46
- message = stub 'message'
47
- message_json = stub 'message json'
48
- resource = stub 'resource'
49
- hostname = stub 'hostname'
50
- socket = stub 'socket'
51
-
52
- message.should_receive(:[]).with("resource")
53
- .and_return resource
54
-
55
- resource.should_receive(:first).and_return hostname
56
-
57
- JSTP::Connector.instance.port = SymbolMatrix inbound: 4444, outbound: 3333
58
-
59
- TCPSocket.should_receive(:open).with(hostname, JSTP::Connector.instance.port.outbound)
60
- .and_return socket
61
-
62
- message.should_receive(:to_json).and_return message_json
63
-
64
- socket.should_receive(:puts).with message_json
65
- socket.should_receive(:close)
66
-
67
- writer = Writer::JSTP::Connector.new ::JSTP::Connector.instance
68
- writer.tcp message
69
- end
70
- end
71
- end