Updated

lib/context_request_subscriber / processor.rb

A
59 lines of codes
5 methods
6.5 complexity/method
2 churn
32.41 complexity
0 duplications
# frozen_string_literal: true module ContextRequestSubscriber # :nodoc: module Processor
  1. ContextRequestSubscriber::Processor has no descriptive comment
autoload :Base, 'context_request_subscriber/processor/base' autoload :Request, 'context_request_subscriber/processor/request' autoload :Context, 'context_request_subscriber/processor/context' # Class to execute the processing of the messages. # Base on the message_type a processor is instantiated and provides # processing. class Executor attr_accessor :handler_params
  1. ContextRequestSubscriber::Processor::Executor#handler_params is a writable attribute
def initialize(logger, **keys) @logger = logger @handler_params = keys[:handler_params] || {} end def run(_delivery_info, properties, payload)
  1. ContextRequestSubscriber::Processor::Executor#run has approx 7 statements
@logger.debug("#{properties[:type]} processing message.")
  1. ContextRequestSubscriber::Processor::Executor#run calls 'properties[:type]' 2 times Locations: 0 1
@logger.debug(" payload: #{payload}") @logger.debug(" properties: #{properties}") payload = JSON.parse(payload) process(properties[:type], properties[:headers]&.dig('version'),
  1. ContextRequestSubscriber::Processor::Executor#run calls 'properties[:type]' 2 times Locations: 0 1
  2. ContextRequestSubscriber::Processor::Executor#run performs a nil-check
payload) rescue JSON::ParserError @logger.error("Could not parse message payload \ with payload #{payload}. Ignoring.") nil end def process(name, version = nil, payload = {}) processor = Processor.processor_class(name, version)
  1. ContextRequestSubscriber::Processor::Executor#process performs a nil-check
&.new(@logger, handler_params: handler_params) if processor processor.call(payload) else @logger.error("Invalid processor type or \ processor type with name #{name} cannot be found") end end # Method can be overwriten to communicate with the MQ and ack the message # if processing was successful. # Default is to do nothing because the setup is auto_ack. def ack(_channel); end end def self.processor_class(name, version = nil) version = "V#{version}" if version [self.name, version, name.tr('.', '_').camelize].compact.join('::') .constantize rescue NameError nil end end end