Sha256: dffa39fa6a7a87210da5a7cbe18e20433ba2227881b42e87c1dbc0c43c0c94d2

Contents?: true

Size: 1013 Bytes

Versions: 1

Compression:

Stored size: 1013 Bytes

Contents

require_relative 'response_builder'
require_relative 'cacheclient_pb'
require_relative 'get_response'

module Momento
  # @private
  class GetResponseBuilder < ResponseBuilder
    # Build a Momento::GetResponse from a block of code
    # which returns a Momento::ControlClient::GetResponse.
    #
    # @return [Momento::GetResponse]
    # @raise [StandardError] when the exception is not recognized.
    # @raise [TypeError] when the response is not recognized.
    def from_block
      response = yield
    rescue *RESCUED_EXCEPTIONS => e
      GetResponse::Error.new(exception: e, context: context)
    else
      from_response(response)
    end

    private

    def from_response(response)
      raise TypeError unless response.is_a?(Momento::CacheClient::GetResponse)

      case response.result
      when :Hit
        GetResponse::Hit.new(grpc_response: response)
      when :Miss
        GetResponse::Miss.new
      else
        raise "Unknown get result: #{response.result}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
momento-0.2.0 lib/momento/get_response_builder.rb