Module: Strum::Esb::Handler::ClassMethods

Defined in:
lib/strum/esb/handler.rb

Instance Method Summary collapse

Instance Method Details

#bind_to(queue, message_type, message_binding, handler = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/strum/esb/handler.rb', line 19

def bind_to(queue, message_type, message_binding, handler = nil)
  bindings ||= (queue_opts && queue_opts[:bindings]) || {}

  if block_given?
    yield(bindings)
  else
    bindings[message_type] ||= []
    bindings[message_type] << message_binding
  end

  from_queue queue, bindings: bindings
  handlers[handler_key(message_type, message_binding)] = handler.to_s if handler
end

#content_type_enabled?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
# File 'lib/strum/esb/handler.rb', line 59

def content_type_enabled?(content_type)
  case content_type
  when "application/json"
    json_enabled?
  when "application/x-protobuf"
    protobuf_enabled?
  else
    false
  end
end

#enable_json(switcher) ⇒ Object



43
44
45
# File 'lib/strum/esb/handler.rb', line 43

def enable_json(switcher)
  @json_mode = switcher
end

#enable_protobuf(switcher) ⇒ Object



51
52
53
# File 'lib/strum/esb/handler.rb', line 51

def enable_protobuf(switcher)
  @protobuf_mode = switcher
end

#handler_key(message_type, message_binding) ⇒ Object



33
34
35
36
37
# File 'lib/strum/esb/handler.rb', line 33

def handler_key(message_type, message_binding)
  _, *msg = Strum::Esb::Functions.public_send("#{message_type}_explain", message_binding)
  params = msg.map { |param| param&.to_s&.gsub(/[^a-zA-Z0-9]/, "_")&.downcase }
  ([message_type] + params).join("-")
end

#handlersObject



39
40
41
# File 'lib/strum/esb/handler.rb', line 39

def handlers
  @handlers ||= {}
end

#json_enabled?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/strum/esb/handler.rb', line 47

def json_enabled?
  @json_mode
end

#protobuf_enabled?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/strum/esb/handler.rb', line 55

def protobuf_enabled?
  @protobuf_mode
end

#protobuf_serviceObject



77
78
79
# File 'lib/strum/esb/handler.rb', line 77

def protobuf_service
  @protobuf_service
end

#use_application_configObject



81
82
83
84
# File 'lib/strum/esb/handler.rb', line 81

def use_application_config
  enable_json(Strum::Esb.config.enable_json)
  enable_protobuf(Strum::Esb.config.enable_protobuf)
end

#use_protobuf_service(service) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
# File 'lib/strum/esb/handler.rb', line 70

def use_protobuf_service(service)
  raise ArgumentError, "#{service} must be a grpc service" unless service.respond_to?(:rpc_descs)

  @protobuf_service = service
  enable_protobuf(true)
end