class Google::Apis::Core::Multipart

Helper for building multipart requests

Constants

Attributes

content_type[R]

@return [String]

Content type header

Public Class Methods

new(content_type: MULTIPART_RELATED, boundary: nil) click to toggle source

@param [String] content_type

Content type for the multipart request

@param [String] boundary

Part delimiter
# File lib/google/apis/core/multipart.rb, line 87
def initialize(content_type: MULTIPART_RELATED, boundary: nil)
  @parts = []
  @boundary = boundary || Digest::SHA1.hexdigest(SecureRandom.random_bytes(8))
  @content_type = "#{content_type}; boundary=#{@boundary}"
end

Public Instance Methods

add_json(body, content_id: nil) click to toggle source

Append JSON data part

@param [String] body

JSON text

@param [String] content_id

Optional unique ID of this part

@return [self]

# File lib/google/apis/core/multipart.rb, line 100
def add_json(body, content_id: nil)
  header = {}
  header['Content-ID'] = content_id unless content_id.nil?
  @parts << Google::Apis::Core::JsonPart.new(body, header).to_io(@boundary)
  self
end
add_upload(upload_io, content_type: nil, content_id: nil) click to toggle source

Append arbitrary data as a part

@param [IO] upload_io

IO stream

@param [String] content_id

Optional unique ID of this part

@return [self]

# File lib/google/apis/core/multipart.rb, line 114
def add_upload(upload_io, content_type: nil, content_id: nil)
  header = {
      'Content-Type' => content_type || 'application/octet-stream'
  }
  header['Content-Id'] = content_id unless content_id.nil?
  @parts << Google::Apis::Core::FilePart.new(upload_io,
                                             header).to_io(@boundary)
  self
end
assemble() click to toggle source

Assemble the multipart requests

@return [IO]

IO stream
# File lib/google/apis/core/multipart.rb, line 128
def assemble
  @parts <<  StringIO.new("--#{@boundary}--\r\n\r\n")
  Google::Apis::Core::CompositeIO.new(*@parts)
end