Class: PatientHttp::PayloadStore::FileStore
- 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.
Instance Attribute Summary collapse
-
#directory ⇒ String
readonly
The directory where payload files are stored.
Instance Method Summary collapse
-
#delete(key) ⇒ Boolean
Delete a payload file.
-
#exists?(key) ⇒ Boolean
Check if a payload exists.
-
#fetch(key) ⇒ Hash?
Fetch data from a JSON file.
-
#initialize(directory: nil) ⇒ FileStore
constructor
Initialize a new file store.
-
#store_json(key, json) ⇒ String
Store pre-serialized JSON string directly to a file.
Methods inherited from Base
create, #generate_key, lookup, register, registered_adapters, #store
Constructor Details
#initialize(directory: nil) ⇒ FileStore
Initialize a new file store.
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
#directory ⇒ String (readonly)
Returns 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.
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.
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.
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.
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 |