Skip to content
Kward Search API index

Class: Kward::Transport::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/transport/host.rb

Overview

Public dependencies made available to a running transport adapter.

Defined Under Namespace

Classes: AllowAllPolicy, Interactions, PluginChat, PluginChatHandle, PluginChatTurn, PluginChatTurnHandle, PluginChats, PolicyDenied, Session, SessionHandle, Sessions, Turn, TurnHandle, UnavailableGateway

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport_id:, gateway: nil, plugin_chat_gateway: nil, config: {}, storage: nil, policy: nil, logger: nil, execution_profile: nil) ⇒ Host

Creates a host for one registered transport instance.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kward/transport/host.rb', line 17

def initialize(transport_id:, gateway: nil, plugin_chat_gateway: nil, config: {}, storage: nil, policy: nil, logger: nil, execution_profile: nil)
  unless execution_profile.nil? || execution_profile.is_a?(ExecutionProfile)
    raise ArgumentError, "execution_profile must be a Transport::ExecutionProfile"
  end

  @transport_id = transport_id.to_s.freeze
  @gateway = gateway
  @plugin_chat_gateway = plugin_chat_gateway
  @config = freeze_copy(config)
  @storage = storage || Store.new(@transport_id)
  @policy = policy || AllowAllPolicy.new
  @execution_profile = execution_profile
  @logger = logger || Logger.new($stderr)
  @sessions = Sessions.new(self)
  @plugin_chats = PluginChats.new(self)
  @interactions = Interactions.new(self)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



35
36
37
# File 'lib/kward/transport/host.rb', line 35

def config
  @config
end

#execution_profileObject (readonly)

Returns the value of attribute execution_profile.



35
36
37
# File 'lib/kward/transport/host.rb', line 35

def execution_profile
  @execution_profile
end

#loggerObject (readonly)

Returns the value of attribute logger.



35
36
37
# File 'lib/kward/transport/host.rb', line 35

def logger
  @logger
end

#policyObject (readonly)

Returns the value of attribute policy.



35
36
37
# File 'lib/kward/transport/host.rb', line 35

def policy
  @policy
end

#storageObject (readonly)

Returns the value of attribute storage.



35
36
37
# File 'lib/kward/transport/host.rb', line 35

def storage
  @storage
end

#transport_idObject (readonly)

Returns the value of attribute transport_id.



35
36
37
# File 'lib/kward/transport/host.rb', line 35

def transport_id
  @transport_id
end

Instance Method Details

#authorize!(action, **attributes) ⇒ Object

Raises:



55
56
57
58
59
# File 'lib/kward/transport/host.rb', line 55

def authorize!(action, **attributes)
  return true if policy.authorize(action: action, transport_id: @transport_id, **attributes) == true

  raise PolicyDenied, "Transport policy denied #{action}"
end

#gatewayObject

Raises:



49
50
51
52
53
# File 'lib/kward/transport/host.rb', line 49

def gateway
  return @gateway if @gateway

  raise UnavailableGateway, "transport host has no session gateway"
end

#interactionsObject



41
42
43
# File 'lib/kward/transport/host.rb', line 41

def interactions
  @interactions
end

#plugin_chat_gatewayObject

Raises:



61
62
63
64
65
# File 'lib/kward/transport/host.rb', line 61

def plugin_chat_gateway
  return @plugin_chat_gateway if @plugin_chat_gateway

  raise UnavailableGateway, "transport host has no plugin chat gateway"
end

#plugin_chatsObject



45
46
47
# File 'lib/kward/transport/host.rb', line 45

def plugin_chats
  @plugin_chats
end

#secret(key, env: nil) ⇒ Object

Reads a transport secret from private config or an explicit environment variable without exposing it through status or logging helpers.

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
# File 'lib/kward/transport/host.rb', line 69

def secret(key, env: nil)
  key = key.to_s
  raise ArgumentError, "secret key is required" if key.empty?

  value = config[key]
  value = ENV.fetch(env.to_s) if value.nil? && env
  value = ENV[default_secret_env_name(key)] if value.nil?
  value.to_s unless value.nil?
end

#sessionsObject



37
38
39
# File 'lib/kward/transport/host.rb', line 37

def sessions
  @sessions
end

#shutdownObject



79
80
81
82
83
# File 'lib/kward/transport/host.rb', line 79

def shutdown
  @gateway&.shutdown
  @plugin_chat_gateway&.shutdown
  nil
end