Class: Line::Message::Builder::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/line/message/builder/container.rb

Overview

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:

  • Base
  • Text
  • Flex::Builder
  • QuickReply

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: nil, mode: :api, &block) ⇒ Container

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 Context object. [mode] The mode to use for building messages. Can be either :api (default) for direct LINE Messaging API format or :sdkv2 for LINE Bot SDK v2 compatible format. [block] A block containing DSL calls to define messages (e.g., text "Hello", flex { ... }).



58
59
60
61
62
63
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

:nodoc:



141
142
143
144
145
# File 'lib/line/message/builder/container.rb', line 141

def method_missing(method_name, ...)
  return context.send(method_name, ...) if context.respond_to?(method_name)

  super
end

Instance Attribute Details

#contextObject (readonly)

[return] The context object, which can hold external data or helper methods accessible within the builder blocks.



40
41
42
# File 'lib/line/message/builder/container.rb', line 40

def context
  @context
end

Instance Method Details

#buildObject

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.



120
121
122
# File 'lib/line/message/builder/container.rb', line 120

def build
  @messages.map(&:to_h)
end

#flex(**options) ⇒ Object

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 Flex Message, primarily :alt_text. See Flex::Builder#initialize. It's important to provide alt_text. [block] A block that will be instance-eval'd in the context of the new Flex::Builder instance. This block is used to define the structure and content of the Flex Message (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


111
112
113
# File 'lib/line/message/builder/container.rb', line 111

def flex(**options, &)
  @messages << Flex::Builder.new(context: context, **options, &)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


136
137
138
# File 'lib/line/message/builder/container.rb', line 136

def respond_to_missing?(method_name, include_private = false)
  context.respond_to?(method_name, include_private) || super
end

#text(text, **options) ⇒ Object

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 Text message 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


84
85
86
# File 'lib/line/message/builder/container.rb', line 84

def text(text, **options, &)
  @messages << Text.new(text, context: context, **options, &)
end

#to_json(*args) ⇒ Object

Converts the array of message hashes (obtained from #build) into a JSON string. This is a convenience method for serializing the messages payload.

[arg] Optional arguments that are passed along to to_json method of the underlying array.



131
132
133
# File 'lib/line/message/builder/container.rb', line 131

def to_json(*args)
  build.to_json(*args)
end