Class: Whatsapp::MessageTemplates::Button::Url

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby/whatsapp/message_templates/button/url.rb

Overview

Opens a URL in the device's default browser.

May carry a single variable, and only appended at the very end of the URL — Meta substitutes the send-side value there. Note the example here is a flat array on the button, unlike the header/body example object built by Example; that inconsistency is Meta's. Source: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/components/

Defined Under Namespace

Modules: Defaults

Constant Summary collapse

TRAILING_VARIABLE =

Matches a placeholder occupying the very end of the URL.

/\{\{\s*\w+\s*\}\}\z/

Constants inherited from Base

Base::MAX_TEXT_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#api_type

Methods included from ValueObject

included

Constructor Details

#initialize(text:, url:, example: nil) ⇒ Url

@raise [ActiveModel::ValidationError] if validation fails.

Parameters:

  • text (String)

    The button label (max 25 characters).

  • url (String)

    The URL (max 2000 characters), optionally ending in a single {{1}} or {{name}} placeholder.

  • example (String, Array<String>, nil) (defaults to: nil)

    A sample value for the placeholder. Required when the URL has one.



46
47
48
49
50
51
52
# File 'lib/ruby/whatsapp/message_templates/button/url.rb', line 46

def initialize(text:, url:, example: nil)
  @text = text
  @url = url
  @example = normalize_example(example)

  validate!
end

Instance Attribute Details

#exampleArray<String>?

Returns:

  • (Array<String>, nil)


33
34
35
# File 'lib/ruby/whatsapp/message_templates/button/url.rb', line 33

def example
  @example
end

#textString

Returns:

  • (String)


25
26
27
# File 'lib/ruby/whatsapp/message_templates/button/url.rb', line 25

def text
  @text
end

#urlString

Returns:

  • (String)


29
30
31
# File 'lib/ruby/whatsapp/message_templates/button/url.rb', line 29

def url
  @url
end

Instance Method Details

#serializeHash

Returns The serialized button.

Returns:

  • (Hash)

    The serialized button.



55
56
57
58
59
60
61
62
# File 'lib/ruby/whatsapp/message_templates/button/url.rb', line 55

def serialize
  {
    type: Defaults::TYPE,
    text:,
    url:,
    example:,
  }.compact
end