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.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/openai/file_part.rb', line 118 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, _] strip_directories(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, ...
55 56 57 |
# File 'lib/openai/file_part.rb', line 55 def content @content end |
#content_type ⇒ String?
59 60 61 |
# File 'lib/openai/file_part.rb', line 59 def content_type OpenAI::FilePart.validate_content_type(@content_type) end |
#filename ⇒ String? (readonly)
64 65 66 |
# File 'lib/openai/file_part.rb', line 64 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.
47 48 49 50 51 52 |
# File 'lib/openai/file_part.rb', line 47 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
107 |
# File 'lib/openai/file_part.rb', line 107 def to_json(*a) = read.to_json(*a) |
#to_yaml(*a) ⇒ String
112 |
# File 'lib/openai/file_part.rb', line 112 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.
72 73 74 75 76 77 |
# File 'lib/openai/file_part.rb', line 72 def with_content(content) dup.tap do |copy| copy.content = content copy.content_type = content_type || default_content_type end end |