Class: Webmate::Responders::Abstract
- Inherits:
-
Object
- Object
- Webmate::Responders::Abstract
- Includes:
- ActiveSupport::Rescuable, Callbacks
- Defined in:
- lib/webmate/responders/abstract.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #action_method ⇒ Object
- #async(&block) ⇒ Object
- #build_connection ⇒ Object
- #channel_active?(channel_name) ⇒ Boolean
-
#channels_to_publish ⇒ Object
take @users_to_notify and pass back channels names with listeners.
-
#initialize(request_info) ⇒ Abstract
constructor
request info - current request params.
- #process_action ⇒ Object
- #publish(response) ⇒ Object
-
#publish_to(*user_ids) ⇒ Object
publish current response to private channels for users in user_ids.
-
#publisher ⇒ Object
subscriptions.
- #render_not_found ⇒ Object
- #rescue_with_handler(exception) ⇒ Object
- #respond ⇒ Object
- #respond_with(response, options = {}) ⇒ Object
Constructor Details
#initialize(request_info) ⇒ Abstract
request info - current request params
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/webmate/responders/abstract.rb', line 10 def initialize(request_info) @action = request_info[:action] @path = request_info[:path] @metadata = request_info[:metadata] @params = request_info[:params] @request = request_info[:request] # publishing actions @users_to_notify = [] @response = nil end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
6 7 8 |
# File 'lib/webmate/responders/abstract.rb', line 6 def action @action end |
#metadata ⇒ Object
Returns the value of attribute metadata.
6 7 8 |
# File 'lib/webmate/responders/abstract.rb', line 6 def @metadata end |
#params ⇒ Object
Returns the value of attribute params.
6 7 8 |
# File 'lib/webmate/responders/abstract.rb', line 6 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/webmate/responders/abstract.rb', line 6 def path @path end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/webmate/responders/abstract.rb', line 7 def request @request end |
Instance Method Details
#action_method ⇒ Object
23 24 25 |
# File 'lib/webmate/responders/abstract.rb', line 23 def action_method action.to_s end |
#async(&block) ⇒ Object
74 75 76 |
# File 'lib/webmate/responders/abstract.rb', line 74 def async(&block) block.call end |
#build_connection ⇒ Object
86 87 88 89 90 91 |
# File 'lib/webmate/responders/abstract.rb', line 86 def build_connection EM::Hiredis.connect rescue warn("problem with connections to redis") nil end |
#channel_active?(channel_name) ⇒ Boolean
111 112 113 |
# File 'lib/webmate/responders/abstract.rb', line 111 def channel_active?(channel_name) return true # in development end |
#channels_to_publish ⇒ Object
take @users_to_notify and pass back channels names with listeners
104 105 106 107 108 109 |
# File 'lib/webmate/responders/abstract.rb', line 104 def channels_to_publish @users_to_notify.flatten.each_with_object([]) do |user_id, channels| channel_name = Webmate::Application.get_channel_name_for(user_id) channels << channel_name if channel_active?(channel_name) end end |
#process_action ⇒ Object
65 66 67 68 |
# File 'lib/webmate/responders/abstract.rb', line 65 def process_action raise ActionNotFound unless respond_to?(action_method) respond_with send(action_method) end |
#publish(response) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/webmate/responders/abstract.rb', line 115 def publish(response) return if publisher.nil? # prepare args for socket.io message packet # this should be prepared data to create socket.io message # without any additional actions packet_data = Webmate::SocketIO::Packets::Message.prepare_packet_data(response) data = Webmate::JSON.dump(packet_data) channels_to_publish.each {|channel_name| publisher.publish(channel_name, data) } end |
#publish_to(*user_ids) ⇒ Object
publish current response to private channels for users in user_ids
publish_to(123, 456)
97 98 99 |
# File 'lib/webmate/responders/abstract.rb', line 97 def publish_to(*user_ids) @users_to_notify += user_ids end |
#publisher ⇒ Object
subscriptions
82 83 84 |
# File 'lib/webmate/responders/abstract.rb', line 82 def publisher @publisher ||= build_connection end |
#render_not_found ⇒ Object
70 71 72 |
# File 'lib/webmate/responders/abstract.rb', line 70 def render_not_found @response = Response.new("Action not Found", status: 200) end |
#rescue_with_handler(exception) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/webmate/responders/abstract.rb', line 57 def rescue_with_handler(exception) if handler = handler_for_rescue(exception) handler.arity != 0 ? handler.call(exception) : handler.call else raise(exception) end end |
#respond ⇒ Object
31 32 33 34 35 |
# File 'lib/webmate/responders/abstract.rb', line 31 def respond process_action rescue Exception => e rescue_with_handler(e) end |
#respond_with(response, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/webmate/responders/abstract.rb', line 37 def respond_with(response, = {}) if @response.is_a?(Response) @response = response else = { status: 200, path: path, metadata: , params: params, action: action } = .merge() @response = Response.new(response, ) end # publish changes to users actions async { publish(@response) } @response end |