Module: Twirp::Encoding
- Defined in:
- lib/twirp/encoding.rb
Constant Summary collapse
- JSON =
"application/json"- PROTO =
"application/protobuf"
Class Method Summary collapse
- .decode(bytes, msg_class, content_type) ⇒ Object
- .decode_json(bytes) ⇒ Object
- .encode(msg_obj, msg_class, content_type) ⇒ Object
- .encode_json(attrs) ⇒ Object
- .valid_content_type?(content_type) ⇒ Boolean
- .valid_content_types ⇒ Object
Class Method Details
.decode(bytes, msg_class, content_type) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/twirp/encoding.rb', line 11 def decode(bytes, msg_class, content_type) case content_type when JSON then msg_class.decode_json(bytes) when PROTO then msg_class.decode(bytes) else raise ArgumentError.new("Invalid content_type") end end |
.decode_json(bytes) ⇒ Object
31 32 33 |
# File 'lib/twirp/encoding.rb', line 31 def decode_json(bytes) ::JSON.parse(bytes) end |
.encode(msg_obj, msg_class, content_type) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/twirp/encoding.rb', line 19 def encode(msg_obj, msg_class, content_type) case content_type when JSON then msg_class.encode_json(msg_obj) when PROTO then msg_class.encode(msg_obj) else raise ArgumentError.new("Invalid content_type") end end |
.encode_json(attrs) ⇒ Object
27 28 29 |
# File 'lib/twirp/encoding.rb', line 27 def encode_json(attrs) ::JSON.generate(attrs) end |
.valid_content_type?(content_type) ⇒ Boolean
35 36 37 |
# File 'lib/twirp/encoding.rb', line 35 def valid_content_type?(content_type) content_type == JSON || content_type == PROTO end |
.valid_content_types ⇒ Object
39 40 41 |
# File 'lib/twirp/encoding.rb', line 39 def valid_content_types [JSON, PROTO] end |