Class: PatientHttp::PayloadStore::FileStore

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

Overview

File-based payload store for testing and development.

Stores payloads as JSON files in a directory. This store is intended for local development and testing only - use Redis or S3 stores for production deployments.

Thread-safe through mutex synchronization.

Examples:

Configuration

config.register_payload_store(:files, adapter: :file, directory: "/tmp/payloads")

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

create, #generate_key, lookup, register, registered_adapters, #store

Constructor Details

#initialize(directory: nil) ⇒ FileStore

Initialize a new file store.

Parameters:

  • directory (String) (defaults to: nil)

    Directory for storing payload files. Defaults to Dir.tmpdir. Will be created if it doesn’t exist.



27
28
29
30
31
# File 'lib/patient_http/payload_store/file_store.rb', line 27

def initialize(directory: nil)
  @directory = directory || Dir.tmpdir
  @mutex = Mutex.new
  FileUtils.mkdir_p(@directory)
end

Instance Attribute Details

#directoryString (readonly)

Returns The directory where payload files are stored.

Returns:

  • (String)

    The directory where payload files are stored



21
22
23
# File 'lib/patient_http/payload_store/file_store.rb', line 21

def directory
  @directory
end

Instance Method Details

#delete(key) ⇒ Boolean

Delete a payload file.

Idempotent - does not raise if file doesn’t exist.

Parameters:

  • key (String)

    The key to delete

Returns:

  • (Boolean)

    true



65
66
67
68
69
70
71
72
73
# File 'lib/patient_http/payload_store/file_store.rb', line 65

def delete(key)
  path = file_path(key)
  @mutex.synchronize do
    File.delete(path) if File.exist?(path)
  end
  true
rescue Errno::ENOENT
  true
end

#exists?(key) ⇒ Boolean

Check if a payload exists.

Parameters:

  • key (String)

    The key to check

Returns:

  • (Boolean)

    true if the payload exists



79
80
81
82
83
# File 'lib/patient_http/payload_store/file_store.rb', line 79

def exists?(key)
  @mutex.synchronize do
    File.exist?(file_path(key))
  end
end

#fetch(key) ⇒ Hash?

Fetch data from a JSON file.

Parameters:

  • key (String)

    The key to fetch

Returns:

  • (Hash, nil)

    The stored data or nil if not found



50
51
52
53
54
55
56
57
# File 'lib/patient_http/payload_store/file_store.rb', line 50

def fetch(key)
  path = file_path(key)
  @mutex.synchronize do
    return nil unless File.exist?(path)

    JSON.parse(File.read(path))
  end
end

#store_json(key, json) ⇒ String

Store pre-serialized JSON string directly to a file.

Parameters:

  • key (String)

    Unique key (used as filename)

  • json (String)

    Pre-serialized JSON string

Returns:

  • (String)

    The key



38
39
40
41
42
43
44
# File 'lib/patient_http/payload_store/file_store.rb', line 38

def store_json(key, json)
  path = file_path(key)
  @mutex.synchronize do
    File.write(path, json)
  end
  key
end