Skip to content

Commit c3e0404

Browse files
authored
Reduced need for JSONMessage. (#19)
1 parent 548a2fe commit c3e0404

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

lib/protocol/websocket/coder.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

lib/protocol/websocket/coder/json.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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

lib/protocol/websocket/json_message.rb

+4-13
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,21 @@
33
# Released under the MIT License.
44
# Copyright, 2022-2023, by Samuel Williams.
55

6-
require 'json'
7-
86
require_relative 'message'
97

8+
warn "Protocol::WebSocket::JSONMessage is deprecated. Use Protocol::WebSocket::TextMessage instead."
9+
1010
module Protocol
1111
module WebSocket
12+
# @deprecated Use {TextMessage} instead.
1213
class JSONMessage < TextMessage
1314
def self.wrap(message)
14-
if message.is_a?(TextMessage)
15-
self.new(message.buffer)
16-
end
15+
message
1716
end
1817

1918
def self.generate(object)
2019
self.new(JSON.generate(object))
2120
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
3021
end
3122
end
3223
end

lib/protocol/websocket/message.rb

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2022-2023, by Samuel Williams.
55

66
require_relative 'frame'
7+
require_relative 'coder'
78

89
module Protocol
910
module WebSocket
@@ -30,6 +31,21 @@ def to_str
3031
def encoding
3132
@buffer.encoding
3233
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
3349
end
3450

3551
class TextMessage < Message

test/protocol/websocket/message.rb

+17
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,21 @@
1212
it "can round-trip basic object" do
1313
expect(message.to_str).to be == buffer
1414
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
1532
end

0 commit comments

Comments
 (0)