Class: PatientHttp::PayloadStore::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_http/payload_store/base.rb

Overview

Abstract base class for payload stores.

Payload stores provide external storage for Request and Response objects that exceed the configured size threshold. This keeps job arguments small while allowing large payloads to be processed.

Subclasses must implement the abstract methods: store, fetch, and delete.

Examples:

Creating a custom store

class MyStore < PatientHttp::PayloadStore::Base
  register :my_store, self

  def initialize(connection:)
    @connection = connection
    @mutex = Mutex.new
  end

  def store(key, data)
    @mutex.synchronize { @connection.set(key, JSON.generate(data)) }
    key
  end

  def fetch(key)
    @mutex.synchronize { JSON.parse(@connection.get(key)) }
  rescue KeyNotFoundError
    nil
  end

  def delete(key)
    @mutex.synchronize { @connection.delete(key) }
    true
  rescue KeyNotFoundError
    true
  end
end

Direct Known Subclasses

ActiveRecordStore, FileStore, RedisStore, S3Store

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(name, **options) ⇒ Base

Create a new store instance from a registered adapter.

Parameters:

  • name (Symbol, String)

    The adapter name

  • options (Hash)

    Options to pass to the adapter constructor

Returns:

  • (Base)

    A new store instance

Raises:

  • (ArgumentError)

    If the adapter is not registered



71
72
73
74
75
76
# File 'lib/patient_http/payload_store/base.rb', line 71

def create(name, **options)
  klass = lookup(name)
  raise ArgumentError, "Unknown payload store adapter: #{name.inspect}" unless klass

  klass.new(**options)
end

.lookup(name) ⇒ Class?

Look up a registered adapter by name.

Parameters:

  • name (Symbol, String)

    The adapter name

Returns:

  • (Class, nil)

    The adapter class or nil if not found



59
60
61
62
63
# File 'lib/patient_http/payload_store/base.rb', line 59

def lookup(name)
  registry_mutex.synchronize do
    registry[name.to_sym]
  end
end

.register(name, klass) ⇒ void

This method returns an undefined value.

Register a payload store adapter.

Parameters:

  • name (Symbol)

    Unique identifier for this adapter

  • klass (Class)

    The adapter class



49
50
51
52
53
# File 'lib/patient_http/payload_store/base.rb', line 49

def register(name, klass)
  registry_mutex.synchronize do
    registry[name.to_sym] = klass
  end
end

.registered_adaptersArray<Symbol>

List all registered adapter names.

Returns:

  • (Array<Symbol>)

    Registered adapter names



81
82
83
84
85
# File 'lib/patient_http/payload_store/base.rb', line 81

def registered_adapters
  registry_mutex.synchronize do
    registry.keys
  end
end

Instance Method Details

#delete(key) ⇒ Boolean

Delete data by key.

This method should be idempotent - deleting a non-existent key should not raise an error.

Parameters:

  • key (String)

    The key to delete

Returns:

  • (Boolean)

    true

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



138
139
140
# File 'lib/patient_http/payload_store/base.rb', line 138

def delete(key)
  raise NotImplementedError, "#{self.class.name} must implement #delete"
end

#fetch(key) ⇒ Hash?

Fetch data by key.

Parameters:

  • key (String)

    The key to fetch

Returns:

  • (Hash, nil)

    The stored data or nil if not found

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



126
127
128
# File 'lib/patient_http/payload_store/base.rb', line 126

def fetch(key)
  raise NotImplementedError, "#{self.class.name} must implement #fetch"
end

#generate_keyString

Generate a unique key for storing data.

Returns:

  • (String)

    A UUID key



145
146
147
# File 'lib/patient_http/payload_store/base.rb', line 145

def generate_key
  SecureRandom.uuid
end

#store(key, data) ⇒ String

Store data with the given key.

Parameters:

  • key (String)

    Unique key for this data

  • data (Hash)

    The data to store (will be serialized as JSON)

Returns:

  • (String)

    The key



103
104
105
106
# File 'lib/patient_http/payload_store/base.rb', line 103

def store(key, data)
  json = JSON.generate(data)
  store_json(key, json)
end

#store_json(key, json) ⇒ String

Store pre-serialized JSON data with the given key.

Subclasses that serialize in #store should override this to write the string directly, avoiding double serialization.

Parameters:

  • key (String)

    Unique key for this data

  • json (String)

    Pre-serialized JSON string

Returns:

  • (String)

    The key

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



117
118
119
# File 'lib/patient_http/payload_store/base.rb', line 117

def store_json(key, json)
  raise NotImplementedError, "#{self.class.name} must implement #store_json"
end