Class: Legion::Data::Extract::Handlers::Jsonl

Inherits:
Base
  • Object
show all
Defined in:
lib/legion/data/extract/handlers/jsonl.rb

Class Method Summary collapse

Methods inherited from Base

available?, for_type, inherited, register, supported_types

Methods included from Logging::Helper

#handle_exception

Class Method Details

.extensionsObject



11
# File 'lib/legion/data/extract/handlers/jsonl.rb', line 11

def self.extensions = %w[.jsonl]

.extract(source) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/legion/data/extract/handlers/jsonl.rb', line 14

def self.extract(source)
  content = source.respond_to?(:read) ? source.read : File.read(source.to_s)
  lines = content.each_line.map { |l| ::JSON.parse(l.strip) rescue l.strip } # rubocop:disable Style/RescueModifier
  text = lines.map { |l| l.is_a?(Hash) ? ::JSON.pretty_generate(l) : l }.join("\n---\n")
  { text: text, metadata: { lines: lines.size } }
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :extract_jsonl)
  { text: nil, error: e.message }
end

.gem_nameObject



12
# File 'lib/legion/data/extract/handlers/jsonl.rb', line 12

def self.gem_name = nil

.typeObject



10
# File 'lib/legion/data/extract/handlers/jsonl.rb', line 10

def self.type = :jsonl