Class: SimpleAcp::Models::MessagePart

Inherits:
Base
  • Object
show all
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.

Examples:

Text part

part = MessagePart.text("Hello, world!")

JSON part

part = MessagePart.json({ key: "value" })

Image from base64

part = MessagePart.image(base64_data, mime_type: "image/png")

URL reference

part = MessagePart.from_url("https://example.com/image.png", content_type: "image/png")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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).

Returns:

  • (String, nil) —

    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".

Returns:

  • (String) —

    "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").

Returns:

  • (String) —

    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).

Returns:

  • (String, nil) —

    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.

Returns:



47
# File 'lib/simple_acp/models/message_part.rb', line 47

attribute :metadata

#name ⇒ String? (readonly)

Returns optional name for the part.

Returns:

  • (String, nil) —

    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).

Parameters:

  • hash (Hash, nil) —

    part data

Returns:



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.

Parameters:

  • url (String) —

    the URL to reference

  • content_type (String) —

    the content type at the URL

  • name (String, nil) (defaults to: nil) —

    optional name

Returns:



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.

Parameters:

  • data (String) —

    base64-encoded image data

  • mime_type (String) (defaults to: "image/png") —

    image MIME type (default: "image/png")

  • name (String, nil) (defaults to: nil) —

    optional name

Returns:



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.

Parameters:

  • data (Hash, Array, String) —

    JSON data (will be serialized if not string)

  • name (String, nil) (defaults to: nil) —

    optional name

Returns:



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.

Parameters:

  • content (String) —

    the text content

  • name (String, nil) (defaults to: nil) —

    optional name

Returns:



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.

Returns:

  • (Boolean) —

    true if content_encoding is "base64"



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).

Returns:

  • (String) —

    decoded content



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.

Returns:

  • (Boolean) —

    true if content_type starts with "image/"



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.

Returns:

  • (Boolean) —

    true if content_type is "application/json"



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.

Returns:

  • (Hash, Array, nil) —

    parsed JSON or nil if not JSON



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.

Returns:

  • (Boolean) —

    true if content_type starts with "text/"



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.

Returns:

  • (String) —

    content for text, URL for references, or "" placeholder



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.

Returns:

  • (Boolean) —

    true if content_url is set



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.

Returns:

  • (Boolean) —

    true if content_type is set and has content or URL (not both)



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