Class: Satori::RTM::SubscriptionEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/satori-rtm-sdk/model.rb

Overview

Event about new subscription data or subscription status change.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data) ⇒ SubscriptionEvent

Returns a new instance of SubscriptionEvent



23
24
25
26
27
28
29
30
31
# File 'lib/satori-rtm-sdk/model.rb', line 23

def initialize(type, data)
  if type == :pdu
    @type = resolve_type(data[:action])
    @data = data[:body]
  else
    @type = type
    @data = data
  end
end

Instance Attribute Details

#dataHash (readonly)

Returns an event data. In most cases it represent body field from incoming subscribe / subscription / unsubscribe PDUs. The type of PDU is accessible by type attribute. Information about fields in data could be found in RTM API specification.

Returns:

  • (Hash)

    event data



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/satori-rtm-sdk/model.rb', line 20

class SubscriptionEvent
  attr_reader :data, :type

  def initialize(type, data)
    if type == :pdu
      @type = resolve_type(data[:action])
      @data = data[:body]
    else
      @type = type
      @data = data
    end
  end

  # Returns +true+ if event is an error PDU.
  #
  # @return [Boolean] +true+ if event is an error PDU, +false+ otherwise
  def error?
    type == :error
  end

  private

  def resolve_type(action)
    case action
    when 'rtm/subscribe/ok'
      :subscribed
    when 'rtm/unsubscribe/ok'
      :unsubscribed
    when 'rtm/subscription/info'
      :info
    when 'rtm/subscription/data'
      :data
    when 'rtm/subscription/error', 'rtm/subscribe/error', 'rtm/unsubscribe/error'
      :error
    end
  end
end

#type:init, ... (readonly)

Returns a type of event.

Returns:

  • (:init)

    event before rtm/subscribe request is sent

  • (:subscribed)

    event when rtm/subscribe/ok is received

  • (:unsubscribed)

    event when rtm/unsubscribe/ok is received

  • (:error)

    event when rtm/subscription/error / rtm/subscribe/error are received

  • (:info)

    event when rtm/subscription/info is received

  • (:disconnect)

    event when connection is lost



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/satori-rtm-sdk/model.rb', line 20

class SubscriptionEvent
  attr_reader :data, :type

  def initialize(type, data)
    if type == :pdu
      @type = resolve_type(data[:action])
      @data = data[:body]
    else
      @type = type
      @data = data
    end
  end

  # Returns +true+ if event is an error PDU.
  #
  # @return [Boolean] +true+ if event is an error PDU, +false+ otherwise
  def error?
    type == :error
  end

  private

  def resolve_type(action)
    case action
    when 'rtm/subscribe/ok'
      :subscribed
    when 'rtm/unsubscribe/ok'
      :unsubscribed
    when 'rtm/subscription/info'
      :info
    when 'rtm/subscription/data'
      :data
    when 'rtm/subscription/error', 'rtm/subscribe/error', 'rtm/unsubscribe/error'
      :error
    end
  end
end

Instance Method Details

#error?Boolean

Returns true if event is an error PDU.

Returns:

  • (Boolean)

    true if event is an error PDU, false otherwise



36
37
38
# File 'lib/satori-rtm-sdk/model.rb', line 36

def error?
  type == :error
end