Class: RelayGrid::Resources::Notifications

Inherits:
Object
  • Object
show all
Defined in:
lib/relaygrid/resources/notifications.rb

Overview

Sending. The developer contract the gem exists for: a user, a template name, and the attributes the template needs.

Constant Summary collapse

RECIPIENT_ATTRIBUTES =

Recipient details a send may carry alongside the id. email becomes the contact email deliveries resolve their recipient through.

%i[first_name middle_name last_name email].freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Notifications

Returns a new instance of Notifications.



12
13
14
# File 'lib/relaygrid/resources/notifications.rb', line 12

def initialize(client)
  @client = client
end

Instance Method Details

#notify(user:, template:, attributes: {}) ⇒ RelayGrid::SendResult

Send a notification, creating or updating the recipient on the way.

Examples:

result = RelayGrid.client.notify(
  user:       { id: "user-123", first_name: "Ada", last_name: "Lovelace" },
  template:   "order_shipped",
  attributes: { order_number: "1042", eta: "tomorrow" }
)
result.delivery_ids # => [201, 202]

Parameters:

  • user (Hash)

    { id:, first_name:, last_name:, middle_name:, email: }. id is the your-system identifier -- it maps to the API's external_user_id. RelayGrid's internal ids are never exposed here. May also be a bare String/Integer id for a recipient that already exists, in which case nothing about them is sent.

    email is optional but load-bearing: email deliveries resolve their recipient through the contact stored for that channel type, so a send over an email channel for a user with no email on file produces a delivery that fails with "no contact on file". Push needs no contact. Passing an empty string removes the contact.

  • template (String)

    the message template's name

  • attributes (Hash) (defaults to: {})

    values for the template's {{placeholders}}

Returns:

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/relaygrid/resources/notifications.rb', line 44

def notify(user:, template:, attributes: {})
  message = {
    message_template_name: template.to_s,
    message_attributes_attributes: format_attributes(attributes),
  }
  message.merge!(recipient_params(user))

  SendResult.new(@client.post("/api/v1/messages", body: { message: message }), client: @client)
end