Class: Line::Message::Builder::Flex::Carousel

Inherits:
Base
  • Object
show all
Includes:
HasPartial
Defined in:
lib/line/message/builder/flex/carousel.rb

Overview

Represents a carousel container in a LINE Flex Message. A carousel is a horizontally scrollable sequence of Bubble components. Each bubble in the carousel is a distinct message unit. Users can swipe left or right to view the different bubbles.

Carousels are ideal for presenting multiple items, such as products, articles, or options, in a compact and interactive way.

Example

Line::Message::Builder.with do
flex alt_text: "Product Showcase" do
  carousel do
    bubble size: :mega do
      hero_image "https://example.com/product1.jpg"
      body { text "Product 1" }
    end
    bubble size: :mega do
      hero_image "https://example.com/product2.jpg"
      body { text "Product 2" }
    end
  end
end
end

See also:

Instance Attribute Summary collapse

Attributes inherited from Base

#context

Instance Method Summary collapse

Methods included from HasPartial

#partial!

Methods inherited from Base

inherited, #method_missing, #quick_reply, #respond_to_missing?

Constructor Details

#initialize(context: nil, **options) ⇒ Carousel

Initializes a new Flex Message Carousel container. The provided block is instance-eval'd, allowing DSL methods like bubble to be called to add bubbles to the carousel.

[context] An optional context for the builder [options] A hash of options (currently none specific to Carousel itself, but available for future extensions or via Base) [block] A block to define the bubbles within this carousel

Example

carousel = Carousel.new do
bubble { body { text "First" } }
bubble { body { text "Second" } }
end


61
62
63
64
65
# File 'lib/line/message/builder/flex/carousel.rb', line 61

def initialize(context: nil, **options, &)
  @contents = [] # Holds an array of Bubble objects

  super # Calls Base#initialize, sets options, and evals block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Line::Message::Builder::Base

Instance Attribute Details

#contentsObject (readonly)

An array holding the Bubble components that form the items of this carousel.



41
42
43
# File 'lib/line/message/builder/flex/carousel.rb', line 41

def contents
  @contents
end

Instance Method Details

#bubble(**options) ⇒ Object

Adds a new Bubble to this carousel's contents. Each call to this method appends another bubble to the horizontal sequence.

[options] Options for the Bubble (see Bubble#initialize) [block] A block to define the sections and content of the Bubble

[return] The newly created Bubble object that was added to the carousel

Example

carousel do
bubble size: :mega do
  body { text "Product 1" }
end
bubble size: :mega do
  body { text "Product 2" }
end
end


88
89
90
91
# File 'lib/line/message/builder/flex/carousel.rb', line 88

def bubble(**options, &)
  # The maximum number of bubbles is validated in `to_h` as per LINE API limits.
  @contents << Line::Message::Builder::Flex::Bubble.new(context: context, **options, &)
end

#to_hObject

:nodoc:

Raises:



94
95
96
97
98
99
100
101
102
# File 'lib/line/message/builder/flex/carousel.rb', line 94

def to_h
  raise RequiredError, "Carousel contents must have at least 1 bubble." if @contents.empty?
  # LINE API as of 2023-10-10 allows up to 12 bubbles in a carousel.
  raise ValidationError, "Carousel contents can have at most 12 bubbles." if @contents.size > 12

  return to_sdkv2 if context.sdkv2?

  to_api
end