ddp-server 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 950aa0c33840f83ddc2708dba9b8be77b6bbed71
4
- data.tar.gz: 602b119c4b1c339c3f5395bc2ce23aa69d152d83
3
+ metadata.gz: 2d646729580513ffa2a77c522b58986656316e38
4
+ data.tar.gz: f0852db69edb241a9c691728f736d016cb7fcbeb
5
5
  SHA512:
6
- metadata.gz: 7e2a9fb98f64ed089af30ee95f06bda60d29c61d69955ef62e90b78819f6e88033c2b413fd1bf2c54a9bb39671ed77aca511a475d5ff095131a61964da132f72
7
- data.tar.gz: 15cebf56397143b80badf733327f2c6a17850ed8789c1f667d8d0d53d15a01a9dd92f6c505c942f093042f03bc8951207db61c6978fd655cba118ad8f6d99695
6
+ metadata.gz: 0731bfe5e51e1871840ea5cccd484a217af6b9c3a7bec5bdf73634cffe1c954b1b8a95adcf3fd6577eb3d2dd507c7d82d93f2e5aabf4f7b7b84308ec91a93304
7
+ data.tar.gz: a2d49e26c08c6bcf97f57c8d3f7f331dd107b9b6f35888a111ff9222d06df46e5a9ca190f4342588b2f01cc4769b9b2373fbfb958d4d4e3edd980395b6ec77ef
data/ddp-server.gemspec CHANGED
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
22
  spec.add_development_dependency 'rake'
23
23
 
24
- spec.add_dependency 'celluloid-websocket', '~> 0.0.7'
24
+ spec.add_dependency 'celluloid-websocket', '~> 0.0.10'
25
25
  end
@@ -1,3 +1,22 @@
1
1
  require 'ddp/server'
2
2
 
3
- run DDP::Server::WebSocket.rack
3
+ class APIClass
4
+ def invoke_rpc(method, *args)
5
+ case method
6
+ when 'hello_world' then 'Hello world!'
7
+ else
8
+ raise 'Don\'t know that method'
9
+ end
10
+ end
11
+
12
+ def collection_query(name, *args)
13
+ lambda do |&on_update|
14
+ 5.times do |i|
15
+ sleep 5
16
+ on_update.({}, id: 1, message: "Message #{name}: #{i}")
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ run DDP::Server::WebSocket.rack(APIClass)
@@ -1,3 +1,5 @@
1
+ require 'celluloid'
2
+
1
3
  module DDP
2
4
  module Server
3
5
  module Protocol
@@ -16,12 +18,31 @@ module DDP
16
18
  end
17
19
  end
18
20
 
19
- def handle_sub(_id, _name, _params)
20
- raise 'Must be overridden'
21
+ def handle_sub(id, name, params)
22
+ params ||= []
23
+ query = api.collection_query(name, *params)
24
+ subscription = subscriptions[id] = Subscription.new(self, id, name, query)
25
+ subscription.async.start
26
+ send_ready([id])
27
+ rescue => e
28
+ send_error_result(id, e)
21
29
  end
22
30
 
23
- def handle_unsub(_id)
24
- raise 'Must be overridden'
31
+ def subscription_update(id, old_value, new_value)
32
+ subscription_name = @subscriptions[id].name
33
+ new_value_id = new_value['id']
34
+ old_value_id = old_value['id']
35
+
36
+ return send_added(subscription_name, new_value_id, new_value) if old_value.nil?
37
+ return send_removed(subscription_name, old_value_id) if new_value.nil?
38
+
39
+ send_changed(subscription_name, old_value_id, new_value, old_value.keys - new_value.keys)
40
+ end
41
+
42
+ def handle_unsub(id)
43
+ subscription = @subscriptions.delete(id)
44
+ subscription.stop unless subscription.nil?
45
+ send_nosub(id)
25
46
  end
26
47
 
27
48
  def send_nosub(id, error = nil)
@@ -60,6 +81,33 @@ module DDP
60
81
  def send_moved_before(collection, id, before = nil)
61
82
  write_message msg: 'movedBefore', id: id, collection: collection, before: before
62
83
  end
84
+
85
+ # Actor that asynchronously monitors a collection
86
+ class Subscription
87
+ include Celluloid
88
+
89
+ attr_reader :name, :stopped, :listener, :query, :id
90
+ alias_method :stopped?, :stopped
91
+
92
+ def initialize(listener, id, name, query)
93
+ @stopped = false
94
+ @name = name
95
+ @listener = listener
96
+ @id = id
97
+ @query = query
98
+ end
99
+
100
+ def start
101
+ query.call do |old_value, new_value|
102
+ listener.subscription_update(id, old_value, new_value)
103
+ break if stopped?
104
+ end
105
+ end
106
+
107
+ def stop
108
+ @stopped = true
109
+ end
110
+ end
63
111
  end
64
112
  end
65
113
  end
@@ -15,8 +15,12 @@ module DDP
15
15
  end
16
16
  end
17
17
 
18
- def handle_method(_id, _method, _params)
19
- raise 'Must be overridden'
18
+ def handle_method(id, method, params)
19
+ params ||= []
20
+ result = api.invoke_rpc(method, *params)
21
+ send_result(id, result)
22
+ rescue => e
23
+ send_error_result(id, e)
20
24
  end
21
25
 
22
26
  def send_result(id, result = NO_RESULT)
@@ -1,6 +1,6 @@
1
1
  module DDP
2
2
  # We use semantic versioning
3
3
  module Server
4
- VERSION = '0.0.5'
4
+ VERSION = '0.1.0'
5
5
  end
6
6
  end
data/lib/ddp/server.rb CHANGED
@@ -10,6 +10,13 @@ module DDP
10
10
  class WebSocket < Celluloid::WebSocket
11
11
  include DDP::Server::Protocol
12
12
 
13
+ attr_accessor :api, :subscriptions
14
+
15
+ def initialize(api_class, *args)
16
+ @api = api_class.new(*args)
17
+ @subscriptions = {}
18
+ end
19
+
13
20
  def on_open
14
21
  handle_connect
15
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddp-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tinco Andringa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-04 00:00:00.000000000 Z
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.0.7
47
+ version: 0.0.10
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.0.7
54
+ version: 0.0.10
55
55
  description: DDP Protocol server for implementing Ruby DDP backends
56
56
  email:
57
57
  - mail@tinco.nl