Class: Riffer::FilePart
- Inherits:
-
Object
- Object
- Riffer::FilePart
- Defined in:
- lib/riffer/file_part.rb
Overview
Represents a file attachment (image or document) in a conversation.
Supports two input sources:
-
URLs (stored and passed to providers that support them via
from_url) -
Raw base64 data (via
new)file = Riffer::FilePart.from_url(“example.com/doc.pdf”, media_type: “application/pdf”) file.url? # => true file.document? # => true
Constant Summary collapse
- MEDIA_TYPES =
{ ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png", ".gif" => "image/gif", ".webp" => "image/webp", ".pdf" => "application/pdf", ".txt" => "text/plain", ".md" => "text/plain", ".csv" => "text/csv", ".html" => "text/html" }.freeze
- SUPPORTED_MEDIA_TYPES =
: Hash[String, String]
MEDIA_TYPES.values.uniq.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the base64-encoded data, or nil for URL-only sources.
-
#filename ⇒ Object
readonly
The filename, if available.
-
#media_type ⇒ Object
readonly
The MIME type of the file.
Class Method Summary collapse
-
.from_url(url, media_type: nil) ⇒ Object
Creates a FilePart from a URL.
Instance Method Summary collapse
-
#document? ⇒ Boolean
Returns true if the file is a document (not an image).
-
#image? ⇒ Boolean
Returns true if the file is an image.
-
#initialize(media_type:, data: nil, filename: nil, url: nil) ⇒ FilePart
constructor
Creates a FilePart from raw base64 data or a URL.
-
#to_h ⇒ Object
Serializes the FilePart to a hash.
-
#url ⇒ Object
Returns the URL if the source was a URL, nil otherwise.
-
#url? ⇒ Boolean
Returns true if the source was a URL.
Constructor Details
#initialize(media_type:, data: nil, filename: nil, url: nil) ⇒ FilePart
Creates a FilePart from raw base64 data or a URL.
At least one of data or url must be provided.
Raises Riffer::ArgumentError if neither data nor url is provided, or if media_type is not supported.
– : (media_type: String, ?data: String?, ?filename: String?, ?url: String?) -> void
48 49 50 51 52 53 54 55 56 |
# File 'lib/riffer/file_part.rb', line 48 def initialize(media_type:, data: nil, filename: nil, url: nil) raise Riffer::ArgumentError, "Either data or url must be provided" if data.nil? && url.nil? raise Riffer::ArgumentError, "Unsupported media type: #{media_type}" unless SUPPORTED_MEDIA_TYPES.include?(media_type) @data = data @media_type = media_type @filename = filename @url_string = url end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the base64-encoded data, or nil for URL-only sources.
78 79 80 |
# File 'lib/riffer/file_part.rb', line 78 def data @data end |
#filename ⇒ Object (readonly)
The filename, if available.
37 38 39 |
# File 'lib/riffer/file_part.rb', line 37 def filename @filename end |
#media_type ⇒ Object (readonly)
The MIME type of the file.
34 35 36 |
# File 'lib/riffer/file_part.rb', line 34 def media_type @media_type end |
Class Method Details
.from_url(url, media_type: nil) ⇒ Object
Creates a FilePart from a URL.
The URL is stored and passed directly to providers that support URL sources. If media_type is not provided, it is detected from the URL path extension.
Raises Riffer::ArgumentError if media_type cannot be detected.
– : (String, ?media_type: String?) -> Riffer::FilePart
67 68 69 70 71 72 73 74 75 |
# File 'lib/riffer/file_part.rb', line 67 def self.from_url(url, media_type: nil) unless media_type ext = ::File.extname(URI.parse(url).path).downcase media_type = MEDIA_TYPES[ext] raise Riffer::ArgumentError, "Cannot detect media type from URL; provide media_type explicitly" unless media_type end new(url: url, media_type: media_type) end |
Instance Method Details
#document? ⇒ Boolean
Returns true if the file is a document (not an image).
– : () -> bool
108 109 110 |
# File 'lib/riffer/file_part.rb', line 108 def document? !image? end |
#image? ⇒ Boolean
Returns true if the file is an image.
– : () -> bool
100 101 102 |
# File 'lib/riffer/file_part.rb', line 100 def image? media_type.start_with?("image/") end |
#to_h ⇒ Object
Serializes the FilePart to a hash.
– : () -> Hash[Symbol, untyped]
116 117 118 119 120 121 122 |
# File 'lib/riffer/file_part.rb', line 116 def to_h hash = {media_type: media_type} hash[:data] = @data if @data hash[:url] = @url_string if @url_string hash[:filename] = filename if filename hash end |
#url ⇒ Object
Returns the URL if the source was a URL, nil otherwise.
– : () -> String?
84 85 86 |
# File 'lib/riffer/file_part.rb', line 84 def url @url_string end |
#url? ⇒ Boolean
Returns true if the source was a URL.
– : () -> bool
92 93 94 |
# File 'lib/riffer/file_part.rb', line 92 def url? !@url_string.nil? end |