class T2Server::PortValue

A class to represent an output port data value.

Constants

EMPTY_TYPE

The mime-type we use for an empty value. Note that an empty value is not simply an empty string. It is the complete absence of a value.

ERROR_TYPE

The mime-type we use for an error value.

Attributes

reference[R]

The URI reference of this port value as a String.

size[R]

The size (in bytes) of the port value.

type[R]

The mime type of this port value as a String.

Public Instance Methods

empty? → true or false click to toggle source

Is this port value empty?

    # File lib/t2-server/port.rb
531 def empty?
532   @type == EMPTY_TYPE
533 end
error? → true or false click to toggle source

Does this port represent an error?

    # File lib/t2-server/port.rb
523 def error?
524   @error
525 end
inspect → string click to toggle source

Return a printable representation of this port value for debugging purposes.

    # File lib/t2-server/port.rb
543 def inspect
544   @@to_s.bind(self).call.sub!(/>\z/) { " @value=#{value.inspect}, " +
545     "@type=#{type.inspect}, @size=#{size.inspect}>"
546   }
547 end
stream_value(stream) → fixnum click to toggle source
stream_value(stream, range) → fixnum

Stream this port value directly into another stream. The stream passed in may be anything that provides a write method; instances of IO and File, for example. No data is cached by this method.

The number of bytes written to the stream is returned.

    # File lib/t2-server/port.rb
492 def stream_value(stream, range = nil)
493   raise ArgumentError,
494     "Stream passed in must provide a write method" unless
495       stream.respond_to? :write
496 
497   bytes = 0
498 
499   value(range) do |chunk|
500     bytes += stream.write(chunk)
501   end
502 
503   bytes
504 end
value → binary blob click to toggle source
value(range) → binary blob
value {|chunk| ...}
value(range) {|chunk| ...}

Get the value of this port from the server.

If no parameters are supplied then this method will simply download and return all the data.

Passing in a block will allow access to the underlying data stream so the data is not stored in memory:

run.output_port("port") do |chunk|
  print chunk
end

In both cases supplying a Range will download and return the data in that range.

This method does not cache any data.

If this port is an error then this value will be the error message.

    # File lib/t2-server/port.rb
461 def value(range = nil, &block)
462   # The following block is a workaround for Taverna Server versions prior
463   # to 2.4.1 and can be removed when support for those versions is no
464   # longer required.
465   if error? && @size == 0
466     value = @port.download(@reference)
467     @size = value.size
468     range = 0...@size if range.nil? || range.min.nil?
469     return value[range]
470   end
471 
472   return "" if @type == EMPTY_TYPE
473 
474   # Check that the range provided is sensible
475   unless range.nil?
476     range = 0..range.max if range.min < 0
477     range = range.min...@size if range.max >= @size
478   end
479 
480   @port.download(@reference, range, &block)
481 end
write_value_to_file(filename) → fixnum click to toggle source
write_value_to_file(filename, range) → fixnum

Stream this port value directly to a file. If a range is supplied then just that range of data is downloaded from the server. No data is cached by this method.

    # File lib/t2-server/port.rb
513 def write_value_to_file(filename, range = nil)
514   File.open(filename, "wb") do |file|
515     stream_value(file, range)
516   end
517 end