class Grumlin::RequestDispatcher
Constants
- ERRORS
- SUCCESS
Attributes
requests[R]
Public Class Methods
new()
click to toggle source
# File lib/grumlin/request_dispatcher.rb, line 27 def initialize @requests = {} end
Public Instance Methods
add_request(request)
click to toggle source
# File lib/grumlin/request_dispatcher.rb, line 31 def add_request(request) raise "ERROR" if @requests.key?(request[:requestId]) Async::Channel.new.tap do |channel| @requests[request[:requestId]] = { request: request, result: [], channel: channel } end end
add_response(response)
click to toggle source
builds a response object, when it's ready sends it to the client via a channel TODO: sometimes response does not include requestID, no idea how to handle it so far.
# File lib/grumlin/request_dispatcher.rb, line 41 def add_response(response) # rubocop:disable Metrics/AbcSize request_id = response[:requestId] raise "ERROR" unless ongoing_request?(request_id) request = @requests[request_id] check_errors!(response[:status], request[:request]) case SUCCESS[response.dig(:status, :code)] when :success request[:channel] << [*request[:result], response.dig(:result, :data)] close_request(request_id) when :partial_content then request[:result] << response.dig(:result, :data) when :no_content request[:channel] << [] close_request(request_id) end rescue StandardError => e request[:channel].exception(e) close_request(request_id) end
clear()
click to toggle source
# File lib/grumlin/request_dispatcher.rb, line 74 def clear @requests.clear end
close_request(request_id)
click to toggle source
# File lib/grumlin/request_dispatcher.rb, line 63 def close_request(request_id) raise "ERROR" unless ongoing_request?(request_id) request = @requests.delete(request_id) request[:channel].close end
ongoing_request?(request_id)
click to toggle source
# File lib/grumlin/request_dispatcher.rb, line 70 def ongoing_request?(request_id) @requests.key?(request_id) end
Private Instance Methods
check_errors!(status, query)
click to toggle source
# File lib/grumlin/request_dispatcher.rb, line 80 def check_errors!(status, query) if (error = ERRORS[status[:code]]) raise error.new(status, query) end return unless SUCCESS[status[:code]].nil? raise(UnknownResponseStatus, status) end