class Line::Message::Builder::Base
The Base class serves as the foundation for all message builder classes within the Line::Message::Builder DSL. It provides core functionality for defining options, handling initialization, and delegating method calls to a context object.
This class is not typically used directly but is inherited by specific message type builders (e.g., Text, Flex::Builder).
Attributes
The context object used for method delegation.
When methods are called on the builder that it doesn’t respond to, they are delegated to this context object if it responds to them. This allows accessing helper methods and variables from the surrounding environment within the builder DSL.
Public Class Methods
Source
# File lib/line/message/builder/base.rb, line 100 def initialize(context: nil, **options, &block) @context = context @quick_reply = nil self.class.options.each do |option| send(option, options[option]) if options.key?(option) end instance_eval(&block) if ::Kernel.block_given? end
Initializes a new instance of a builder class.
- context
-
An optional external context object. Methods not defined in the builder will be delegated to this context if it responds to them. This allows for using helper methods or accessing data from the surrounding environment within the builder DSL.
- options
-
A hash of options to set on the builder instance. These options are typically defined using the
optionmethod in the builder class. - block
-
An optional block that is instance-eval’d within the new builder instance. This is the primary way the DSL is used to define message content.
Example
builder = MyBuilder.new(context: view_context) do text "Hello from context" end
Public Instance Methods
Source
# File lib/line/message/builder/base.rb, line 130 def quick_reply(&) @quick_reply = QuickReply.new(context: context, &) end
Defines a quick reply for the message.
A quick reply consists of a set of buttons that are displayed along with the message, allowing users to make quick responses.
The provided block is executed in the context of a new QuickReply instance, where you can define the quick reply buttons.
Example
text_message do text "Please choose an option:" quick_reply do button action: :message, label: "Option A", text: "You chose A" button action: :camera, label: "Open Camera" end end
Source
# File lib/line/message/builder/base.rb, line 160 def to_h return to_sdkv2 if sdkv2? to_api end
Converts the builder to a hash representation.
The format of the hash depends on the mode (API or SDK v2) determined by the builder’s configuration. Subclasses must implement to_api and to_sdkv2 methods to provide the actual conversion logic.
- return
-
A hash representation of the message in the appropriate format.
Example
builder = Text.new { text "Hello" } builder.to_h # => { type: "text", text: "Hello" }