Class: Google::Apis::Core::Multipart

Inherits:
Object
  • Object
show all
Defined in:
lib/google/apis/core/multipart.rb

Overview

Helper for building multipart requests

Constant Summary collapse

'multipart/related'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type: MULTIPART_RELATED, boundary: nil) ⇒ Multipart

Returns a new instance of Multipart.

Parameters:

  • content_type (String) (defaults to: MULTIPART_RELATED)

    Content type for the multipart request

  • boundary (String) (defaults to: nil)

    Part delimiter



87
88
89
90
91
# 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

Instance Attribute Details

#content_typeString (readonly)

Returns Content type header.

Returns:

  • (String)

    Content type header



80
81
82
# File 'lib/google/apis/core/multipart.rb', line 80

def content_type
  @content_type
end

Instance Method Details

#add_json(body, content_id: nil) ⇒ self

Append JSON data part

Parameters:

  • body (String)

    JSON text

  • content_id (String) (defaults to: nil)

    Optional unique ID of this part

Returns:

  • (self)


100
101
102
103
104
105
# 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) ⇒ self

Append arbitrary data as a part

Parameters:

  • upload_io (IO)

    IO stream

  • content_id (String) (defaults to: nil)

    Optional unique ID of this part

Returns:

  • (self)


114
115
116
117
118
119
120
121
122
# 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

#assembleIO

Assemble the multipart requests

Returns:

  • (IO)

    IO stream



128
129
130
131
# 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