Class: Courier::Send::TemplateMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/trycourier/send/types/template_message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template:, to:, data: nil, brand_id: nil, channels: nil, context: nil, metadata: nil, providers: nil, routing: nil, timeout: nil, delay: nil, expiry: nil, additional_properties: nil) ⇒ Send::TemplateMessage

Parameters:

  • template (String)

    The id of the notification template to be rendered and sent to the recipient(s). This field or the content field must be supplied.

  • to (Send::MessageRecipient)

    The recipient or a list of recipients of the message

  • data (Send::MESSAGE_DATA) (defaults to: nil)

    An arbitrary object that includes any data you want to pass to the message. The data will populate the corresponding template or elements variables.

  • brand_id (String) (defaults to: nil)
  • channels (Send::MESSAGE_CHANNELS) (defaults to: nil)

    “Define run-time configuration for one or more channels. If you don’t specify channels, the default configuration for each channel will be used. Valid ChannelId’s are: email, sms, push, inbox, direct_message, banner, and webhook.”

  • context (Send::MessageContext) (defaults to: nil)

    Context to load with this recipient. Will override any context set on message.context.

  • metadata (Send::MessageMetadata) (defaults to: nil)

    Metadata such as utm tracking attached with the notification through this channel.

  • providers (Send::MESSAGE_PROVIDERS) (defaults to: nil)

    An object whose keys are valid provider identifiers which map to an object.

  • routing (Send::Routing) (defaults to: nil)
  • timeout (Send::Timeout) (defaults to: nil)

    Time in ms to attempt the channel before failing over to the next available channel.

  • delay (Send::Delay) (defaults to: nil)

    Defines the time to wait before delivering the message.

  • expiry (Send::Expiry) (defaults to: nil)

    “Expiry allows you to set an absolute or relative time in which a message expires. Note: This is only valid for the Courier Inbox channel as of 12-08-2022.”

  • additional_properties (OpenStruct) (defaults to: nil)

    Additional properties unmapped to the current class definition



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/trycourier/send/types/template_message.rb', line 38

def initialize(template:, to:, data: nil, brand_id: nil, channels: nil, context: nil, metadata: nil,
               providers: nil, routing: nil, timeout: nil, delay: nil, expiry: nil, additional_properties: nil)
  # @type [String] The id of the notification template to be rendered and sent to the recipient(s).
  #   This field or the content field must be supplied.
  @template = template
  # @type [Send::MessageRecipient] The recipient or a list of recipients of the message
  @to = to
  # @type [Send::MESSAGE_DATA] An arbitrary object that includes any data you want to pass to the message.
  #   The data will populate the corresponding template or elements variables.
  @data = data
  # @type [String]
  @brand_id = brand_id
  # @type [Send::MESSAGE_CHANNELS] "Define run-time configuration for one or more channels. If you don't specify channels, the default configuration for each channel will be used. Valid ChannelId's are: email, sms, push, inbox, direct_message, banner, and webhook."
  @channels = channels
  # @type [Send::MessageContext] Context to load with this recipient. Will override any context set on message.context.
  @context = context
  # @type [Send::MessageMetadata] Metadata such as utm tracking attached with the notification through this channel.
  @metadata = 
  # @type [Send::MESSAGE_PROVIDERS] An object whose keys are valid provider identifiers which map to an object.
  @providers = providers
  # @type [Send::Routing]
  @routing = routing
  # @type [Send::Timeout] Time in ms to attempt the channel before failing over to the next available channel.
  @timeout = timeout
  # @type [Send::Delay] Defines the time to wait before delivering the message.
  @delay = delay
  # @type [Send::Expiry] "Expiry allows you to set an absolute or relative time in which a message expires.
  #   Note: This is only valid for the Courier Inbox channel as of 12-08-2022."
  @expiry = expiry
  # @type [OpenStruct] Additional properties unmapped to the current class definition
  @additional_properties = additional_properties
end

Instance Attribute Details

#additional_propertiesObject (readonly)

Returns the value of attribute additional_properties.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def additional_properties
  @additional_properties
end

#brand_idObject (readonly)

Returns the value of attribute brand_id.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def brand_id
  @brand_id
end

#channelsObject (readonly)

Returns the value of attribute channels.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def channels
  @channels
end

#contextObject (readonly)

Returns the value of attribute context.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def context
  @context
end

#dataObject (readonly)

Returns the value of attribute data.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def data
  @data
end

#delayObject (readonly)

Returns the value of attribute delay.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def delay
  @delay
end

#expiryObject (readonly)

Returns the value of attribute expiry.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def expiry
  @expiry
end

#metadataObject (readonly)

Returns the value of attribute metadata.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def 
  @metadata
end

#providersObject (readonly)

Returns the value of attribute providers.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def providers
  @providers
end

#routingObject (readonly)

Returns the value of attribute routing.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def routing
  @routing
end

#templateObject (readonly)

Returns the value of attribute template.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def template
  @template
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def timeout
  @timeout
end

#toObject (readonly)

Returns the value of attribute to.



18
19
20
# File 'lib/trycourier/send/types/template_message.rb', line 18

def to
  @to
end

Class Method Details

.from_json(json_object:) ⇒ Send::TemplateMessage

Deserialize a JSON object to an instance of TemplateMessage

Parameters:

  • json_object (JSON)

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/trycourier/send/types/template_message.rb', line 75

def self.from_json(json_object:)
  struct = JSON.parse(json_object, object_class: OpenStruct)
  parsed_json = JSON.parse(json_object)
  template = struct.template
  if parsed_json["to"].nil?
    to = nil
  else
    to = parsed_json["to"].to_json
    to = Send::MessageRecipient.from_json(json_object: to)
  end
  data = struct.data
  brand_id = struct.brand_id
  channels = struct.channels
  if parsed_json["context"].nil?
    context = nil
  else
    context = parsed_json["context"].to_json
    context = Send::MessageContext.from_json(json_object: context)
  end
  if parsed_json["metadata"].nil?
     = nil
  else
     = parsed_json["metadata"].to_json
     = Send::MessageMetadata.from_json(json_object: )
  end
  providers = struct.providers
  if parsed_json["routing"].nil?
    routing = nil
  else
    routing = parsed_json["routing"].to_json
    routing = Send::Routing.from_json(json_object: routing)
  end
  if parsed_json["timeout"].nil?
    timeout = nil
  else
    timeout = parsed_json["timeout"].to_json
    timeout = Send::Timeout.from_json(json_object: timeout)
  end
  if parsed_json["delay"].nil?
    delay = nil
  else
    delay = parsed_json["delay"].to_json
    delay = Send::Delay.from_json(json_object: delay)
  end
  if parsed_json["expiry"].nil?
    expiry = nil
  else
    expiry = parsed_json["expiry"].to_json
    expiry = Send::Expiry.from_json(json_object: expiry)
  end
  new(template: template, to: to, data: data, brand_id: brand_id, channels: channels, context: context,
      metadata: , providers: providers, routing: routing, timeout: timeout, delay: delay, expiry: expiry, additional_properties: struct)
end

.validate_raw(obj:) ⇒ Void

Leveraged for Union-type generation, validate_raw attempts to parse the given hash and check each fields type against the current object’s property definitions.

Parameters:

  • obj (Object)

Returns:

  • (Void)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/trycourier/send/types/template_message.rb', line 153

def self.validate_raw(obj:)
  obj.template.is_a?(String) != false || raise("Passed value for field obj.template is not the expected type, validation failed.")
  Send::MessageRecipient.validate_raw(obj: obj.to)
  obj.data&.is_a?(Hash) != false || raise("Passed value for field obj.data is not the expected type, validation failed.")
  obj.brand_id&.is_a?(String) != false || raise("Passed value for field obj.brand_id is not the expected type, validation failed.")
  obj.channels&.is_a?(Hash) != false || raise("Passed value for field obj.channels is not the expected type, validation failed.")
  obj.context.nil? || Send::MessageContext.validate_raw(obj: obj.context)
  obj..nil? || Send::MessageMetadata.validate_raw(obj: obj.)
  obj.providers&.is_a?(Hash) != false || raise("Passed value for field obj.providers is not the expected type, validation failed.")
  obj.routing.nil? || Send::Routing.validate_raw(obj: obj.routing)
  obj.timeout.nil? || Send::Timeout.validate_raw(obj: obj.timeout)
  obj.delay.nil? || Send::Delay.validate_raw(obj: obj.delay)
  obj.expiry.nil? || Send::Expiry.validate_raw(obj: obj.expiry)
end

Instance Method Details

#to_json(*_args) ⇒ JSON

Serialize an instance of TemplateMessage to a JSON object

Returns:

  • (JSON)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/trycourier/send/types/template_message.rb', line 132

def to_json(*_args)
  {
    "template": @template,
    "to": @to,
    "data": @data,
    "brand_id": @brand_id,
    "channels": @channels,
    "context": @context,
    "metadata": @metadata,
    "providers": @providers,
    "routing": @routing,
    "timeout": @timeout,
    "delay": @delay,
    "expiry": @expiry
  }.to_json
end