Class: Rubyists::Leopard::NatsServiceDiscovery::Operation::SubjectMap

Inherits:
Trailblazer::Operation
  • Object
show all
Includes:
DiscoveryOptions
Defined in:
lib/leopard/nats_service_discovery/operation/subject_map.rb

Overview

Builds a subject-to-endpoint listener map from $SRV.INFO responses.

On success, the result exposes :responses and :subject_map.

Instance Method Summary collapse

Methods included from DiscoveryOptions

#operation_options

Instance Method Details

#add_listener(subjects, service, endpoint) ⇒ void (private)

This method returns an undefined value.

Adds an endpoint listener to the subject map.

Parameters:

  • subjects (Hash)

    Subject map accumulator.

  • service (Hash)

    Parsed $SRV.INFO service response.

  • endpoint (Hash)

    Endpoint payload from the service response.



55
56
57
58
59
60
61
# File 'lib/leopard/nats_service_discovery/operation/subject_map.rb', line 55

def add_listener(subjects, service, endpoint)
  subject = endpoint['subject']
  return if subject.to_s.empty?

  subjects[subject] ||= []
  subjects[subject] << listener_entry(service, endpoint)
end

#listener_entry(service, endpoint) ⇒ Hash (private)

Builds a subject-map listener entry.

Parameters:

  • service (Hash)

    Parsed $SRV.INFO service response.

  • endpoint (Hash)

    Endpoint payload from the service response.

Returns:

  • (Hash)

    Listener entry suitable for subject maps.



69
70
71
72
73
74
75
76
77
78
# File 'lib/leopard/nats_service_discovery/operation/subject_map.rb', line 69

def listener_entry(service, endpoint)
  {
    'service' => service['name'],
    'service_id' => service['id'],
    'version' => service['version'],
    'endpoint' => endpoint['name'],
    'queue_group' => endpoint['queue_group'],
    'metadata' => endpoint['metadata'],
  }
end

#load_info?(ctx) ⇒ Boolean

Loads raw service info responses for mapping.

Parameters:

  • ctx (Hash)

    Operation context.

Returns:

  • (Boolean)

    Whether service info was loaded.



25
26
27
28
29
30
# File 'lib/leopard/nats_service_discovery/operation/subject_map.rb', line 25

def load_info?(ctx, **)
  result = Info.call(**operation_options(ctx))
  ctx[:error] = result[:error] unless result.success?
  ctx[:responses] = result[:responses]
  result.success?
end

#map_subjects(ctx, responses:) ⇒ Hash

Builds a subject-to-listeners map from info responses.

Parameters:

  • ctx (Hash)

    Operation context.

  • responses (Array<Hash>)

    Parsed $SRV.INFO responses.

Returns:

  • (Hash)

    Listener entries keyed by NATS subject.



38
39
40
41
42
43
44
# File 'lib/leopard/nats_service_discovery/operation/subject_map.rb', line 38

def map_subjects(ctx, responses:, **)
  ctx[:subject_map] = responses.each_with_object({}) do |service, subjects|
    Array(service['endpoints']).each do |endpoint|
      add_listener(subjects, service, endpoint)
    end
  end
end