Class: Garufa::Subscription
- Inherits:
-
Object
- Object
- Garufa::Subscription
- Defined in:
- lib/garufa/subscription.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
- #channel ⇒ Object
- #channel_data ⇒ Object
- #channel_prefix ⇒ Object
-
#initialize(data, connection) ⇒ Subscription
constructor
A new instance of Subscription.
- #notify(message) ⇒ Object
- #presence_channel? ⇒ Boolean
- #private_channel? ⇒ Boolean
- #public_channel? ⇒ Boolean
- #set_error(code, message) ⇒ Object
- #socket_id ⇒ Object
- #subscribe ⇒ Object
- #success? ⇒ Boolean
- #unsubscribe ⇒ Object
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
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/garufa/subscription.rb', line 7 def error @error end |
Instance Method Details
#channel ⇒ Object
52 53 54 |
# File 'lib/garufa/subscription.rb', line 52 def channel @data['channel'] end |
#channel_data ⇒ Object
56 57 58 |
# File 'lib/garufa/subscription.rb', line 56 def channel_data @data['channel_data'] end |
#channel_prefix ⇒ Object
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() @connection. 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, ) @error = SubscriptionError.new(code, ) end |
#socket_id ⇒ Object
68 69 70 |
# File 'lib/garufa/subscription.rb', line 68 def socket_id @connection.socket_id end |
#subscribe ⇒ Object
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 |
#unsubscribe ⇒ Object
28 29 30 |
# File 'lib/garufa/subscription.rb', line 28 def unsubscribe Subscriptions.remove self end |