Class: Google::Apis::Core::Multipart
- Inherits:
-
Object
- Object
- Google::Apis::Core::Multipart
- Defined in:
- lib/google/apis/core/multipart.rb
Overview
Helper for building multipart requests
Constant Summary collapse
- MULTIPART_RELATED =
'multipart/related'
Instance Attribute Summary collapse
-
#content_type ⇒ String
readonly
Content type header.
Instance Method Summary collapse
-
#add_json(body, content_id: nil) ⇒ self
Append JSON data part.
-
#add_upload(upload_io, content_type: nil, content_id: nil) ⇒ self
Append arbitrary data as a part.
-
#assemble ⇒ IO
Assemble the multipart requests.
-
#initialize(content_type: MULTIPART_RELATED, boundary: nil) ⇒ Multipart
constructor
A new instance of Multipart.
Constructor Details
#initialize(content_type: MULTIPART_RELATED, boundary: nil) ⇒ Multipart
Returns a new instance of Multipart.
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_type ⇒ String (readonly)
Returns 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
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
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 |
#assemble ⇒ IO
Assemble the multipart requests
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 |