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.



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

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"]
  @scheduled_at = parse_time(message["scheduled_at"])

  @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.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def deliveries
  @deliveries
end

#message_idObject (readonly)

Returns the value of attribute message_id.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def message_id
  @message_id
end

#push_tokenObject (readonly)

Returns the value of attribute push_token.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def push_token
  @push_token
end

#push_token_expires_atObject (readonly)

Returns the value of attribute push_token_expires_at.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def push_token_expires_at
  @push_token_expires_at
end

#rawObject (readonly)

Returns the value of attribute raw.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def raw
  @raw
end

#rendered_bodyObject (readonly)

Returns the value of attribute rendered_body.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def rendered_body
  @rendered_body
end

#rendered_subjectObject (readonly)

Returns the value of attribute rendered_subject.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def rendered_subject
  @rendered_subject
end

#scheduled_atObject (readonly)

Returns the value of attribute scheduled_at.



11
12
13
# File 'lib/relaygrid/send_result.rb', line 11

def scheduled_at
  @scheduled_at
end

Instance Method Details

#delivery_for(channel_type) ⇒ Object



45
46
47
# File 'lib/relaygrid/send_result.rb', line 45

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.



35
36
37
# File 'lib/relaygrid/send_result.rb', line 35

def delivery_ids
  deliveries.map(&:id)
end

#inspectObject



84
85
86
87
# File 'lib/relaygrid/send_result.rb', line 84

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)


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

def push?
  !push_token.nil?
end

#scheduled?Boolean

True when this send was scheduled for later rather than sent immediately.

Returns:

  • (Boolean)


50
51
52
# File 'lib/relaygrid/send_result.rb', line 50

def scheduled?
  !scheduled_at.nil?
end

#to_hObject



80
81
82
# File 'lib/relaygrid/send_result.rb', line 80

def to_h
  raw
end

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

Poll until every delivery reaches a terminal state, or timeout seconds elapse. A delivery the server is still retrying reads retrying.

A scheduled delivery is never terminal -- nothing has been handed to a provider yet, so there is nothing to settle -- and polling it would just burn the full timeout waiting for a state change that isn't coming until dispatch. When scheduled? is true, this returns deliveries (each still reading status: "scheduled") immediately without calling the API at all.

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:



69
70
71
72
73
74
75
76
77
78
# File 'lib/relaygrid/send_result.rb', line 69

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

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