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



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

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

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

Instance Method Details

#channelObject



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

def channel
  @data['channel']
end

#channel_dataObject



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

def channel_data
  @data['channel_data']
end

#channel_prefixObject



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

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

#notify(message) ⇒ Object



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

def notify(message)
  @connection.send_message message
end

#presence_channel?Boolean



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

def presence_channel?
  channel_prefix == 'presence'
end

#private_channel?Boolean



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

def private_channel?
  channel_prefix == 'private'
end

#public_channel?Boolean



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

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

#set_error(code, message) ⇒ Object



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

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

#socket_idObject



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

def socket_id
  @connection.socket_id
end

#subscribeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/garufa/subscription.rb', line 13

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



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

def success?
  @error.nil?
end

#unsubscribeObject



28
29
30
# File 'lib/garufa/subscription.rb', line 28

def unsubscribe
  Subscriptions.remove self
end