Class: SlackRTMReceiver::Session
- Inherits:
-
Object
- Object
- SlackRTMReceiver::Session
- Defined in:
- lib/slack-rtm-receiver/session.rb
Overview
Websocket client for the RTM session
Instance Attribute Summary collapse
-
#last_timestamp ⇒ Object
(also: #ts)
readonly
Returns the value of attribute last_timestamp.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#self_uid ⇒ Object
readonly
Returns the value of attribute self_uid.
-
#websocket ⇒ Object
(also: #ws)
readonly
Returns the value of attribute websocket.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
True if RTM session is alive.
-
#close ⇒ Object
Close RTM session.
-
#idle_time ⇒ Float
Idle for how long?.
-
#initialize ⇒ Session
constructor
A new instance of Session.
-
#ping(timeout = 5) ⇒ Boolean
Send RTM ping.
-
#ping_if_idle(sec = 10) ⇒ Boolean
Ping if idle for more than set seconds.
- #reset_vars ⇒ Object
-
#send_event(event) ⇒ Hash
Send RTM event.
-
#start(json) ⇒ Boolean
Start RTM websocket session.
-
#stats(opts = {}) ⇒ Hash
Return statistics.
Constructor Details
#initialize ⇒ Session
Returns a new instance of Session.
17 18 19 20 21 |
# File 'lib/slack-rtm-receiver/session.rb', line 17 def initialize @logger = SlackRTMReceiver.logger logger.debug 'Initializing Session...' reset_vars end |
Instance Attribute Details
#last_timestamp ⇒ Object (readonly) Also known as: ts
Returns the value of attribute last_timestamp.
14 15 16 |
# File 'lib/slack-rtm-receiver/session.rb', line 14 def end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/slack-rtm-receiver/session.rb', line 10 def logger @logger end |
#self_uid ⇒ Object (readonly)
Returns the value of attribute self_uid.
11 12 13 |
# File 'lib/slack-rtm-receiver/session.rb', line 11 def self_uid @self_uid end |
#websocket ⇒ Object (readonly) Also known as: ws
Returns the value of attribute websocket.
12 13 14 |
# File 'lib/slack-rtm-receiver/session.rb', line 12 def websocket @websocket end |
Instance Method Details
#alive? ⇒ Boolean
True if RTM session is alive
86 87 88 89 |
# File 'lib/slack-rtm-receiver/session.rb', line 86 def alive? return true if ws && ts return false end |
#close ⇒ Object
Close RTM session
149 150 151 |
# File 'lib/slack-rtm-receiver/session.rb', line 149 def close ws.close if ws end |
#idle_time ⇒ Float
Idle for how long?
93 94 95 96 |
# File 'lib/slack-rtm-receiver/session.rb', line 93 def idle_time return 0 unless alive? return Time.now - ts end |
#ping(timeout = 5) ⇒ Boolean
Send RTM ping
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/slack-rtm-receiver/session.rb', line 101 def ping(timeout = 5) if @ping logger.debug 'RTM ping requested, but another was recently sent. Ignoring...' return false end event = send_event({type: 'ping', time: Time.now.to_f}) return false unless event timer = EM::Timer.new(timeout) do logger.warn "RTM ping timed out: threshold #{timeout} sec" close end event[:em_timer] = timer @ping = event @stats[:pings_sent] ||= 0 @stats[:pings_sent] += 1 return true end |
#ping_if_idle(sec = 10) ⇒ Boolean
Ping if idle for more than set seconds
122 123 124 125 |
# File 'lib/slack-rtm-receiver/session.rb', line 122 def ping_if_idle(sec = 10) return false if idle_time < sec ping end |
#reset_vars ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/slack-rtm-receiver/session.rb', line 23 def reset_vars @self_uid = nil @websocket = nil = nil @ping = nil @stats = {} end |
#send_event(event) ⇒ Hash
Send RTM event
76 77 78 79 80 81 82 83 |
# File 'lib/slack-rtm-receiver/session.rb', line 76 def send_event(event) return nil unless alive? event[:id] = new_event_id ws.send(JSON.fast_generate(event)) logger.info "Event sent with id: #{event[:id]} type: #{event[:type]}" logger.debug "Sent event: #{event}" return event end |
#start(json) ⇒ Boolean
Start RTM websocket session
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/slack-rtm-receiver/session.rb', line 34 def start(json) if ws logger.info 'RTM session start requested, but already running. Ignoring...' return false end h = parse_rtm_start_res(json) @self_uid = h[:self_id] logger.debug "I am Slack ID: #{self_uid}. Connecting to websocket...\n URL: #{h[:url]}" opts = {ping: 60} @websocket = Faye::WebSocket::Client.new(h[:url], nil, opts) ws.on :open do logger.debug 'Websocket opened' touch_ts end ws.on :message do |ws_event| touch_ts event = JSON.parse(ws_event.data, {symbolize_names: true}) case event[:type] when 'hello' hello_handler(event) when 'pong' pong_handler(event) else run_event_handlers(event) end @stats[:events_received] ||= 0 @stats[:events_received] += 1 end ws.on :close do |ws_event| logger.warn 'RTM session closed' cleanup end return true end |
#stats(opts = {}) ⇒ Hash
Return statistics
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/slack-rtm-receiver/session.rb', line 130 def stats(opts = {}) return nil if @stats.empty? secs = Time.now - @stats[:hello_time] secs = secs.to_i days = secs / 86400 secs = secs % 86400 hours = secs / 3600 secs = secs % 3600 mins = secs / 60 secs = secs % 60 if opts[:log] msg = "Statistics since #{@stats[:hello_time]} (#{days} days, #{hours} hrs, #{mins} mins, #{secs} secs)\n" msg << "#{@stats}" logger.info msg end return @stats end |