Class: GroqRuby::Multipart

Inherits:
Object
  • Object
show all
Defined in:
lib/groq_ruby/multipart.rb

Overview

Encodes a list of fields into a multipart/form-data body. Used by the file upload endpoints (audio transcription/translation, files.create). Single-purpose — builds the body and content-type header. Does no IO.

Examples:

parts = [
  Multipart::Part.new("model", "whisper-large-v3"),
  Multipart::Part.file("file", io: file_io, filename: "audio.mp3", content_type: "audio/mpeg")
]
multipart = Multipart.new(parts)
request_body = multipart.body
header_value = multipart.content_type

Defined Under Namespace

Classes: Part

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts, boundary: SecureRandom.hex(16)) ⇒ Multipart

Returns a new instance of Multipart.

Parameters:

  • parts (Array<Part>)

    the fields to encode

  • boundary (String) (defaults to: SecureRandom.hex(16))

    override the auto-generated boundary



48
49
50
51
# File 'lib/groq_ruby/multipart.rb', line 48

def initialize(parts, boundary: SecureRandom.hex(16))
  @parts = parts
  @boundary = boundary
end

Instance Attribute Details

#boundaryString (readonly)

Returns the random boundary string used to separate parts.

Returns:

  • (String)

    the random boundary string used to separate parts



44
45
46
# File 'lib/groq_ruby/multipart.rb', line 44

def boundary
  @boundary
end

Instance Method Details

#bodyString

Returns the encoded multipart body, suitable for ‘Net::HTTP::Post#body=`.

Returns:

  • (String)

    the encoded multipart body, suitable for ‘Net::HTTP::Post#body=`



54
55
56
57
58
59
# File 'lib/groq_ruby/multipart.rb', line 54

def body
  buffer = +""
  @parts.each { |p| buffer << encode_part(p) }
  buffer << "--#{@boundary}--\r\n"
  buffer
end

#content_typeString

Returns the value to use for the ‘Content-Type` header.

Returns:

  • (String)

    the value to use for the ‘Content-Type` header



62
63
64
# File 'lib/groq_ruby/multipart.rb', line 62

def content_type
  "multipart/form-data; boundary=#{@boundary}"
end