Class: Micdrop::Ext::JsonLines::JsonLinesSource

Inherits:
Object
  • Object
show all
Defined in:
lib/micdrop/ext/json_lines.rb

Overview

Takes a file, such as a pipe to another process, and interpret the results as JSON-Lines (ND-JSON)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, close: true) ⇒ JsonLinesSource

Returns a new instance of JsonLinesSource.



13
14
15
16
# File 'lib/micdrop/ext/json_lines.rb', line 13

def initialize(file, close: true)
  @file = file
  @close = close
end

Class Method Details

.from_command(*args, **kwargs) ⇒ Object



18
19
20
# File 'lib/micdrop/ext/json_lines.rb', line 18

def self.from_command(*args, **kwargs)
  self.class.new(IO.popen(*args, **kwargs), close: true)
end

Instance Method Details

#closeObject



30
31
32
# File 'lib/micdrop/ext/json_lines.rb', line 30

def close
  @file.close if @close
end

#eachObject



22
23
24
25
26
27
28
# File 'lib/micdrop/ext/json_lines.rb', line 22

def each
  return enum_for unless block_given?

  @file.each_line do |line|
    yield JSON.parse line
  end
end