Class: UU::OS::Lang::BinaryValue

Inherits:
Object
  • Object
show all
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).

Examples:

Init binary value from string


bv = UU::OS::Lang::BinaryValue.new("Some content.")

bv.content      # StringIO
bv.content.read # String 'Some content.'
bv.size         # Fixnum 13, extracted from original String
bv.content_type # String 'application/octet-stream', default value
bv.encoding     # String 'UTF-8', extracted from original String
bv.name         # nil

Init binary value from block


bv = UU::OS::Lang::BinaryValue.new do |io|
  io.write("Some content.")
end

bv.content      # IO
bv.content.read # String 'Some content.'
bv.size         # nil
bv.content_type # String 'application/octet-stream', default value
bv.encoding     # nil
bv.name         # nil

Init binary value from block with params


bv = UU::OS::Lang::BinaryValue.new(content_type: 'application/xml', encoding: 'US-ASCII') do |io|
  io.write("Some content.")
end

bv.content      # IO
bv.content.read # String 'Some content.'
bv.size         # nil
bv.content_type # String 'application/xml'
bv.encoding     # String 'US-ASCII'
bv.name         # nil

Init binary value from file


bv = UU::OS::Lang::BinaryValue.new(File.open('content.xml', 'r'))

bv.content      # File
bv.content.read # String '<xml></xml>'
bv.size         # 460, extracted from original File
bv.content_type # String 'application/octet-stream', default value
bv.encoding     # nil
bv.name         # String 'content.xml', extracted from original File

Init binary value from file with params

bv = UU::OS::Lang::BinaryValue.new(
    name: 'MyName',
    content_type: 'application/xml',
    encoding: 'US-ASCII',
    content: File.open('content.xml', 'r')
  )

bv.content      # File
bv.content.read # String '<xml></xml>'
bv.size         # 460, extracted from original File
bv.content_type # String 'application/xml'
bv.encoding     # String 'US-ASCII'
bv.name         # String 'MyName', overwrote by parameter

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (BinaryValue) initialize(init_data = nil) {|content| ... }

Creates new instance of binary value.

Examples:

# init binary value from string
bv = UU::OS::Lang::BinaryValue.new("Some content.")

# init binary value from block with params
bv = UU::OS::Lang::BinaryValue.new(content_type: 'application/xml') do |io|
  io.write("Some content.")
end

Parameters:

  • init_data (Hash, UU::OS::Lang::BinaryValue) (defaults to: nil)

    Initialization attributes.

  • [Proc] (Hash)

    a customizable set of options

Options Hash (init_data):

  • :name (String)

    Name (filename) of binary value. If not set, name is obtained from actual binary content if possible.

  • :size (Fixnum)

    Size (in bytes) of binary content. If not set, size is obtained from actual binary content if possible.

  • :content_type (String)

    Content type of binary value. If not set, content type is obtained from actual binary content if possible.

  • :encoding (String)

    Encoding of text-based binary value. If not set, encoding is obtained from actual binary content. If not possible, all text is expected to be in UTF-8 encoding.

  • :content (IO, String)

    Actual binary content to be wrapped. In case block for generating binary content is used, this value is completely ignored.

Yields:

  • (content)

    Block with one parameter that is initial binary content.

Yield Parameters:

  • content (IO)

    IO where new content will be written.



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.

Returns:

  • (String)


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.

Returns:

  • (String)


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.

Returns:

  • (String)


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.

Returns:

  • (Fixnum)


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.

Examples:


# write binary content to file
bv = UU::OS::Lang::BinaryValue.new("Some content.")
io = bv.content
file = File.open('content.txt', 'w')
file.write io.read

# you must close file and binary content
file.close
io.close

# same as above using block, if you use block, you don't have to call close on binary content or file
bv = UU::OS::Lang::BinaryValue.new("Some content.")
bv.content do |io|
  File.open('content.txt', 'w') do |file|
    file.write(io.read)
  end
end

Yields:

  • (io)

    Block with one parameter that is actual binary content, binary content is automatically closed after leaving block.

Yield Parameters:

  • comp (IO)

    Actual binary content.

Returns:

  • (IO)


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