Sha256: 9707afb1578d1e5e7e223e56f394cf776b61700203e3b5a456de95659d04202c

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require_relative 'response/error'

module Momento
  # A response from listing the caches.
  #
  # Each response is a single page of caches, there may be additional pages.
  # Use Momento::SimpleCacheClient#caches to efficiently get the whole list.
  class ListCachesResponse < Response
    # Did it get a page of caches?
    # @return [Boolean]
    def success?
      false
    end

    # The names of the caches in this page.
    # @return [Array,nil]
    def cache_names
      nil
    end

    # A token to fetch the next page.
    # The last page will have a blank token.
    # @return [String,nil]
    def next_token
      nil
    end

    # @!method to_s
    #   Displays the response and the list of cache names.
    #   The list of cache names will be truncated.
    #   @return [String]

    # @private
    class Success < ListCachesResponse
      CACHE_NAMES_TO_DISPLAY = 5

      # rubocop:disable Lint/MissingSuper
      def initialize(grpc_response:)
        @grpc_response = grpc_response
      end
      # rubocop:enable Lint/MissingSuper

      def success?
        true
      end

      def cache_names
        @grpc_response.cache.map(&:cache_name)
      end

      def next_token
        @grpc_response.next_token
      end

      def to_s
        "#{super}: #{display_cache_names}"
      end

      private

      def display_cache_names
        display = cache_names.first(CACHE_NAMES_TO_DISPLAY).join(", ")

        if cache_names.size > CACHE_NAMES_TO_DISPLAY
          "#{display}, ..."
        else
          display
        end
      end
    end

    # @private
    class Error < ListCachesResponse
      include ::Momento::Response::Error
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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