Module: Twirbet::Encoding
- Extended by:
- T::Sig
- Defined in:
- lib/twirbet/encoding.rb
Class Method Summary collapse
- .decode(object, klass, content_type) ⇒ Object
- .decode_request(request) ⇒ Object
- .encode(object, content_type) ⇒ Object
- .encode_request(request, klass) ⇒ Object
- .supported?(content_type) ⇒ Boolean
Class Method Details
.decode(object, klass, content_type) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/twirbet/encoding.rb', line 40 def decode(object, klass, content_type) case content_type when "application/protobuf" T.unsafe(klass).decode(object) when "application/json" T.unsafe(klass).decode_json(object) else raise "Unsupported content type: #{content_type}" end end |
.decode_request(request) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/twirbet/encoding.rb', line 20 def decode_request(request) method = request.env["twirp.method"] raise "Missing `twirp.method` in request environment." if method.nil? decode(request.body.read, method.request, request.content_type) end |
.encode(object, content_type) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/twirbet/encoding.rb', line 28 def encode(object, content_type) case content_type when "application/protobuf" T.unsafe(object).to_proto when "application/json" T.unsafe(object).to_json else raise "Unsupported content type: #{content_type}" end end |
.encode_request(request, klass) ⇒ Object
15 16 17 |
# File 'lib/twirbet/encoding.rb', line 15 def encode_request(request, klass) T.unsafe(klass).encode(request) end |
.supported?(content_type) ⇒ Boolean
10 11 12 |
# File 'lib/twirbet/encoding.rb', line 10 def supported?(content_type) content_type == "application/protobuf" || content_type == "application/json" end |