Class: TrustedSandbox::Response

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = nil, stderr = nil, host_code_dir_path = nil, output_file_name = nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • stdout (String, Array) (defaults to: nil)

    response of stdout from the container

  • stderr (String, Array) (defaults to: nil)

    response of stderr from the container

  • host_code_dir_path (String) (defaults to: nil)

    path to the folder where the argument value needs to be stored

  • output_file_name (String) (defaults to: nil)

    name of output file inside the host_code_dir_path



11
12
13
14
15
16
# File 'lib/trusted_sandbox/response.rb', line 11

def initialize(stdout = nil, stderr = nil, host_code_dir_path = nil, output_file_name = nil)
  @stdout = [stdout].flatten.compact
  @stderr = [stderr].flatten.compact
  @host_code_dir_path = host_code_dir_path
  @output_file_name = output_file_name
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def error
  @error
end

#error_to_raiseObject (readonly)

Returns the value of attribute error_to_raise.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def error_to_raise
  @error_to_raise
end

#host_code_dir_pathObject (readonly)

Returns the value of attribute host_code_dir_path.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def host_code_dir_path
  @host_code_dir_path
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def output
  @output
end

#output_file_nameObject (readonly)

Returns the value of attribute output_file_name.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def output_file_name
  @output_file_name
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def raw_response
  @raw_response
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def status
  @status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



4
5
6
# File 'lib/trusted_sandbox/response.rb', line 4

def stdout
  @stdout
end

Class Method Details

.error(error, error_to_raise, stdout = nil, stderr = nil) ⇒ Response

Returns object initialized with error details.

Parameters:

  • error (StandardError)

    error object that was raised during execution of the code

  • error_to_raise (Class)

    an error class in the TrustedSandbox module.

  • stdout (String) (defaults to: nil)
  • stderr (String) (defaults to: nil)

Returns:

  • (Response)

    object initialized with error details



25
26
27
28
29
30
31
32
33
# File 'lib/trusted_sandbox/response.rb', line 25

def self.error(error, error_to_raise, stdout = nil, stderr = nil)
  obj = new(stdout, stderr)
  obj.instance_eval do
    @status = 'error'
    @error = error
    @error_to_raise = error_to_raise.new(error)
  end
  obj
end

.shortcut(output, stdout = nil, stderr = nil) ⇒ Response

This is used when user decides not to go through docker

Parameters:

  • output (Object)

    the result of the code execution

  • stdout (String) (defaults to: nil)
  • stderr (String) (defaults to: nil)

Returns:

  • (Response)

    object initialized with output



40
41
42
43
44
45
46
47
# File 'lib/trusted_sandbox/response.rb', line 40

def self.shortcut(output, stdout = nil, stderr = nil)
  obj = new(stdout, stderr)
  obj.instance_eval do
    @status = 'success'
    @output = output
  end
  obj
end

Instance Method Details

#output!Object

Returns the output returned by the container. Raises errors if encountered.

Returns:

  • (Object)

    the output returned by the container. Raises errors if encountered.

Raises:



57
58
59
60
# File 'lib/trusted_sandbox/response.rb', line 57

def output!
  propagate_errors!
  output
end

#parse!nil

Parses the output file and stores the values in the appropriate ivars

Returns:

  • (nil)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/trusted_sandbox/response.rb', line 64

def parse!
  unless File.exists? output_file_path
    @status = 'error'
    @error = ContainerError.new('User code did not finish properly')
    @error_to_raise = @error
    return
  end

  begin
    data = File.binread output_file_path
    @raw_response = Marshal.load(data)
  rescue => e
    @status = 'error'
    @error = e
    @error_to_raise = ContainerError.new(e)
    return
  end

  unless ['success', 'error'].include? @raw_response[:status]
    @status = 'error'
    @error = InternalError.new('Output file has invalid format')
    @error_to_raise = @error
    return
  end

  @status = @raw_response[:status]
  @output = @raw_response[:output]
  @error = @raw_response[:error]
  @error_to_raise = UserCodeError.new(@error) if @error
  nil
end

#valid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/trusted_sandbox/response.rb', line 50

def valid?
  status == 'success'
end