Class: RelayGrid::SendResult

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

Overview

What RelayGrid.client.notify hands back: the message that was created, the deliveries queued for it, and -- when the template has a live push channel -- the short-lived token the recipient's device needs to subscribe to the realtime channel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, client:) ⇒ SendResult

Returns a new instance of SendResult.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/relaygrid/send_result.rb', line 12

def initialize(body, client:)
  @raw = body
  @client = client

  message = body["message"] || {}
  @message_id = message["id"]
  @rendered_subject = message["rendered_subject"]
  @rendered_body = message["rendered_body"]

  @push_token = body["push_token"]
  expires_in = body["push_token_expires_in"]
  @push_token_expires_at = expires_in ? Time.now + expires_in.to_i : nil

  @deliveries = Array(message["deliveries"]).map { |d| Delivery.new(d, client: client) }.freeze

  freeze
end

Instance Attribute Details

#deliveriesObject (readonly)

Returns the value of attribute deliveries.



9
10
11
# File 'lib/relaygrid/send_result.rb', line 9

def deliveries
  @deliveries
end

#message_idObject (readonly)

Returns the value of attribute message_id.



9
10
11
# File 'lib/relaygrid/send_result.rb', line 9

def message_id
  @message_id
end

#push_tokenObject (readonly)

Returns the value of attribute push_token.



9
10
11
# File 'lib/relaygrid/send_result.rb', line 9

def push_token
  @push_token
end

#push_token_expires_atObject (readonly)

Returns the value of attribute push_token_expires_at.



9
10
11
# File 'lib/relaygrid/send_result.rb', line 9

def push_token_expires_at
  @push_token_expires_at
end

#rawObject (readonly)

Returns the value of attribute raw.



9
10
11
# File 'lib/relaygrid/send_result.rb', line 9

def raw
  @raw
end

#rendered_bodyObject (readonly)

Returns the value of attribute rendered_body.



9
10
11
# File 'lib/relaygrid/send_result.rb', line 9

def rendered_body
  @rendered_body
end

#rendered_subjectObject (readonly)

Returns the value of attribute rendered_subject.



9
10
11
# File 'lib/relaygrid/send_result.rb', line 9

def rendered_subject
  @rendered_subject
end

Instance Method Details

#delivery_for(channel_type) ⇒ Object



42
43
44
# File 'lib/relaygrid/send_result.rb', line 42

def delivery_for(channel_type)
  deliveries.find { |delivery| delivery.channel_type == channel_type.to_s }
end

#delivery_idsObject

Ids to hand to client.deliveries.get_all later. The primary reason a caller keeps anything from a send.



32
33
34
# File 'lib/relaygrid/send_result.rb', line 32

def delivery_ids
  deliveries.map(&:id)
end

#inspectObject



73
74
75
76
# File 'lib/relaygrid/send_result.rb', line 73

def inspect
  "#<RelayGrid::SendResult message_id: #{message_id.inspect}, " \
    "delivery_ids: #{delivery_ids.inspect}, push: #{push?}>"
end

#push?Boolean

True when the template dispatched over a push channel, so push_token is present and worth handing down to the recipient's device.

Returns:

  • (Boolean)


38
39
40
# File 'lib/relaygrid/send_result.rb', line 38

def push?
  !push_token.nil?
end

#to_hObject



69
70
71
# File 'lib/relaygrid/send_result.rb', line 69

def to_h
  raw
end

#wait_for_deliveries(timeout: 30, interval: 2, raise_on_timeout: false) ⇒ Array<RelayGrid::Delivery>

Poll until every delivery settles into a success state, or timeout seconds elapse.

failed is not treated as terminal inside the window: delivery jobs retry, so a failed row can still flip to sent and then delivered. That mirrors the dashboard's semantics.

Parameters:

  • timeout (Numeric) (defaults to: 30)

    seconds to keep polling

  • interval (Numeric) (defaults to: 2)

    seconds between polls

  • raise_on_timeout (Boolean) (defaults to: false)

    raise TimeoutWaitingForDeliveries instead of returning the last-seen deliveries

Returns:



58
59
60
61
62
63
64
65
66
67
# File 'lib/relaygrid/send_result.rb', line 58

def wait_for_deliveries(timeout: 30, interval: 2, raise_on_timeout: false)
  return deliveries if deliveries.empty?

  @client.deliveries.wait_for(
    delivery_ids,
    timeout: timeout,
    interval: interval,
    raise_on_timeout: raise_on_timeout
  )
end