Sha256: 57e971e4d4c61b1e5b14a82068012c33b2e93913532663d3ac266932bab0f3c6

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'json'

# A writer for Traject::Indexer, that just writes out
# all the output as Json. It's newline delimitted json, but
# right now no checks to make sure there is no internal newlines
# as whitespace in the json. TODO, add that. 
#
# Not currently thread-safe (have to make sure whole object and newline
# get written without context switch. Can be made so.)
#
# You can force pretty-printing with setting 'json_writer.pretty_print' of boolean
# true or string 'true'.  Useful mostly for human checking of output. 
#
# Output will be sent to settings["output_file"] string path, or else
# settings["output_stream"] (ruby IO object), or else stdout. 
class Traject::JsonWriter
  attr_reader :settings

  def initialize(argSettings)
    @settings = argSettings
  end

  def put(context)
    hash = context.output_hash

    serialized = 
      if settings["json_writer.pretty_print"]
        JSON.pretty_generate(hash)
      else
        JSON.generate(hash)
      end
    output_file.puts(serialized)
  end

  def output_file
    unless defined? @output_file
      @output_file = 
        if settings["output_file"]
          File.open(settings["output_file"], 'w:UTF-8')
        elsif settings["output_stream"]
          settings["output_stream"]
        else
          $stdout
        end
    end
    return @output_file
  end

  def close 
    @output_file.close unless (@output_file.nil? || @output_file.tty?)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
traject-0.9.1 lib/traject/json_writer.rb