Sha256: 92b540c9cd9051e5a42cc5d3213945cf7f41e337144bbbb8d8457c07eec2af9c
Contents?: true
Size: 1.87 KB
Versions: 18
Compression:
Stored size: 1.87 KB
Contents
module NxtHttpClient class ResponseHandler CallbackAlreadyRegistered = Class.new(StandardError) def initialize @callbacks = {} @result = nil end attr_accessor :result attr_reader :callbacks def eval_callback(target, key, response) return unless callbacks[key] target.instance_exec(response, &callbacks[key]) end def configure(&block) tap { |handler| block.call(handler) } end def register_callback(code, overwrite: false, &block) key = code.to_s # This would add callbacks to the response handler unless overwrite callbacks[key].present? && raise_callback_already_registered(code) end callbacks[key] = block end def register_callback!(code, &block) register_callback(code, overwrite: true, &block) end alias_method :on, :register_callback alias_method :on!, :register_callback! def callback_for_response(response) key_from_response = response.code.to_s return callbacks['any'] if callbacks['any'].present? first_matching_key = callbacks.keys.sort.reverse.find do |key| regex_key = key.gsub('*', '[0-9]{1}') key_from_response =~ /\A#{regex_key}\z/ end first_matching_key && callbacks[first_matching_key] || response.success? && callbacks['success'] || response.timed_out? && callbacks['timed_out'] || !response.success? && callbacks['error'] || callbacks['others'] end private def raise_callback_already_registered(code) msg = "Callback already registered for status: #{code}." msg << ' Use bang method to overwrite the callback.' raise CallbackAlreadyRegistered, msg end # we need to dup callbacks since dup is shallow def initialize_copy(original) super @callbacks = original.send(:callbacks).dup @result = nil end end end
Version data entries
18 entries across 18 versions & 1 rubygems