Class: RelayGrid::Delivery

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

Overview

One attempt to deliver a message over one channel (email, push, ...).

Immutable: #refresh returns a new Delivery rather than mutating this one, so a Delivery handed to another thread never changes underneath it.

Constant Summary collapse

QUEUED =
"queued"
SENT =
"sent"
DELIVERED =
"delivered"
FAILED =
"failed"
BOUNCED =
"bounced"
OPENED =
"opened"
SUCCESS_STATUSES =

States the server will not move away from on its own.

[DELIVERED, OPENED].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, client: nil) ⇒ Delivery

Returns a new instance of Delivery.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/relaygrid/delivery.rb', line 26

def initialize(attributes, client: nil)
  @raw = attributes
  @client = client

  @id = attributes["id"]
  @message_id = attributes["message_id"]
  @status = attributes["status"]
  @error_message = attributes["error_message"]
  @friendly_error_message = attributes["friendly_error_message"]

  channel = attributes["notification_channel"] || {}
  @channel_id = channel["id"]
  @channel_name = channel["name"]
  @channel_type = channel["channel_type"]

  @sent_at = parse_time(attributes["sent_at"])
  @delivered_at = parse_time(attributes["delivered_at"])
  @failed_at = parse_time(attributes["failed_at"])
  @opened_at = parse_time(attributes["opened_at"])
  @created_at = parse_time(attributes["created_at"])
  @updated_at = parse_time(attributes["updated_at"])

  freeze
end

Instance Attribute Details

#channel_idObject (readonly)

Returns the value of attribute channel_id.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def channel_id
  @channel_id
end

#channel_nameObject (readonly)

Returns the value of attribute channel_name.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def channel_name
  @channel_name
end

#channel_typeObject (readonly)

Returns the value of attribute channel_type.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def channel_type
  @channel_type
end

#created_atObject (readonly)

Returns the value of attribute created_at.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def created_at
  @created_at
end

#delivered_atObject (readonly)

Returns the value of attribute delivered_at.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def delivered_at
  @delivered_at
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def error_message
  @error_message
end

#failed_atObject (readonly)

Returns the value of attribute failed_at.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def failed_at
  @failed_at
end

#friendly_error_messageObject (readonly)

Returns the value of attribute friendly_error_message.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def friendly_error_message
  @friendly_error_message
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def id
  @id
end

#message_idObject (readonly)

Returns the value of attribute message_id.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def message_id
  @message_id
end

#opened_atObject (readonly)

Returns the value of attribute opened_at.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def opened_at
  @opened_at
end

#rawObject (readonly)

Returns the value of attribute raw.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def raw
  @raw
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def sent_at
  @sent_at
end

#statusObject (readonly)

Returns the value of attribute status.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def status
  @status
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



21
22
23
# File 'lib/relaygrid/delivery.rb', line 21

def updated_at
  @updated_at
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



101
102
103
# File 'lib/relaygrid/delivery.rb', line 101

def ==(other)
  other.is_a?(Delivery) && other.id == id && other.status == status
end

#bounced?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/relaygrid/delivery.rb', line 67

def bounced?
  status == BOUNCED
end

#delivered?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/relaygrid/delivery.rb', line 59

def delivered?
  status == DELIVERED
end

#email?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/relaygrid/delivery.rb', line 88

def email?
  channel_type == "email"
end

#failed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/relaygrid/delivery.rb', line 63

def failed?
  status == FAILED
end

#hashObject



106
107
108
# File 'lib/relaygrid/delivery.rb', line 106

def hash
  [self.class, id, status].hash
end

#inspectObject



110
111
112
113
# File 'lib/relaygrid/delivery.rb', line 110

def inspect
  "#<RelayGrid::Delivery id: #{id.inspect}, channel_type: #{channel_type.inspect}, " \
    "status: #{status.inspect}>"
end

#opened?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/relaygrid/delivery.rb', line 71

def opened?
  status == OPENED
end

#push?Boolean

Returns:

  • (Boolean)


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

def push?
  channel_type == "push"
end

#queued?Boolean

Returns:

  • (Boolean)


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

def queued?
  status == QUEUED
end

#refreshRelayGrid::Delivery

Returns this delivery's current server-side state.

Returns:



93
94
95
# File 'lib/relaygrid/delivery.rb', line 93

def refresh
  client.deliveries.get(id)
end

#sent?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/relaygrid/delivery.rb', line 55

def sent?
  status == SENT
end

#success?Boolean

Reached a state the server considers final and successful.

failed is deliberately not terminal: delivery jobs retry, so a failed row can still flip to sent. This mirrors the dashboard's polling semantics (see docs/DELIVERY_STATUS_PLAN.md in the API repo).

Returns:

  • (Boolean)


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

def success?
  SUCCESS_STATUSES.include?(status)
end

#to_hObject



97
98
99
# File 'lib/relaygrid/delivery.rb', line 97

def to_h
  raw
end