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.
Instance Method Summary collapse
- #acceptable?(message) ⇒ Boolean
- #handle_invalid_token(message) ⇒ Object
- #handle_set_status(message) ⇒ Object
- #handle_subscribe(message) ⇒ Object
- #handle_user_action(message) ⇒ Object
- #incoming(message, callback) ⇒ Object
-
#initialize(faye, colossus = Colossus.new) ⇒ Extension
constructor
A new instance of Extension.
- #invalid_user_channel?(user_id) ⇒ Boolean
Constructor Details
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 |
Instance Method Details
#acceptable?(message) ⇒ Boolean
32 33 34 35 |
# File 'lib/colossus/faye/extension.rb', line 32 def acceptable?() ['ext'] && (['ext']['user_token'] || ['ext']['writer_token']) end |
#handle_invalid_token(message) ⇒ Object
51 52 53 54 |
# File 'lib/colossus/faye/extension.rb', line 51 def handle_invalid_token() ['error'] = 'Invalid Token' end |
#handle_set_status(message) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/colossus/faye/extension.rb', line 72 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/colossus/faye/extension.rb', line 56 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
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/colossus/faye/extension.rb', line 37 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, callback) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/colossus/faye/extension.rb', line 16 def incoming(, 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'] .delete('ext') end callback.call() end |
#invalid_user_channel?(user_id) ⇒ Boolean
97 98 99 |
# File 'lib/colossus/faye/extension.rb', line 97 def invalid_user_channel?(user_id) user_id.empty? || user_id.include?('*') || user_id.include?('/') end |