Class: Pubnub::Subscriber
Overview
Module that holds subscriber logic
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#current_subscription ⇒ Object
readonly
Returns the value of attribute current_subscription.
-
#current_subscription_id ⇒ Object
readonly
Returns the value of attribute current_subscription_id.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#listeners ⇒ Object
readonly
Returns the value of attribute listeners.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#wildcard_channels ⇒ Object
readonly
Returns the value of attribute wildcard_channels.
Instance Method Summary collapse
- #add_listener(options) ⇒ Object
- #add_subscription(event) ⇒ Object
- #announce_status(options) ⇒ Object
- #fire_async_callbacks(envelopes) ⇒ Object
-
#initialize(app) ⇒ Subscriber
constructor
A new instance of Subscriber.
- #remove_listener(options) ⇒ Object
- #remove_subscription(event) ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(app) ⇒ Subscriber
Returns a new instance of Subscriber.
9 10 11 12 13 14 15 16 |
# File 'lib/pubnub/subscriber.rb', line 9 def initialize(app) @app = app @channels = [] @groups = [] @wildcard_channels = [] @listeners = {} @callbacks = { channels: {}, groups: {}, wildcard_channels: {} } end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
5 6 7 |
# File 'lib/pubnub/subscriber.rb', line 5 def callbacks @callbacks end |
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
5 6 7 |
# File 'lib/pubnub/subscriber.rb', line 5 def channels @channels end |
#current_subscription ⇒ Object (readonly)
Returns the value of attribute current_subscription.
5 6 7 |
# File 'lib/pubnub/subscriber.rb', line 5 def current_subscription @current_subscription end |
#current_subscription_id ⇒ Object (readonly)
Returns the value of attribute current_subscription_id.
5 6 7 |
# File 'lib/pubnub/subscriber.rb', line 5 def current_subscription_id @current_subscription_id end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
5 6 7 |
# File 'lib/pubnub/subscriber.rb', line 5 def groups @groups end |
#listeners ⇒ Object (readonly)
Returns the value of attribute listeners.
6 7 8 |
# File 'lib/pubnub/subscriber.rb', line 6 def listeners @listeners end |
#ssl ⇒ Object
Returns the value of attribute ssl.
7 8 9 |
# File 'lib/pubnub/subscriber.rb', line 7 def ssl @ssl end |
#wildcard_channels ⇒ Object (readonly)
Returns the value of attribute wildcard_channels.
5 6 7 |
# File 'lib/pubnub/subscriber.rb', line 5 def wildcard_channels @wildcard_channels end |
Instance Method Details
#add_listener(options) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/pubnub/subscriber.rb', line 18 def add_listener() name = ([:name] || UUID.generate).to_sym callback = [:callback] fail 'Invalid listener.' unless callback.is_a?(SubscribeCallback) fail 'Listener with such name already exists.' if @listeners.keys.include?(name) @listeners[name] = callback end |
#add_subscription(event) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/pubnub/subscriber.rb', line 39 def add_subscription(event) @ssl = event.ssl Pubnub.logger.debug('Pubnub::Subscriber') { 'Adding subscription to Subscriber' } add_channels event add_groups event add_wildcard_channels event Pubnub.logger.debug('Pubnub::Subscriber') { 'Added subscription to Subscriber' } end |
#announce_status(options) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/pubnub/subscriber.rb', line 84 def announce_status() announcement_type = [:announcement_type] event = [:event] = [:given_options] request = [:request] _response = [:response] case announcement_type when Pubnub::Constants::TIMEOUT_ANNOUNCEMENT envelope = ErrorEnvelope.new( event: event, event_options: , timetoken: nil, status: { code: nil, client_request: request, server_response: nil, data: nil, category: Pubnub::Constants::STATUS_TIMEOUT, error: true, auto_retried: true, current_timetoken: @app.env[:timetoken].to_i, last_timetoken: @app.env[:timetoken].to_i, subscribed_channels: @app.subscribed_channels, subscribed_channel_groups: @app.subscribed_groups, config: get_config }, result: { code: nil, operation: nil, client_request: request, server_response: nil, data: nil } ) when Pubnub::Constants::RECONNECTED_ANNOUNCEMENT envelope = ErrorEnvelope.new( event: event, event_options: , timetoken: nil, status: { code: nil, client_request: request, server_response: nil, data: nil, category: Pubnub::Constants::STATUS_ACK, error: false, auto_retried: true, current_timetoken: @app.env[:timetoken].to_i, last_timetoken: @app.env[:timetoken].to_i, subscribed_channels: @app.subscribed_channels, subscribed_channel_groups: @app.subscribed_groups, config: get_config }, result: { code: nil, operation: nil, client_request: request, server_response: nil, data: nil } ) else Pubnub.logger.warn('Unknown announcement type.') end @listeners.each do |_name, callbacks| secure_call callbacks.callbacks[:status], envelope end end |
#fire_async_callbacks(envelopes) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pubnub/subscriber.rb', line 63 def fire_async_callbacks(envelopes) Pubnub.logger.debug('Pubnub::Subscriber') { 'Firing callback from listeners' } envelopes.each do |envelope| @listeners.each do |name, callbacks| Pubnub.logger.debug('Pubnub::Subscriber') { "Firing callbacks from listener '#{name}'." } if envelope.is_a?(ErrorEnvelope) secure_call callbacks.callbacks[:status], envelope else case envelope.result[:operation] when Pubnub::Constants::OPERATION_SUBSCRIBE secure_call callbacks.callbacks[:message], envelope when Pubnub::Constants::OPERATION_PRESENCE secure_call callbacks.callbacks[:presence], envelope else secure_call callbacks.callbacks[:status], envelope end end end end end |
#remove_listener(options) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pubnub/subscriber.rb', line 28 def remove_listener() name = [:name] callback = [:callback] fail 'You have to specify name _or_ listener object.' if name && callback @listeners.delete_if { |k, _v| k == name.to_sym } if name @listeners.delete_if { |_k, v| v == callback } if callback end |
#remove_subscription(event) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/pubnub/subscriber.rb', line 48 def remove_subscription(event) Pubnub.logger.debug('Pubnub::Subscriber') { 'Removing subscription from Subscriber' } remove_channels event remove_groups event remove_wildcard_channels event Pubnub.logger.debug('Pubnub::Subscriber') { 'Removed subscription from Subscriber' } end |
#reset ⇒ Object
56 57 58 59 60 61 |
# File 'lib/pubnub/subscriber.rb', line 56 def reset remove_current_subscription return if @channels.empty? && @groups.empty? && @wildcard_channels.empty? build_subscription start_subscription end |