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
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
#error ⇒ Object (readonly)
Returns the value of attribute error.
8 9 10 |
# File 'lib/garufa/subscription.rb', line 8 def error @error end |
Instance Method Details
#channel ⇒ Object
55 56 57 |
# File 'lib/garufa/subscription.rb', line 55 def channel @data['channel'] end |
#channel_data ⇒ Object
59 60 61 |
# File 'lib/garufa/subscription.rb', line 59 def channel_data @data['channel_data'] end |
#channel_prefix ⇒ Object
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() @connection. end |
#presence_channel? ⇒ Boolean
43 44 45 |
# File 'lib/garufa/subscription.rb', line 43 def presence_channel? channel_prefix == 'presence' end |
#private_channel? ⇒ Boolean
39 40 41 |
# File 'lib/garufa/subscription.rb', line 39 def private_channel? channel_prefix == 'private' end |
#public_channel? ⇒ 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, ) @error = SubscriptionError.new(code, ) end |
#socket_id ⇒ Object
71 72 73 |
# File 'lib/garufa/subscription.rb', line 71 def socket_id @connection.socket_id end |
#subscribe ⇒ Object
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
51 52 53 |
# File 'lib/garufa/subscription.rb', line 51 def success? @error.nil? end |
#unsubscribe ⇒ Object
31 32 33 |
# File 'lib/garufa/subscription.rb', line 31 def unsubscribe Subscriptions.remove self end |