Class: Prescient::DocumentSource::JsonFile
- Defined in:
- lib/prescient/document_source.rb
Overview
Read JSON documents from a local file.
Instance Method Summary collapse
-
#fetch ⇒ Array<Hash>
Documents loaded from the file.
-
#initialize(path:, **options) ⇒ JsonFile
constructor
A new instance of JsonFile.
Constructor Details
#initialize(path:, **options) ⇒ JsonFile
Returns a new instance of JsonFile.
70 71 72 73 74 75 76 |
# File 'lib/prescient/document_source.rb', line 70 def initialize(path:, **) super(**) @path = path return if @path.is_a?(String) && !@path.empty? raise Prescient::Error, "document source path must be a non-empty string" end |
Instance Method Details
#fetch ⇒ Array<Hash>
Returns Documents loaded from the file.
79 80 81 82 83 84 85 86 |
# File 'lib/prescient/document_source.rb', line 79 def fetch raise Prescient::Error, "document source file not found: #{@path}" unless File.file?(@path) raise Prescient::Error, "document source file exceeds #{@max_bytes} bytes" if File.size(@path) > @max_bytes parse_json(File.read(@path)) rescue Errno::EACCES => e raise Prescient::Error, "unable to read document source: #{e.}" end |