Class: TrustedSandbox::Response
- Inherits:
-
Object
- Object
- TrustedSandbox::Response
- Defined in:
- lib/trusted_sandbox/response.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#error_to_raise ⇒ Object
readonly
Returns the value of attribute error_to_raise.
-
#host_code_dir_path ⇒ Object
readonly
Returns the value of attribute host_code_dir_path.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#output_file_name ⇒ Object
readonly
Returns the value of attribute output_file_name.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
-
.error(error, error_to_raise, stdout = nil, stderr = nil) ⇒ Response
Object initialized with error details.
-
.shortcut(output, stdout = nil, stderr = nil) ⇒ Response
This is used when user decides not to go through docker.
Instance Method Summary collapse
-
#initialize(stdout = nil, stderr = nil, host_code_dir_path = nil, output_file_name = nil) ⇒ Response
constructor
A new instance of Response.
-
#output! ⇒ Object
The output returned by the container.
-
#parse! ⇒ nil
Parses the output file and stores the values in the appropriate ivars.
- #valid? ⇒ Boolean
Constructor Details
#initialize(stdout = nil, stderr = nil, host_code_dir_path = nil, output_file_name = nil) ⇒ Response
Returns a new instance of Response.
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
#error ⇒ Object (readonly)
Returns the value of attribute error.
4 5 6 |
# File 'lib/trusted_sandbox/response.rb', line 4 def error @error end |
#error_to_raise ⇒ Object (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_path ⇒ Object (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 |
#output ⇒ Object (readonly)
Returns the value of attribute output.
4 5 6 |
# File 'lib/trusted_sandbox/response.rb', line 4 def output @output end |
#output_file_name ⇒ Object (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_response ⇒ Object (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 |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/trusted_sandbox/response.rb', line 4 def status @status end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
4 5 6 |
# File 'lib/trusted_sandbox/response.rb', line 4 def stderr @stderr end |
#stdout ⇒ Object (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.
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
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.
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
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
50 51 52 |
# File 'lib/trusted_sandbox/response.rb', line 50 def valid? status == 'success' end |