Class: Rubyists::Dapr::Client::State

Inherits:
Object
  • Object
show all
Includes:
Rubyists::Dapr::Client, SemanticLogger::Loggable
Defined in:
lib/dapr/client/state.rb

Overview

The State class is a client for the Dapr State Management Building block Info here: docs.dapr.io/developing-applications/building-blocks/state-management/

Defined Under Namespace

Classes: Item

Constant Summary collapse

Empty =
Google::Protobuf::Empty
Runtime =
Client::Runtime
GetBulkStateResponse =
Runtime::GetBulkStateResponse
GetStateRequest =
Runtime::GetBulkStateRequest
SaveStateRequest =
Runtime::SaveStateRequest
DEFAULT_STORE_NAME =
'statestore'

Constants included from Rubyists::Dapr::Client

DAPR_PORT, DAPR_STUB, DAPR_URI

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rubyists::Dapr::Client

client, #client, singleton, #singleton

Constructor Details

#initialize(store_name) ⇒ State

Initialize the State Management client

Parameters:

  • store_name (String)

    name of the Dapr Configuration component to use



46
47
48
# File 'lib/dapr/client/state.rb', line 46

def initialize(store_name)
  @store_name = store_name
end

Instance Attribute Details

#store_nameObject (readonly)

Returns the value of attribute store_name.



15
16
17
# File 'lib/dapr/client/state.rb', line 15

def store_name
  @store_name
end

Class Method Details

.get(*keys, store_name: DEFAULT_STORE_NAME, metadata: {}) ⇒ GetBulkStateResponse

Returns The response from the Dapr State Management component.

Parameters:

  • keys (Array<String>)

    keys to retrieve from the state store

  • store_name (String) (defaults to: DEFAULT_STORE_NAME)

    name of the State Management component, defaults to ‘dapr-statestore’

  • metadata (Hash) (defaults to: {})

    metadata to include

Returns:



30
31
32
# File 'lib/dapr/client/state.rb', line 30

def self.get(*keys, store_name: DEFAULT_STORE_NAME, metadata: {})
  new(store_name).get(keys, metadata:)
end

.set(states, store_name: DEFAULT_STORE_NAME, metadata: {}) ⇒ Empty

Returns The response from the Dapr State Management component.

Parameters:

  • states (Hash)

    states to set in the state store, key/value pairs

  • store_name (String) (defaults to: DEFAULT_STORE_NAME)

    name of the State Management component, defaults to ‘dapr-statestore’

  • metadata (Hash) (defaults to: {})

    metadata to include

Returns:

  • (Empty)

    The response from the Dapr State Management component



39
40
41
# File 'lib/dapr/client/state.rb', line 39

def self.set(states, store_name: DEFAULT_STORE_NAME, metadata: {})
  new(store_name).set(states, metadata:)
end

Instance Method Details

#get(keys, metadata: {}) ⇒ Array<BulkStateItem>

Returns Array of items returned by the state store.

Parameters:

  • keys (Array<String>)

    keys to retrieve from the state store

  • metadata (Hash) (defaults to: {})

    metadata to include

Returns:

  • (Array<BulkStateItem>)

    Array of items returned by the state store



54
55
56
57
58
# File 'lib/dapr/client/state.rb', line 54

def get(keys, metadata: {})
  logger.debug('Getting state', keys:, store_name:, metadata:)
  response = singleton.get_bulk_state GetStateRequest.new(store_name:, keys:, metadata:)
  response.items.to_h { |i| [i.key, Item.new(data: i.data, etag: i.etag, metadata: i.)] }
end

#set(states, metadata: {}) ⇒ Empty

Returns The response from the Dapr State Management component (Empty means success).

Parameters:

  • states (Hash)

    states to set (key/values in the state store)

  • metadata (Hash) (defaults to: {})

    metadata to include

Returns:

  • (Empty)

    The response from the Dapr State Management component (Empty means success)



64
65
66
67
68
69
# File 'lib/dapr/client/state.rb', line 64

def set(states, metadata: {})
  states  = states.map { |key, value| { key:, value:, metadata: } }
  request = SaveStateRequest.new(store_name:, states:)
  logger.debug('Setting state', states:, store_name:)
  singleton.save_state request
end