Class: UU::OS::Lang::BinaryValue
- Inherits:
-
Object
- Object
- UU::OS::Lang::BinaryValue
- Defined in:
- uu_os_commons-1.5.0/lib/uu/os/lang/binary_value.rb
Overview
Object representing binary data. This object is used for both, data upload (as request parameter) and data download (as response return value).
Constant Summary
Instance Attribute Summary (collapse)
-
- (String) content_type
readonly
Content type of binary value.
-
- (String) encoding
readonly
Encoding of text-based binary value.
-
- (String) name
readonly
Name (filename) of binary value.
-
- (Fixnum) size
readonly
Size (in bytes) of binary content.
Instance Method Summary (collapse)
-
- (IO) content {|io| ... }
Actual binary content.
-
- (BinaryValue) initialize(init_data = nil) {|content| ... }
constructor
Creates new instance of binary value.
Constructor Details
- (BinaryValue) initialize(init_data = nil) {|content| ... }
Creates new instance of binary value.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/binary_value.rb', line 117 def initialize(init_data = nil, &block) init_data ||= {} case init_data when StringIO @content = init_data @encoding = init_data.string.encoding.to_s when IO, Tempfile @content = init_data when String @encoding = init_data.encoding.to_s @content = StringIO.new(init_data) when UU::OS::Lang::BinaryValue @name = init_data.name @content_type = init_data.content_type @size = init_data.size @content = init_data.content @encoding = init_data.encoding else @name = init_data[:name] @content_type = init_data[:contentType] || init_data[:content_type] @size = init_data[:size] @encoding = init_data[:encoding] @content = init_data[:content] if @content.kind_of?(String) @encoding ||= @content.encoding.to_s @content = StringIO.new(@content) elsif @content.kind_of?(UU::OS::Lang::BinaryValue) @name ||= @content.name @content_type ||= @content.content_type @size ||= @content.size @encoding ||= @content.encoding @content = @content.content end end if block.nil? @name ||= File.basename(@content) if @content.respond_to?(:path) @size ||= @content.size if @content.respond_to?(:size) @content_type ||= @content.content_type if @content.respond_to?(:content_type) @encoding ||= @content.encoding.to_s if @content.respond_to?(:encoding) else @content, content = IO.pipe content.write('') Thread.new do begin block.yield(content) ensure content.close unless content.closed? end end end if @content_type =~ /;[ ]*charset=(.*)/i @encoding ||= $1.strip @content_type = @content_type.split(';')[0].strip end @content_type ||= DEFAULT_CONTENT_TYPE end |
Instance Attribute Details
- (String) content_type (readonly)
Content type of binary value. Defaults to
application/octet-stream
if not set.
79 80 81 |
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/binary_value.rb', line 79 def content_type @content_type end |
- (String) encoding (readonly)
Encoding of text-based binary value. If not set, all text is expected to be
in UTF-8
encoding.
89 90 91 |
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/binary_value.rb', line 89 def encoding @encoding end |
- (String) name (readonly)
Name (filename) of binary value. Might be nil
.
74 75 76 |
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/binary_value.rb', line 74 def name @name end |
- (Fixnum) size (readonly)
Size (in bytes) of binary content. Might be nil
in case size
of binary content is unknown.
84 85 86 |
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/binary_value.rb', line 84 def size @size end |
Instance Method Details
- (IO) content {|io| ... }
Actual binary content. Content must not be considered as rewindable and must be always closed once processing of binary value is finished and you do not use block variant.
201 202 203 204 205 206 207 208 |
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/binary_value.rb', line 201 def content if block_given? yield @content @content.close else return @content end end |