class Line::Message::Builder::Container
The Container class is the top-level entry point for constructing a batch of LINE messages using the builder DSL. It acts as a holder for one or more individual message objects (such as Text or Flex messages).
When you use Line::Message::Builder.with {}, you are operating within the context of a Container instance. This container allows you to define multiple messages that can be sent together in a single API call to LINE, although the LINE API typically expects an array of message objects, which this container helps to build.
Each message added to the container can also have its own quick reply.
Example
message_payload = Line::Message::Builder.with do text "Hello, this is the first message!" flex alt_text: "This is a Flex Message" do bubble do body do body.text "This is a Flex Message body." end end end end.build # => Returns an array of message hashes
See also:
Attributes
- return
-
The context object, which can hold external data or helper methods accessible within the builder blocks.
Public Class Methods
Source
# File lib/line/message/builder/container.rb, line 58 def initialize(context: nil, mode: :api, &block) @messages = [] # Initializes an empty array to store message objects @context = Context.new(context, mode:) instance_eval(&block) if ::Kernel.block_given? end
Initializes a new message container. This is typically not called directly but through Line::Message::Builder.with. The provided block is instance-eval’d, allowing DSL methods like text and flex to be called directly on the container instance.
- context
-
An optional context object that can be used to share data or helper methods within the builder block. It’s wrapped in a
Contextobject. - mode
-
The mode to use for building messages. Can be either
:api(default) for direct LINE Messaging API format or:sdkv2for LINE Bot SDK v2 compatible format. - block
-
A block containing DSL calls to define messages (e.g.,
text "Hello",flex { ... }).
Public Instance Methods
Source
# File lib/line/message/builder/container.rb, line 120 def build @messages.map(&:to_h) end
Converts all messages held by this container into their hash representations. This method iterates over each message object (e.g., Text, Flex::Builder) stored in the container and calls its to_h method. The result is an array of hashes, where each hash represents a single LINE message object ready for JSON serialization and sending to the LINE Messaging API.
Source
# File lib/line/message/builder/container.rb, line 111 def flex(**options, &) @messages << Flex::Builder.new(context: context, **options, &) end
Creates a new Flex::Builder for constructing a Flex Message and adds it to this container. The block is mandatory and is used to define the content of the Flex Message using the Flex Message DSL.
- option
-
Options for the
FlexMessage, primarily:alt_text. See Flex::Builder#initialize. It’s important to providealt_text. - block
-
A block that will be instance-eval’d in the context of the new
Flex::Builderinstance. This block is used to define the structure and content of theFlexMessage(e.g., bubbles, carousels).
Raises ArgumentError if alt_text is not provided in options (validation is typically within Flex::Builder).
Example
flex alt_text: "Important information" do bubble do header { text "Header" } body { text "Body" } end end
Source
# File lib/line/message/builder/container.rb, line 84 def text(text, **options, &) @messages << Text.new(text, context: context, **options, &) end
Creates a new Text message and adds it to this container.
- text
-
The text content of the message.
- option
-
Additional options for the text message, such as
:quick_reply. See Text#initialize. - block
-
An optional block that will be instance-eval’d in the context of the new
Textmessage instance. This can be used to add a quick reply to the text message.
Example
root.text "Hello, world!" do quick_reply do button action: :message, label: "Hi!", text: "Hi!" end end
Source
# File lib/line/message/builder/container.rb, line 131 def to_json(*args) build.to_json(*args) end