Class: RelayGrid::Resources::Messages

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

Overview

The receive side: reading a recipient's inbox and marking messages off.

user_id is always your identifier for the user (the API's external_user_id), never a RelayGrid internal id.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Messages

Returns a new instance of Messages.



10
11
12
# File 'lib/relaygrid/resources/messages.rb', line 10

def initialize(client)
  @client = client
end

Instance Method Details

#get(id) ⇒ Hash

Returns the message with its deliveries.

Parameters:

  • id (Integer)

    message id

Returns:

  • (Hash)

    the message with its deliveries



31
32
33
# File 'lib/relaygrid/resources/messages.rb', line 31

def get(id)
  @client.get("/api/v1/messages/#{id}")["message"]
end

#mark_as_delivered(id) ⇒ Boolean

Confirm a push message reached the device. Normally the browser/device does this over the websocket; this is the HTTP equivalent.

Returns:

  • (Boolean)


51
52
53
# File 'lib/relaygrid/resources/messages.rb', line 51

def mark_as_delivered(id)
  @client.patch("/api/v1/messages/#{id}/mark_as_delivered")["success"] == true
end

#mark_as_seen(id, notification_channel_id: nil) ⇒ Hash

Mark a message read. With a channel id, the matching delivery is marked opened (which is what drives open-rate analytics); without one, only the message's own seen flag is set.

Returns:

  • (Hash)

    the updated message



40
41
42
43
44
45
# File 'lib/relaygrid/resources/messages.rb', line 40

def mark_as_seen(id, notification_channel_id: nil)
  body = {}
  body[:notification_channel_id] = notification_channel_id if notification_channel_id

  @client.patch("/api/v1/messages/#{id}/mark_as_seen", body: body)["message"]
end

#new_for(user_id:) ⇒ Array<Hash>

Unseen messages for a recipient, newest first. The polling counterpart to RelayGrid.websocket, and what an inbox view renders.

Parameters:

  • user_id (String)

Returns:

  • (Array<Hash>)

Raises:



20
21
22
23
24
25
26
27
# File 'lib/relaygrid/resources/messages.rb', line 20

def new_for(user_id:)
  body = @client.get(
    "/api/v1/messages/new_messages_for",
    params: { external_user_id: user_id.to_s }
  )

  Array(body["messages"])
end

#render(id) ⇒ Hash

Rendered subject and body with the message's attributes substituted in.

Returns:

  • (Hash)

    { "rendered_subject" => ..., "rendered_body" => ... }



58
59
60
# File 'lib/relaygrid/resources/messages.rb', line 58

def render(id)
  @client.get("/api/v1/messages/#{id}/render_message")
end