Class: T2Server::PortValue
- Inherits:
-
Object
- Object
- T2Server::PortValue
- Defined in:
- lib/t2-server/port.rb
Overview
A class to represent an output port data value.
Constant Summary collapse
- ERROR_TYPE =
The mime-type we use for an error value.
"application/x-error"
- 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.
"application/x-empty"
- @@to_s =
Used within #inspect, below to help override the built in version.
Kernel.instance_method(:to_s)
Instance Attribute Summary collapse
-
#reference ⇒ Object
readonly
The URI reference of this port value as a String.
-
#size ⇒ Object
readonly
The size (in bytes) of the port value.
-
#type ⇒ Object
readonly
The mime type of this port value as a String.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
:call-seq: empty? -> true or false.
-
#error? ⇒ Boolean
:call-seq: error? -> true or false.
-
#initialize(port, ref, error, size, type = "") ⇒ PortValue
constructor
:stopdoc:.
-
#inspect ⇒ Object
:call-seq: inspect -> string.
-
#stream_value(stream, range = 0...@size) ⇒ Object
:call-seq: stream_value(stream) -> fixnum stream_value(stream, range) -> fixnum.
-
#value(range = 0...@size, &block) ⇒ Object
:call-seq: value -> binary blob value(range) -> binary blob value {|chunk| …} value(range) {|chunk| …}.
-
#write_value_to_file(filename, range = 0...@size) ⇒ Object
:call-seq: write_value_to_file(filename) -> fixnum write_value_to_file(filename, range) -> fixnum.
Constructor Details
#initialize(port, ref, error, size, type = "") ⇒ PortValue
:stopdoc:
448 449 450 451 452 453 454 |
# File 'lib/t2-server/port.rb', line 448 def initialize(port, ref, error, size, type = "") @port = port @reference = URI.parse(ref) @type = (error ? ERROR_TYPE : type) @size = size @error = error end |
Instance Attribute Details
#reference ⇒ Object (readonly)
The URI reference of this port value as a String.
432 433 434 |
# File 'lib/t2-server/port.rb', line 432 def reference @reference end |
#size ⇒ Object (readonly)
The size (in bytes) of the port value.
438 439 440 |
# File 'lib/t2-server/port.rb', line 438 def size @size end |
#type ⇒ Object (readonly)
The mime type of this port value as a String.
435 436 437 |
# File 'lib/t2-server/port.rb', line 435 def type @type end |
Instance Method Details
#empty? ⇒ Boolean
:call-seq:
empty? -> true or false
Is this port value empty?
548 549 550 |
# File 'lib/t2-server/port.rb', line 548 def empty? @type == EMPTY_TYPE end |
#error? ⇒ Boolean
:call-seq:
error? -> true or false
Does this port represent an error?
540 541 542 |
# File 'lib/t2-server/port.rb', line 540 def error? @error end |
#inspect ⇒ Object
:call-seq:
inspect -> string
Return a printable representation of this port value for debugging purposes.
560 561 562 563 564 |
# File 'lib/t2-server/port.rb', line 560 def inspect @@to_s.bind(self).call.sub!(/>\z/) { " @value=#{value.inspect}, " + "@type=#{type.inspect}, @size=#{size.inspect}>" } end |
#stream_value(stream, range = 0...@size) ⇒ Object
:call-seq:
stream_value(stream) -> fixnum
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.
509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/t2-server/port.rb', line 509 def stream_value(stream, range = 0...@size) raise ArgumentError, "Stream passed in must provide a write method" unless stream.respond_to? :write bytes = 0 value(range) do |chunk| bytes += stream.write(chunk) end bytes end |
#value(range = 0...@size, &block) ⇒ Object
:call-seq:
value -> binary blob
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.
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/t2-server/port.rb', line 480 def value(range = 0...@size, &block) # The following block is a workaround for Taverna Server versions prior # to 2.4.1 and can be removed when support for those versions is no # longer required. if error? && @size == 0 value = @port.download(@reference) @size = value.size range = 0...@size if range.min.nil? return value[range] end return "" if @type == EMPTY_TYPE # Check that the range provided is sensible range = 0..range.max if range.min < 0 range = range.min...@size if range.max >= @size @port.download(@reference, range, &block) end |
#write_value_to_file(filename, range = 0...@size) ⇒ Object
:call-seq:
write_value_to_file(filename) -> fixnum
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.
530 531 532 533 534 |
# File 'lib/t2-server/port.rb', line 530 def write_value_to_file(filename, range = 0...@size) File.open(filename, "wb") do |file| stream_value(file, range) end end |