Class: OpenAI::FilePart
- Inherits:
-
Object
- Object
- OpenAI::FilePart
- Defined in:
- lib/openai/file_part.rb,
sig/openai/file_part.rbs
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(content, filename: nil, content_type: nil) ⇒ FilePart
constructor
A new instance of FilePart.
- #to_json(*a) ⇒ String
- #to_yaml(*a) ⇒ String
-
#with_content(content) ⇒ OpenAI::FilePart
private
Return a copy with different content while preserving multipart behavior.
Constructor Details
#initialize(content, filename: nil, content_type: nil) ⇒ FilePart
Returns a new instance of FilePart.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/openai/file_part.rb', line 110 def initialize(content, filename: nil, content_type: nil) OpenAI::FilePart.validate_content_type(content_type) @content_type = content_type @filename = case [filename, (@content = content)] in [String | Pathname, _] ::File.basename(filename) in [nil, Pathname] content.basename.to_path in [nil, IO] content.to_path&.then { ::File.basename(_1) } else filename end end |
Instance Attribute Details
#content ⇒ Pathname, ...
47 48 49 |
# File 'lib/openai/file_part.rb', line 47 def content @content end |
#content_type ⇒ String?
51 52 53 |
# File 'lib/openai/file_part.rb', line 51 def content_type OpenAI::FilePart.validate_content_type(@content_type) end |
#filename ⇒ String? (readonly)
56 57 58 |
# File 'lib/openai/file_part.rb', line 56 def filename @filename end |
Class Method Details
.validate_content_type(content_type) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
39 40 41 42 43 44 |
# File 'lib/openai/file_part.rb', line 39 def self.validate_content_type(content_type) return if content_type.nil? return content_type if content_type.is_a?(String) && MEDIA_TYPE.match?(content_type.b) raise ArgumentError, "`content_type` must be a valid MIME media type" end |
Instance Method Details
#to_json(*a) ⇒ String
99 |
# File 'lib/openai/file_part.rb', line 99 def to_json(*a) = read.to_json(*a) |
#to_yaml(*a) ⇒ String
104 |
# File 'lib/openai/file_part.rb', line 104 def to_yaml(*a) = read.to_yaml(*a) |
#with_content(content) ⇒ OpenAI::FilePart
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a copy with different content while preserving multipart behavior.
64 65 66 67 68 69 |
# File 'lib/openai/file_part.rb', line 64 def with_content(content) dup.tap do |copy| copy.content = content copy.content_type = content_type || default_content_type end end |