anycable 0.4.0 → 0.4.1

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: 2589605d172fda191b1733259d1205e474754285
4
- data.tar.gz: 444266a714ada061f08f699ffafd61f08edd6e59
3
+ metadata.gz: 26a8ddbb776d6aa172c87b764baaf5a50e99c4b8
4
+ data.tar.gz: '0139cc0a3fd8e545ec262262ec37451bb0e5403c'
5
5
  SHA512:
6
- metadata.gz: cdcdca3db177557e4d144d0af7b8d8a643ad2dfe1c3728e7be95aaa84494e21f38e7bda57d61c3f617f62b1e7302be2c19a0b07b4e5795fb213b898bd8279280
7
- data.tar.gz: 669cdc0c8b3d83114a1a0f5acf2e7a1686e70ba2a739a1acdcbdc01cdcbc5122e56eab907f26aefe49cf227a631a047f17a8d817c75acfa429d6654f9b9e00c8
6
+ metadata.gz: 55c331b5be0551543aeb12ad5332155b2301d72858b31dc03f3e87698538232f6427a69ca5e1c38c7ca1e054447da7526e648c27149b30d7ee81db130a6dc497
7
+ data.tar.gz: ef3dc2181c6f8fe1b4e73fcca29e177daf3fc16d00f22b6107be423410b1e0b574e307f5f96d530d11c46e0f3a286a6897b86dc08b24c2881e0df3c6b69baf2b
data/.rubocop.yml CHANGED
@@ -46,6 +46,10 @@ Metrics/LineLength:
46
46
  Exclude:
47
47
  - 'spec/**/*.rb'
48
48
 
49
+ Metrics/BlockLength:
50
+ Exclude:
51
+ - 'spec/**/*.rb'
52
+
49
53
  Rails/Date:
50
54
  Enabled: false
51
55
 
data/CHANGELOG.md CHANGED
@@ -1,6 +1,20 @@
1
1
  # Change log
2
2
 
3
- ## 0.4.0
3
+ ## 0.4.1 (2017-01-24)
4
+
5
+ - Introduce _fake_ socket instance to handle transmissions and streams. ([@palkan][])
6
+
7
+ - Make commands handling more abstract. ([@palkan][])
8
+
9
+ We now do not explicitly call channels action but use the only one entrypoing for all commands:
10
+
11
+ ```ruby
12
+ connection.handle_channel_command(identifier, command, data)
13
+ ```
14
+
15
+ This method should return `true` if command was successful and `false` otherwise.
16
+
17
+ ## 0.4.0 (2017-01-22)
4
18
 
5
19
  - Refactor RPC API. ([@palkan][])
6
20
 
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
+ require 'anycable/socket'
2
3
  require 'anycable/rpc/rpc'
3
4
  require 'anycable/rpc/rpc_services'
4
5
 
5
- # rubocop:disable Metrics/ClassLength
6
+ # rubocop:disable Metrics/AbcSize
6
7
  # rubocop:disable Metrics/MethodLength
7
8
  module Anycable
8
9
  # RPC service handler
@@ -11,17 +12,19 @@ module Anycable
11
12
  def connect(request, _unused_call)
12
13
  logger.debug("RPC Connect: #{request}")
13
14
 
14
- connection = factory.create(env: rack_env(request))
15
+ socket = build_socket(env: rack_env(request))
16
+
17
+ connection = factory.create(socket)
15
18
 
16
19
  connection.handle_open
17
20
 
18
- if connection.closed?
21
+ if socket.closed?
19
22
  Anycable::ConnectionResponse.new(status: Anycable::Status::ERROR)
20
23
  else
21
24
  Anycable::ConnectionResponse.new(
22
25
  status: Anycable::Status::SUCCESS,
23
26
  identifiers: connection.identifiers_json,
24
- transmissions: connection.transmissions
27
+ transmissions: socket.transmissions
25
28
  )
26
29
  end
27
30
  end
@@ -29,8 +32,10 @@ module Anycable
29
32
  def disconnect(request, _unused_call)
30
33
  logger.debug("RPC Disonnect: #{request}")
31
34
 
35
+ socket = build_socket(env: rack_env(request))
36
+
32
37
  connection = factory.create(
33
- env: rack_env(request),
38
+ socket,
34
39
  identifiers: request.identifiers,
35
40
  subscriptions: request.subscriptions
36
41
  )
@@ -45,68 +50,30 @@ module Anycable
45
50
  def command(message, _unused_call)
46
51
  logger.debug("RPC Command: #{message}")
47
52
 
48
- run_command(message)
49
- end
53
+ socket = build_socket
50
54
 
51
- private
52
-
53
- def run_command(message)
54
55
  connection = factory.create(
56
+ socket,
55
57
  identifiers: message.connection_identifiers
56
58
  )
57
59
 
58
- channel = connection.channel_for(message.identifier)
59
-
60
- if !channel.nil?
61
- send("handle_#{message.command}", message, connection, channel)
62
- else
63
- Anycable::CommandResponse.new(
64
- status: Anycable::Status::ERROR
65
- )
66
- end
67
- end
68
-
69
- def handle_subscribe(_msg, connection, channel)
70
- channel.handle_subscribe
71
- if channel.subscription_rejected?
72
- Anycable::CommandResponse.new(
73
- status: Anycable::Status::ERROR,
74
- disconnect: connection.closed?,
75
- transmissions: connection.transmissions
76
- )
77
- else
78
- Anycable::CommandResponse.new(
79
- status: Anycable::Status::SUCCESS,
80
- disconnect: connection.closed?,
81
- stop_streams: channel.stop_streams?,
82
- streams: channel.streams,
83
- transmissions: connection.transmissions
84
- )
85
- end
86
- end
87
-
88
- def handle_unsubscribe(_mgs, connection, channel)
89
- channel.handle_unsubscribe
90
-
91
- Anycable::CommandResponse.new(
92
- status: Anycable::Status::SUCCESS,
93
- disconnect: connection.closed?,
94
- stop_streams: true,
95
- transmissions: connection.transmissions
60
+ result = connection.handle_channel_command(
61
+ message.identifier,
62
+ message.command,
63
+ message.data
96
64
  )
97
- end
98
65
 
99
- def handle_message(msg, connection, channel)
100
- channel.handle_action(msg.data)
101
66
  Anycable::CommandResponse.new(
102
- status: Anycable::Status::SUCCESS,
103
- disconnect: connection.closed?,
104
- stop_streams: channel.stop_streams?,
105
- streams: channel.streams,
106
- transmissions: connection.transmissions
67
+ status: result ? Anycable::Status::SUCCESS : Anycable::Status::ERROR,
68
+ disconnect: socket.closed?,
69
+ stop_streams: socket.stop_streams?,
70
+ streams: socket.streams,
71
+ transmissions: socket.transmissions
107
72
  )
108
73
  end
109
74
 
75
+ private
76
+
110
77
  # Build env from path
111
78
  def rack_env(request)
112
79
  uri = URI.parse(request.path)
@@ -124,6 +91,10 @@ module Anycable
124
91
  }
125
92
  end
126
93
 
94
+ def build_socket(**options)
95
+ Anycable::Socket.new(**options)
96
+ end
97
+
127
98
  def logger
128
99
  Anycable.logger
129
100
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ module Anycable
3
+ # Socket mock to be used with application connection
4
+ class Socket
5
+ attr_reader :transmissions, :env
6
+
7
+ def initialize(env: nil)
8
+ @transmissions = []
9
+ @env = env
10
+ end
11
+
12
+ def transmit(websocket_message)
13
+ transmissions << websocket_message
14
+ end
15
+
16
+ def stream(broadcasting)
17
+ streams << broadcasting
18
+ end
19
+
20
+ def streams
21
+ @streams ||= []
22
+ end
23
+
24
+ def close
25
+ @closed = true
26
+ @transmissions.clear
27
+ @streams&.clear
28
+ @stop_all_streams = true
29
+ end
30
+
31
+ def closed?
32
+ @closed == true
33
+ end
34
+
35
+ def stop_all_streams
36
+ @stop_all_streams = true
37
+ end
38
+
39
+ def stop_streams?
40
+ @stop_all_streams == true
41
+ end
42
+ end
43
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Anycable
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anycable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-22 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anyway_config
@@ -175,6 +175,7 @@ files:
175
175
  - lib/anycable/rpc/rpc_services.rb
176
176
  - lib/anycable/rpc_handler.rb
177
177
  - lib/anycable/server.rb
178
+ - lib/anycable/socket.rb
178
179
  - lib/anycable/version.rb
179
180
  - protos/rpc.proto
180
181
  homepage: http://github.com/anycable/anycable