Class: WhatsappSdk::Resource::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp_sdk/resource/component.rb

Defined Under Namespace

Modules: Subtype, Type Classes: InvalidField

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, parameters: [], sub_type: nil, index: nil) ⇒ Component

Returns a new instance of Component.



57
58
59
60
61
62
63
# File 'lib/whatsapp_sdk/resource/component.rb', line 57

def initialize(type:, parameters: [], sub_type: nil, index: nil)
  @parameters = parameters
  @type = type
  @sub_type = sub_type
  @index = index.nil? && type == Type::BUTTON ? 0 : index
  validate_fields
end

Instance Attribute Details

#indexObject

Required when type=button. Not used for the other types. Position index of the button. You can have up to 3 buttons using index values of 0 to 2.



51
52
53
# File 'lib/whatsapp_sdk/resource/component.rb', line 51

def index
  @index
end

#parametersObject

Returns the parameters of the component. For button type, it’s required.



35
36
37
# File 'lib/whatsapp_sdk/resource/component.rb', line 35

def parameters
  @parameters
end

#sub_typeObject

Returns the Type of button to create. Required when type=button. Not used for the other types. Supported Options quick_reply: Refers to a previously created quick reply button that allows for the customer to return a predefined message. url: Refers to a previously created button that allows the customer to visit the URL generated by appending the text parameter to the predefined prefix URL in the template.



45
46
47
# File 'lib/whatsapp_sdk/resource/component.rb', line 45

def sub_type
  @sub_type
end

#typeObject

Returns the Component type.



30
31
32
# File 'lib/whatsapp_sdk/resource/component.rb', line 30

def type
  @type
end

Instance Method Details

#add_parameter(parameter) ⇒ Object



53
54
55
# File 'lib/whatsapp_sdk/resource/component.rb', line 53

def add_parameter(parameter)
  @parameters << parameter
end

#to_jsonObject



65
66
67
68
69
70
71
72
73
# File 'lib/whatsapp_sdk/resource/component.rb', line 65

def to_json
  json = {
    type: type,
    parameters: parameters.map(&:to_json)
  }
  json[:sub_type] = sub_type if sub_type
  json[:index] = index if index
  json
end