Class: SimpleAcp::Models::MessagePart
- Defined in:
- lib/simple_acp/models/message_part.rb
Overview
Individual content unit within a Message.
Message parts can contain text, JSON, images, or URL references. Each part has a content type (MIME type) and either inline content or a URL reference.
Instance Attribute Summary collapse
-
#content ⇒ String?
readonly
Inline content (mutually exclusive with content_url).
-
#content_encoding ⇒ String
readonly
"plain" or "base64".
-
#content_type ⇒ String
readonly
MIME type (e.g., "text/plain", "application/json").
-
#content_url ⇒ String?
readonly
URL reference (mutually exclusive with content).
-
#metadata ⇒ CitationMetadata, ...
readonly
Optional metadata.
-
#name ⇒ String?
readonly
Optional name for the part.
Class Method Summary collapse
-
.from_hash(hash) ⇒ MessagePart?
Create from a hash (JSON deserialization).
-
.from_url(url, content_type:, name: nil) ⇒ MessagePart
Create a message part referencing a URL.
-
.image(data, mime_type: "image/png", name: nil) ⇒ MessagePart
Create an image message part from base64 data.
-
.json(data, name: nil) ⇒ MessagePart
Create a JSON message part.
-
.text(content, name: nil) ⇒ MessagePart
Create a plain text message part.
Instance Method Summary collapse
-
#base64_encoded? ⇒ Boolean
Check if content is base64 encoded.
-
#decoded_content ⇒ String
Get decoded content (decodes base64 if needed).
-
#image? ⇒ Boolean
Check if this is an image content part.
-
#initialize(**kwargs) ⇒ MessagePart
constructor
A new instance of MessagePart.
-
#json? ⇒ Boolean
Check if this is a JSON content part.
-
#parsed_json ⇒ Hash, ...
Parse JSON content into Ruby objects.
-
#text? ⇒ Boolean
Check if this is a text content part.
-
#to_s ⇒ String
Convert to string representation.
-
#url_reference? ⇒ Boolean
Check if this part references a URL.
-
#valid? ⇒ Boolean
Validate the message part.
Methods inherited from Base
#==, attribute, attributes, #hash, own_attributes, #to_h, #to_json
Constructor Details
#initialize(**kwargs) ⇒ MessagePart
Returns a new instance of MessagePart.
49 50 51 52 |
# File 'lib/simple_acp/models/message_part.rb', line 49 def initialize(**kwargs) super validate! end |
Instance Attribute Details
#content ⇒ String? (readonly)
Returns inline content (mutually exclusive with content_url).
35 |
# File 'lib/simple_acp/models/message_part.rb', line 35 attribute :content |
#content_encoding ⇒ String (readonly)
Returns "plain" or "base64".
39 |
# File 'lib/simple_acp/models/message_part.rb', line 39 attribute :content_encoding, default: Types::ContentEncoding::PLAIN |
#content_type ⇒ String (readonly)
Returns MIME type (e.g., "text/plain", "application/json").
31 |
# File 'lib/simple_acp/models/message_part.rb', line 31 attribute :content_type, required: true |
#content_url ⇒ String? (readonly)
Returns URL reference (mutually exclusive with content).
43 |
# File 'lib/simple_acp/models/message_part.rb', line 43 attribute :content_url |
#metadata ⇒ CitationMetadata, ... (readonly)
Returns optional metadata.
47 |
# File 'lib/simple_acp/models/message_part.rb', line 47 attribute :metadata |
#name ⇒ String? (readonly)
Returns optional name for the part.
27 |
# File 'lib/simple_acp/models/message_part.rb', line 27 attribute :name |
Class Method Details
.from_hash(hash) ⇒ MessagePart?
Create from a hash (JSON deserialization).
58 59 60 61 62 63 64 |
# File 'lib/simple_acp/models/message_part.rb', line 58 def self.from_hash(hash) return nil if hash.nil? instance = allocate instance.send(:initialize_from_hash, hash) instance end |
.from_url(url, content_type:, name: nil) ⇒ MessagePart
Create a message part referencing a URL.
113 114 115 116 117 118 119 |
# File 'lib/simple_acp/models/message_part.rb', line 113 def self.from_url(url, content_type:, name: nil) new( content_type: content_type, content_url: url, name: name ) end |
.image(data, mime_type: "image/png", name: nil) ⇒ MessagePart
Create an image message part from base64 data.
98 99 100 101 102 103 104 105 |
# File 'lib/simple_acp/models/message_part.rb', line 98 def self.image(data, mime_type: "image/png", name: nil) new( content_type: mime_type, content: data, content_encoding: Types::ContentEncoding::BASE64, name: name ) end |
.json(data, name: nil) ⇒ MessagePart
Create a JSON message part.
84 85 86 87 88 89 90 |
# File 'lib/simple_acp/models/message_part.rb', line 84 def self.json(data, name: nil) new( content_type: "application/json", content: data.is_a?(String) ? data : data.to_json, name: name ) end |
.text(content, name: nil) ⇒ MessagePart
Create a plain text message part.
71 72 73 74 75 76 77 |
# File 'lib/simple_acp/models/message_part.rb', line 71 def self.text(content, name: nil) new( content_type: "text/plain", content: content, name: name ) end |
Instance Method Details
#base64_encoded? ⇒ Boolean
Check if content is base64 encoded.
145 146 147 |
# File 'lib/simple_acp/models/message_part.rb', line 145 def base64_encoded? @content_encoding == Types::ContentEncoding::BASE64 end |
#decoded_content ⇒ String
Get decoded content (decodes base64 if needed).
159 160 161 162 163 |
# File 'lib/simple_acp/models/message_part.rb', line 159 def decoded_content return @content unless base64_encoded? Base64.decode64(@content) end |
#image? ⇒ Boolean
Check if this is an image content part.
138 139 140 |
# File 'lib/simple_acp/models/message_part.rb', line 138 def image? @content_type&.start_with?("image/") end |
#json? ⇒ Boolean
Check if this is a JSON content part.
131 132 133 |
# File 'lib/simple_acp/models/message_part.rb', line 131 def json? @content_type == "application/json" end |
#parsed_json ⇒ Hash, ...
Parse JSON content into Ruby objects.
168 169 170 171 172 |
# File 'lib/simple_acp/models/message_part.rb', line 168 def parsed_json return nil unless json? JSON.parse(@content) end |
#text? ⇒ Boolean
Check if this is a text content part.
124 125 126 |
# File 'lib/simple_acp/models/message_part.rb', line 124 def text? @content_type&.start_with?("text/") end |
#to_s ⇒ String
Convert to string representation.
188 189 190 191 192 193 |
# File 'lib/simple_acp/models/message_part.rb', line 188 def to_s return @content if text? return @content_url if url_reference? "<#{@content_type}>" end |
#url_reference? ⇒ Boolean
Check if this part references a URL.
152 153 154 |
# File 'lib/simple_acp/models/message_part.rb', line 152 def url_reference? !@content_url.nil? end |
#valid? ⇒ Boolean
Validate the message part.
177 178 179 180 181 182 183 |
# File 'lib/simple_acp/models/message_part.rb', line 177 def valid? return false if @content_type.nil? return false if @content.nil? && @content_url.nil? return false if @content && @content_url true end |