Class: OutputManager

Inherits:
Object
  • Object
show all
Defined in:
lib/mangledotdev.rb

Overview

OutputManager - Manages receiving requests from other processes and sending responses.

This is a class-based/static manager - all methods are class methods. Must call init() before using.

Class Attributes:

data: The request data (accessible after init())

Class Methods:

init(): Initialize and read request from stdin
output(val): Send response back via stdout

Constant Summary collapse

@@original_stdout =
nil
@@request =
nil
@@data =
nil
@@request_status =
nil
@@optional =
nil
@@unique_state =
nil
@@init_error =
nil
@@errors =
[]
@@warnings =
[]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataObject

Returns the value of attribute data.



319
320
321
# File 'lib/mangledotdev.rb', line 319

def data
  @data
end

Class Method Details

.bundle(value) ⇒ Object

Bundle any value for use with output() - for API consistency with other languages. In Ruby, this just returns the value as-is since Ruby handles serialization automatically.

Parameters:

  • value (Object)

    Any value

Returns:

  • (Object)

    The same value (Ruby handles JSON serialization automatically)



390
391
392
# File 'lib/mangledotdev.rb', line 390

def bundle(value)
  value
end

.initObject

Initialize OutputManager and read request from stdin.

Must be called before using output() or accessing data. Suppresses stdout to prevent pollution of JSON protocol.



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/mangledotdev.rb', line 325

def init
  # Save original stdout so we can restore it later

  @@original_stdout = $stdout

  # Redirect stdout to StringIO to suppress all puts/print statements

  $stdout = StringIO.new

  # Read the entire stdin (the JSON request from InputManager)

  @@request = $stdin.read
  @@data = JSON.parse(@@request)
  self.data = @@data['data']
  @@optional = @@data['optionalOutput']

  # Reset state for new request

  @@errors = []
  @@warnings = []
  @@init_error = nil
  @@request_status = nil
  @@unique_state = nil
end

.output(val) ⇒ Object

Send response back to the calling process.

Note:

Can be called multiple times if isUnique=false in request.
Will error if called multiple times when isUnique=true.

Parameters:

  • val (Object)

    Data to send (any JSON-serializable type)



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/mangledotdev.rb', line 353

def output(val)
  # Check if OutputManager was initialized

  if @@data.nil?
    unless @@init_error
      $stdout = @@original_stdout if @@original_stdout

      @@request_status = false
      @@errors << "Error: OutputManager isn't initialized."
      write_output(nil, @@data)
      @@init_error = true
    end
  else
    # Check if we can output based on isUnique setting

    # unique_state tracks if we've already output once

    if !@@unique_state || !@@data['isUnique']
      @@request_status = true
      write_output(val, @@data)
    else
      # Multiple outputs when isUnique=true is an error

      @@request_status = false
      @@errors << "Error: outputs out of bound (isUnique: #{@@unique_state})."
      write_output(val, @@data)
    end

    # Mark that we've output once

    @@unique_state = @@data['isUnique']

    # Re-suppress stdout after writing response

    $stdout = StringIO.new
  end
end