Class: Prescient::DocumentSource::JsonFile

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

Overview

Read JSON documents from a local file.

Instance Method Summary collapse

Constructor Details

#initialize(path:, **options) ⇒ JsonFile

Returns a new instance of JsonFile.

Parameters:

  • path (String)

    JSON file path

Raises:



70
71
72
73
74
75
76
# File 'lib/prescient/document_source.rb', line 70

def initialize(path:, **options)
  super(**options)
  @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

#fetchArray<Hash>

Returns Documents loaded from the file.

Returns:

  • (Array<Hash>)

    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.message}"
end