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"
RETRYING =
"retrying"
FAILED =
"failed"
BOUNCED =
"bounced"
OPENED =
"opened"
SUCCESS_STATUSES =
[DELIVERED, OPENED].freeze
TERMINAL_STATUSES =

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

(SUCCESS_STATUSES + [FAILED, BOUNCED]).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, client: nil) ⇒ Delivery

Returns a new instance of Delivery.



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

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.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def channel_id
  @channel_id
end

#channel_nameObject (readonly)

Returns the value of attribute channel_name.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def channel_name
  @channel_name
end

#channel_typeObject (readonly)

Returns the value of attribute channel_type.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def channel_type
  @channel_type
end

#created_atObject (readonly)

Returns the value of attribute created_at.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def created_at
  @created_at
end

#delivered_atObject (readonly)

Returns the value of attribute delivered_at.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def delivered_at
  @delivered_at
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def error_message
  @error_message
end

#failed_atObject (readonly)

Returns the value of attribute failed_at.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def failed_at
  @failed_at
end

#friendly_error_messageObject (readonly)

Returns the value of attribute friendly_error_message.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def friendly_error_message
  @friendly_error_message
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def id
  @id
end

#message_idObject (readonly)

Returns the value of attribute message_id.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def message_id
  @message_id
end

#opened_atObject (readonly)

Returns the value of attribute opened_at.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def opened_at
  @opened_at
end

#rawObject (readonly)

Returns the value of attribute raw.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def raw
  @raw
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def sent_at
  @sent_at
end

#statusObject (readonly)

Returns the value of attribute status.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def status
  @status
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



24
25
26
# File 'lib/relaygrid/delivery.rb', line 24

def updated_at
  @updated_at
end

Instance Method Details

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



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

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

#bounced?Boolean

Returns:

  • (Boolean)


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

def bounced?
  status == BOUNCED
end

#delivered?Boolean

Returns:

  • (Boolean)


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

def delivered?
  status == DELIVERED
end

#email?Boolean

Returns:

  • (Boolean)


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

def email?
  channel_type == "email"
end

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  status == FAILED
end

#hashObject



115
116
117
# File 'lib/relaygrid/delivery.rb', line 115

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

#inspectObject



119
120
121
122
# File 'lib/relaygrid/delivery.rb', line 119

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

#opened?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/relaygrid/delivery.rb', line 78

def opened?
  status == OPENED
end

#push?Boolean

Returns:

  • (Boolean)


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

def push?
  channel_type == "push"
end

#queued?Boolean

Returns:

  • (Boolean)


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

def queued?
  status == QUEUED
end

#refreshRelayGrid::Delivery

Returns this delivery's current server-side state.

Returns:



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

def refresh
  client.deliveries.get(id)
end

#retrying?Boolean

Returns:

  • (Boolean)


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

def retrying?
  status == RETRYING
end

#sent?Boolean

Returns:

  • (Boolean)


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

def sent?
  status == SENT
end

#success?Boolean

Reached a state the server considers final and successful.

Returns:

  • (Boolean)


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

def success?
  SUCCESS_STATUSES.include?(status)
end

#terminal?Boolean

Reached a state the server will not move away from on its own. A delivery being retried is retrying, not failed, so failed is final.

Returns:

  • (Boolean)


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

def terminal?
  TERMINAL_STATUSES.include?(status)
end

#to_hObject



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

def to_h
  raw
end