Class: Yes::Core::ProcessManagers::ServiceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/core/process_managers/service_client.rb

Overview

Handles communication with external command API services.

Examples:

client = Yes::Core::ProcessManagers::ServiceClient.new('my_service')
client.call(access_token: token, commands_data: [...], channel: '/pm/channel')

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ ServiceClient

Initializes a new ServiceClient.

Parameters:

  • service (String)

    the name of the service to connect to



19
20
21
22
23
24
# File 'lib/yes/core/process_managers/service_client.rb', line 19

def initialize(service)
  @service_url = ENV.fetch(
    "#{service.upcase}_SERVICE_URL",
    "http://#{service.underscore.tr('_', '-')}-cluster-ip-service:3000"
  )
end

Instance Method Details

#call(access_token: nil, commands_data: [], channel: nil) ⇒ Faraday::Response

Sends commands to the service.

Parameters:

  • access_token (String) (defaults to: nil)

    JWT token for authentication

  • commands_data (Array<Hash>) (defaults to: [])

    array of command data to be sent

  • channel (String) (defaults to: nil)

    the channel to send command notifications to

Returns:

  • (Faraday::Response)

    the response from the service

Raises:

  • (ArgumentError)

    if access_token or channel is nil



33
34
35
36
37
38
39
40
# File 'lib/yes/core/process_managers/service_client.rb', line 33

def call(access_token: nil, commands_data: [], channel: nil)
  raise ArgumentError, 'channel and access_token is required' if access_token.nil? || channel.nil?

  connection(access_token).post do |req|
    req.url '/v1/commands'
    req.body = { channel:, commands: commands_data }
  end
end