Class: SpannerLib::MessageHandler
- Inherits:
-
Object
- Object
- SpannerLib::MessageHandler
- Defined in:
- lib/spannerlib/message_handler.rb
Instance Method Summary collapse
-
#data(proto_klass: nil) ⇒ Object
Returns the data payload from the message.
-
#initialize(message) ⇒ MessageHandler
constructor
A new instance of MessageHandler.
- #object_id ⇒ Object
- #throw_if_error! ⇒ Object
Constructor Details
#initialize(message) ⇒ MessageHandler
Returns a new instance of MessageHandler.
23 24 25 |
# File 'lib/spannerlib/message_handler.rb', line 23 def initialize() @message = end |
Instance Method Details
#data(proto_klass: nil) ⇒ Object
Returns the data payload from the message. If a proto_klass is provided, it decodes the bytes into a Protobuf object. Otherwise, it returns the raw bytes as a string.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/spannerlib/message_handler.rb', line 35 def data(proto_klass: nil) throw_if_error! len = @message[:length] ptr = @message[:pointer] return (proto_klass ? proto_klass.new : "") unless len.positive? && !ptr.null? bytes = ptr.read_string(len) proto_klass ? proto_klass.decode(bytes) : bytes end |
#object_id ⇒ Object
27 28 29 30 |
# File 'lib/spannerlib/message_handler.rb', line 27 def object_id throw_if_error! @message[:objectId] end |
#throw_if_error! ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/spannerlib/message_handler.rb', line 48 def throw_if_error! code = @message[:code] return if code.zero? error_msg = SpannerLib.(@message) raise SpannerLibException, "Call failed with code #{code}: #{error_msg}" end |