Class: MessengerClient::ListTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/messenger_client/list_template.rb

Constant Summary collapse

TEMPLATE_STYLES =
%w(LARGE COMPACT)

Instance Method Summary collapse

Methods inherited from Template

#to_json

Constructor Details

#initialize(template_items, buttons = [], style = "LARGE") ⇒ ListTemplate

Returns a new instance of ListTemplate.



5
6
7
8
9
# File 'lib/messenger_client/list_template.rb', line 5

def initialize(template_items, buttons = [], style = "LARGE")
  @template_items = template_items
  @buttons        = buttons
  @style          = style.upcase
end

Instance Method Details

#payloadObject

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/messenger_client/list_template.rb', line 15

def payload
  data = {
    template_type: type,
    elements: @template_items.map(&:to_json),
  }

  raise ArgumentError, "#{@style} is not a valid template style. Your choices are #{TEMPLATE_STYLES.join(', ')}" unless TEMPLATE_STYLES.include?(@style)
  data.merge!(top_element_style: @style)

  data.merge!(buttons: @buttons.map(&:to_json)) if @buttons.any?
  data
end

#typeObject



11
12
13
# File 'lib/messenger_client/list_template.rb', line 11

def type
  "list"
end