Class: Colossus::Faye::Extension
- Inherits:
-
Object
- Object
- Colossus::Faye::Extension
- Defined in:
- lib/colossus/faye/extension.rb
Overview
Faye extension implementing all the presence, authorization, authentification and push logic.
Constant Summary collapse
- VALID_STATUSES =
%w(disconnected away active).freeze
Instance Attribute Summary collapse
-
#colossus ⇒ Object
readonly
Returns the value of attribute colossus.
-
#faye ⇒ Object
readonly
Returns the value of attribute faye.
-
#faye_client ⇒ Object
readonly
Returns the value of attribute faye_client.
Instance Method Summary collapse
- #acceptable?(message) ⇒ Boolean
- #handle_invalid_token(message) ⇒ Object
- #handle_presence_request(message) ⇒ Object
- #handle_presence_response(message) ⇒ Object
- #handle_publish(message) ⇒ Object
- #handle_server_action(message) ⇒ Object
- #handle_set_status(message) ⇒ Object
- #handle_subscribe(message) ⇒ Object
- #handle_user_action(message) ⇒ Object
- #incoming(message, _request, callback) ⇒ Object
-
#initialize(faye, colossus = Colossus.new) ⇒ Extension
constructor
A new instance of Extension.
- #invalid_user_channel?(user_id) ⇒ Boolean
Constructor Details
#initialize(faye, colossus = Colossus.new) ⇒ Extension
Returns a new instance of Extension.
10 11 12 13 14 15 16 17 18 |
# File 'lib/colossus/faye/extension.rb', line 10 def initialize(faye, colossus = Colossus.new) @colossus = colossus @faye = faye faye.add_extension(self) @faye_client = faye.get_client colossus_faye_extension = Colossus::WriterClient::FayeExtension. new(colossus.verifier.writer_token) faye_client.add_extension(colossus_faye_extension) end |
Instance Attribute Details
#colossus ⇒ Object (readonly)
Returns the value of attribute colossus.
6 7 8 |
# File 'lib/colossus/faye/extension.rb', line 6 def colossus @colossus end |
#faye ⇒ Object (readonly)
Returns the value of attribute faye.
6 7 8 |
# File 'lib/colossus/faye/extension.rb', line 6 def faye @faye end |
#faye_client ⇒ Object (readonly)
Returns the value of attribute faye_client.
6 7 8 |
# File 'lib/colossus/faye/extension.rb', line 6 def faye_client @faye_client end |
Instance Method Details
#acceptable?(message) ⇒ Boolean
37 38 39 40 41 |
# File 'lib/colossus/faye/extension.rb', line 37 def acceptable?() ['ext'] && (['ext']['user_token'] || ['ext']['writer_token']) end |
#handle_invalid_token(message) ⇒ Object
111 112 113 114 |
# File 'lib/colossus/faye/extension.rb', line 111 def handle_invalid_token() ['error'] = 'Invalid Token' end |
#handle_presence_request(message) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/colossus/faye/extension.rb', line 83 def handle_presence_request() user_ids = ['data'] && ['data']['user_ids'] presence_id = ['channel'].partition('/presences/request/').last if user_ids && user_ids.is_a?(Array) statuses = colossus.get_multi(user_ids) ['data'].delete('user_ids') faye_client.publish("/presences/response/#{presence_id}", { 'statuses' => Hash[user_ids.zip(statuses)] }) elsif user_ids == nil statuses = colossus.get_all faye_client.publish("/presences/response/#{presence_id}", { 'statuses' => statuses }) else .delete('data') ['error'] = 'Invalid user_ids data' end end |
#handle_presence_response(message) ⇒ Object
103 104 105 |
# File 'lib/colossus/faye/extension.rb', line 103 def handle_presence_response() end |
#handle_publish(message) ⇒ Object
107 108 109 |
# File 'lib/colossus/faye/extension.rb', line 107 def handle_publish() end |
#handle_server_action(message) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/colossus/faye/extension.rb', line 57 def handle_server_action() token = ['ext'] && ['ext']['writer_token'] unless token && colossus.verifier.verify_writer_token(token) ['error'] = 'Invalid Token' .delete('data') return end if ['channel'].start_with?('/meta/') .delete('data') elsif ['channel'].start_with?('/presences/request/') handle_presence_request() elsif ['channel'].start_with?('/presences/response/') handle_presence_response() elsif ['channel'].start_with?('/users/') handle_publish() else ['error'] = 'Unknown Action' .delete('data') end end |
#handle_set_status(message) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/colossus/faye/extension.rb', line 132 def handle_set_status() token = ['ext']['user_token'] user_id = ['channel'].partition('/users/').last status = ['data'] && ['data']['status'] if invalid_user_channel?(user_id) ['error'] = 'The only accepted channel_name is users/:user_id' return end unless status && VALID_STATUSES.include?(status) ['error'] = 'Invalid Status' return end unless colossus.verifier.verify_token(token, user_id) ['error'] = 'Invalid Token' return end colossus.set(user_id, ['clientId'], status) end |
#handle_subscribe(message) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/colossus/faye/extension.rb', line 116 def handle_subscribe() token = ['ext']['user_token'] user_id = ['subscription'].partition('/users/').last if invalid_user_channel?(user_id) ['error'] = 'The only accepted channel_name is users/:user_id' return end unless colossus.verifier.verify_token(token, user_id) ['error'] = 'Invalid Token' end end |
#handle_user_action(message) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/colossus/faye/extension.rb', line 43 def handle_user_action() if ['channel'] == '/meta/subscribe' handle_subscribe() elsif ['channel'].start_with?('/meta/') elsif ['channel'].start_with?('/users/') handle_set_status() else ['error'] = 'Unknown Action' end end |
#incoming(message, _request, callback) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/colossus/faye/extension.rb', line 20 def incoming(, _request, callback) if !acceptable?() handle_invalid_token() .delete('data') .delete('ext') elsif ['ext']['user_token'] handle_user_action() .delete('data') .delete('ext') elsif ['ext']['writer_token'] handle_server_action() .delete('ext') end callback.call() end |
#invalid_user_channel?(user_id) ⇒ Boolean
157 158 159 |
# File 'lib/colossus/faye/extension.rb', line 157 def invalid_user_channel?(user_id) user_id.empty? || user_id.include?('*') || user_id.include?('/') end |