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'
DEFAULT_BOUNDARY =
'RubyApiClientMultiPart'

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



136
137
138
139
140
# File 'lib/google/apis/core/multipart.rb', line 136

def initialize(content_type: MULTIPART_RELATED, boundary: nil)
  @parts = []
  @boundary = boundary || DEFAULT_BOUNDARY
  @content_type = "#{content_type}; boundary=#{boundary}"
end

Instance Attribute Details

#content_typeString (readonly)

Returns Content type header.

Returns:

  • (String)

    Content type header



129
130
131
# File 'lib/google/apis/core/multipart.rb', line 129

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)


149
150
151
152
153
# File 'lib/google/apis/core/multipart.rb', line 149

def add_json(body, content_id: nil)
  header = { :content_id => content_id }
  @parts << Google::Apis::Core::JsonPart.new(@boundary, body, header)
  self
end

#add_upload(upload_io, content_id: nil) ⇒ self

Append arbitrary data as a part

Parameters:

Returns:

  • (self)


162
163
164
165
166
167
168
# File 'lib/google/apis/core/multipart.rb', line 162

def add_upload(upload_io, content_id: nil)
  header = { :content_id => content_id }
  @parts << Google::Apis::Core::FilePart.new(@boundary,
                                             upload_io,
                                             header)
  self
end

#assembleIO

Assemble the multipart requests

Returns:

  • (IO)

    IO stream



174
175
176
177
178
179
180
181
182
183
# File 'lib/google/apis/core/multipart.rb', line 174

def assemble
  @parts << Hurley::Multipart::EpiloguePart.new(@boundary)
  ios = []
  len = 0
  @parts.each do |part|
    len += part.length
    ios << part.to_io
  end
  Hurley::CompositeReadIO.new(len, *ios)
end