Class: Prescient::DocumentSource::RedisJson

Inherits:
Base
  • Object
show all
Defined in:
lib/prescient/document_source.rb

Overview

Read JSON documents from a Redis-compatible client. The client is injected to keep Redis optional for gem consumers.

Instance Method Summary collapse

Constructor Details

#initialize(client:, key:, **options) ⇒ RedisJson

Returns a new instance of RedisJson.

Parameters:

  • client (#get)

    Redis-compatible client

  • key (String)

    Redis key containing a JSON document or array

Raises:



94
95
96
97
98
99
100
101
# File 'lib/prescient/document_source.rb', line 94

def initialize(client:, key:, **options)
  super(**options)
  @client = client
  @key = key
  return if @key.is_a?(String) && !@key.empty?

  raise Prescient::Error, "Redis document key must be a non-empty string"
end

Instance Method Details

#fetchArray<Hash>

Returns Documents loaded from Redis.

Returns:

  • (Array<Hash>)

    Documents loaded from Redis



104
105
106
107
108
109
110
111
# File 'lib/prescient/document_source.rb', line 104

def fetch
  value = @client.get(@key)
  raise Prescient::Error, "Redis document source key not found: #{@key}" unless value

  parse_json(value)
rescue NoMethodError
  raise Prescient::Error, "Redis document source client must provide get"
end