-
Notifications
You must be signed in to change notification settings - Fork 19
/
connection_state_change.rb
42 lines (38 loc) · 1.46 KB
/
connection_state_change.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Ably::Models
# ConnectionStateChange is a class that is emitted by the {Ably::Realtime::Connection} object
# when a state change occurs
#
# @!attribute [r] current
# @return [Connection::STATE] Current connection state
# @!attribute [r] previous
# @return [Connection::STATE] Previous connection state
# @!attribute [r] retry_in
# @return [Integer] Time in seconds until the connection will reattempt to connect when in the +:disconnected+ or +:suspended+ state
# @!attribute [r] reason
# @return [Ably::Models::ErrorInfo] Object describing the reason for a state change when not initiated by the consumer of the client library
#
class ConnectionStateChange
include Ably::Modules::ModelCommon
def initialize(hash_object)
unless (hash_object.keys - [:current, :previous, :retry_in, :reason]).empty?
raise ArgumentError, 'Invalid attributes, expecting :current, :previous, :retry_in, :reason'
end
@hash_object = {
current: hash_object.fetch(:current),
previous: hash_object.fetch(:previous),
retry_in: hash_object[:retry_in],
reason: hash_object[:reason]
}
rescue KeyError => e
raise ArgumentError, e
end
%w(current previous retry_in reason).each do |attribute|
define_method attribute do
@hash_object[attribute.to_sym]
end
end
def to_s
"ConnectionStateChange: current state #{current}, previous state #{previous}"
end
end
end