File tree 5 files changed +69
-13
lines changed
5 files changed +69
-13
lines changed Original file line number Diff line number Diff line change
1
+ require_relative 'coder/json'
2
+
3
+ module Protocol
4
+ module WebSocket
5
+ module Coder
6
+ DEFAULT = JSON ::DEFAULT
7
+ end
8
+ end
9
+ end
Original file line number Diff line number Diff line change
1
+ require 'json'
2
+
3
+ module Protocol
4
+ module WebSocket
5
+ module Coder
6
+ class JSON
7
+ def initialize ( **options )
8
+ @options = options
9
+ end
10
+
11
+ def parse ( buffer )
12
+ ::JSON . parse ( buffer , **@options )
13
+ end
14
+
15
+ def generate ( object )
16
+ ::JSON . generate ( object , **@options )
17
+ end
18
+
19
+ DEFAULT = new ( symbolize_names : true )
20
+ end
21
+ end
22
+ end
23
+ end
Original file line number Diff line number Diff line change 3
3
# Released under the MIT License.
4
4
# Copyright, 2022-2023, by Samuel Williams.
5
5
6
- require 'json'
7
-
8
6
require_relative 'message'
9
7
8
+ warn "Protocol::WebSocket::JSONMessage is deprecated. Use Protocol::WebSocket::TextMessage instead."
9
+
10
10
module Protocol
11
11
module WebSocket
12
+ # @deprecated Use {TextMessage} instead.
12
13
class JSONMessage < TextMessage
13
14
def self . wrap ( message )
14
- if message . is_a? ( TextMessage )
15
- self . new ( message . buffer )
16
- end
15
+ message
17
16
end
18
17
19
18
def self . generate ( object )
20
19
self . new ( JSON . generate ( object ) )
21
20
end
22
-
23
- def parse ( symbolize_names : true , **options )
24
- JSON . parse ( @buffer , symbolize_names : symbolize_names , **options )
25
- end
26
-
27
- def to_h
28
- parse . to_h
29
- end
30
21
end
31
22
end
32
23
end
Original file line number Diff line number Diff line change 4
4
# Copyright, 2022-2023, by Samuel Williams.
5
5
6
6
require_relative 'frame'
7
+ require_relative 'coder'
7
8
8
9
module Protocol
9
10
module WebSocket
@@ -30,6 +31,21 @@ def to_str
30
31
def encoding
31
32
@buffer . encoding
32
33
end
34
+
35
+ # Generate a message from a value using the given coder.
36
+ # @property value [Object] The value to encode.
37
+ # @property coder [Coder] The coder to use. Defaults to JSON.
38
+ def self . generate ( value , coder = Coder ::DEFAULT )
39
+ new ( coder . generate ( value ) )
40
+ end
41
+
42
+ def parse ( coder = Coder ::DEFAULT )
43
+ coder . parse ( @buffer )
44
+ end
45
+
46
+ def to_h ( ...)
47
+ parse ( ...) . to_h
48
+ end
33
49
end
34
50
35
51
class TextMessage < Message
Original file line number Diff line number Diff line change 12
12
it "can round-trip basic object" do
13
13
expect ( message . to_str ) . to be == buffer
14
14
end
15
+
16
+ with "a JSON encoded message" do
17
+ let ( :value ) { { hello : "world" } }
18
+ let ( :message ) { subject . generate ( value ) }
19
+
20
+ with "#parse" do
21
+ it "can parse JSON" do
22
+ expect ( message . parse ) . to be == { hello : "world" }
23
+ end
24
+ end
25
+
26
+ with "#to_h" do
27
+ it "can convert to hash" do
28
+ expect ( message . to_h ) . to be == { hello : "world" }
29
+ end
30
+ end
31
+ end
15
32
end
You can’t perform that action at this time.
0 commit comments