Class: Garufa::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/garufa/subscription.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, connection) ⇒ Subscription

Returns a new instance of Subscription.



10
11
12
13
14
# File 'lib/garufa/subscription.rb', line 10

def initialize(data, connection)
  @data = data
  @connection = connection
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/garufa/subscription.rb', line 8

def error
  @error
end

Instance Method Details

#channelObject



55
56
57
# File 'lib/garufa/subscription.rb', line 55

def channel
  @data['channel']
end

#channel_dataObject



59
60
61
# File 'lib/garufa/subscription.rb', line 59

def channel_data
  @data['channel_data']
end

#channel_prefixObject



63
64
65
# File 'lib/garufa/subscription.rb', line 63

def channel_prefix
  channel[/^private-|presence-/].to_s[0...-1]
end

#notify(message) ⇒ Object



67
68
69
# File 'lib/garufa/subscription.rb', line 67

def notify(message)
  @connection.send_message message
end

#presence_channel?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/garufa/subscription.rb', line 43

def presence_channel?
  channel_prefix == 'presence'
end

#private_channel?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/garufa/subscription.rb', line 39

def private_channel?
  channel_prefix == 'private'
end

#public_channel?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/garufa/subscription.rb', line 35

def public_channel?
  !(private_channel? || presence_channel?)
end

#set_error(code, message) ⇒ Object



47
48
49
# File 'lib/garufa/subscription.rb', line 47

def set_error(code, message)
  @error = SubscriptionError.new(code, message)
end

#socket_idObject



71
72
73
# File 'lib/garufa/subscription.rb', line 71

def socket_id
  @connection.socket_id
end

#subscribeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/garufa/subscription.rb', line 16

def subscribe
  case true
  when !valid_channel?
    set_error(nil, 'Invalid channel')
  when !public_channel? && !valid_signature?
    set_error(nil, 'Invalid signature')
  when !public_channel? && !valid_app_key?
    set_error(nil, 'Invalid key')
  when already_subscribed?
    set_error(nil, "Already subscribed to channel: #{channel}")
  else
    Subscriptions.add self
  end
end

#success?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/garufa/subscription.rb', line 51

def success?
  @error.nil?
end

#unsubscribeObject



31
32
33
# File 'lib/garufa/subscription.rb', line 31

def unsubscribe
  Subscriptions.remove self
end