Class: Webmate::Routes::Handler
- Inherits:
-
Object
- Object
- Webmate::Routes::Handler
- Defined in:
- lib/webmate/routes/handler.rb
Instance Attribute Summary collapse
-
#application ⇒ Object
Returns the value of attribute application.
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
- #handle(route_info) ⇒ Object
-
#initialize(application, request) ⇒ Handler
constructor
A new instance of Handler.
-
#prepare_response_params(route_info, request) ⇒ Object
this method prepare data for responder from request.
-
#request_params_all(request) ⇒ Object
Get and parse all request params.
-
#websocket_connection_authorized?(request) ⇒ Boolean
Check that client with that scope is authorized to open connection.
Constructor Details
#initialize(application, request) ⇒ Handler
Returns a new instance of Handler.
5 6 7 8 |
# File 'lib/webmate/routes/handler.rb', line 5 def initialize(application, request) @application = application @request = request end |
Instance Attribute Details
#application ⇒ Object
Returns the value of attribute application.
3 4 5 |
# File 'lib/webmate/routes/handler.rb', line 3 def application @application end |
#request ⇒ Object
Returns the value of attribute request.
3 4 5 |
# File 'lib/webmate/routes/handler.rb', line 3 def request @request end |
Instance Method Details
#handle(route_info) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/webmate/routes/handler.rb', line 10 def handle(route_info) if request.websocket? unless (request) return [401, {}, []] end session_id = route_info[:params][:session_id] Webmate::Websockets.subscribe(session_id, request) do |request| if route_info = application.routes.match(request[:method], 'WS', request.path) request_info = prepare_response_params(route_info, request) response = route_info[:responder].new(request_info).respond end end # this response not pass to user - so we keep connection alive. # passing other response will close connection and socket [-1, {}, []] else # HTTP request_info = prepare_response_params(route_info, request) response = route_info[:responder].new(request_info).respond response.to_rack end end |
#prepare_response_params(route_info, request) ⇒ Object
this method prepare data for responder from request
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/webmate/routes/handler.rb', line 37 def prepare_response_params(route_info, request) # create unified request info request_params = request_params_all(request) = request_params.delete(:metadata) { path: request.path, metadata: || {}, action: route_info[:action], params: request_params.merge(route_info[:params]), request: request } end |
#request_params_all(request) ⇒ Object
Get and parse all request params
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/webmate/routes/handler.rb', line 51 def request_params_all(request) request_params = HashWithIndifferentAccess.new request_params.merge!(request.params || {}) # read post or put params. this will erase params # {code: 123, mode: 123} # "code=123&mode=123" request_body = request.body.read if request_body.present? body_params = begin JSON.parse(request_body) # {code: 123, mode: 123} rescue JSON::ParserError Rack::Utils.parse_nested_query(request_body) # "code=123&mode=123" end else body_params = {} end request_params.merge(body_params) end |
#websocket_connection_authorized?(request) ⇒ Boolean
Check that client with that scope is authorized to open connection
73 74 75 |
# File 'lib/webmate/routes/handler.rb', line 73 def (request) true end |