Module: Legion::Gaia::Routes

Defined in:
lib/legion/gaia/routes.rb

Class Method Summary collapse

Class Method Details

.register_buffer_route(app) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/legion/gaia/routes.rb', line 90

def self.register_buffer_route(app)
  app.get '/api/gaia/buffer' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) unless gaia_available?

    buffer = Legion::Gaia.sensory_buffer
    json_response({
                    depth: buffer&.size || 0,
                    empty: buffer.nil? || buffer.empty?,
                    max_size: gaia_buffer_max_size
                  })
  end
end

.register_channels_route(app) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/gaia/routes.rb', line 74

def self.register_channels_route(app)
  app.get '/api/gaia/channels' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) unless gaia_available?

    registry = Legion::Gaia.channel_registry
    return json_response({ channels: [] }) unless registry

    channels = registry.active_channels.map do |ch_id|
      adapter = registry.adapter_for(ch_id)
      build_channel_info(ch_id, adapter)
    end

    json_response({ channels: channels, count: channels.size })
  end
end

.register_sessions_route(app) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/legion/gaia/routes.rb', line 103

def self.register_sessions_route(app)
  app.get '/api/gaia/sessions' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) unless gaia_available?

    store = Legion::Gaia.session_store
    json_response({
                    count: store&.size || 0,
                    active: gaia_available?
                  })
  end
end

.register_status_route(app) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/legion/gaia/routes.rb', line 64

def self.register_status_route(app)
  app.get '/api/gaia/status' do
    if gaia_available?
      json_response(Legion::Gaia.status)
    else
      json_response({ started: false }, status_code: 503)
    end
  end
end

.register_teams_webhook_route(app) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/legion/gaia/routes.rb', line 115

def self.register_teams_webhook_route(app)
  app.post '/api/channels/teams/webhook' do
    if defined?(Legion::Logging)
      Legion::Logging.debug "API: POST /api/channels/teams/webhook params=#{params.keys}"
    end
    body = request.body.read
    activity = Legion::JSON.load(body)

    adapter = Routes.teams_adapter
    unless adapter
      if defined?(Legion::Logging)
        Legion::Logging.warn 'API POST /api/channels/teams/webhook returned 503: teams adapter not available'
      end
      halt 503, json_response({ error: 'teams adapter not available' }, status_code: 503)
    end

    input_frame = adapter.translate_inbound(activity)
    Legion::Gaia.sensory_buffer&.push(input_frame) if defined?(Legion::Gaia)
    Legion::Logging.info "API: accepted Teams webhook frame_id=#{input_frame&.id}" if defined?(Legion::Logging)
    json_response({ status: 'accepted', frame_id: input_frame&.id })
  end
end

.registered(app) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/legion/gaia/routes.rb', line 13

def self.registered(app) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  app.helpers do # rubocop:disable Metrics/BlockLength
    unless method_defined?(:gaia_available?)
      define_method(:gaia_available?) do
        defined?(Legion::Gaia) && Legion::Gaia.respond_to?(:started?) && Legion::Gaia.started?
      end
    end

    unless method_defined?(:gaia_buffer_max_size)
      define_method(:gaia_buffer_max_size) do
        return nil unless defined?(Legion::Gaia::SensoryBuffer)

        Legion::Gaia::SensoryBuffer::MAX_BUFFER_SIZE
      rescue NameError
        nil
      end
    end

    unless method_defined?(:build_channel_info)
      define_method(:build_channel_info) do |channel_id, adapter|
        info = { id: channel_id, started: adapter&.started? || false }
        info[:capabilities] = adapter.capabilities if adapter.respond_to?(:capabilities)
        info[:type] = adapter.class.name.split('::').last if adapter
        info
      end
    end

    unless method_defined?(:json_response)
      define_method(:json_response) do |data, status_code: 200|
        content_type :json
        status status_code
        Legion::JSON.dump({ data: data })
      end
    end

    unless method_defined?(:json_error)
      define_method(:json_error) do |code, message, status_code: 400|
        content_type :json
        status status_code
        Legion::JSON.dump({ error: { code: code, message: message } })
      end
    end
  end

  register_status_route(app)
  register_channels_route(app)
  register_buffer_route(app)
  register_sessions_route(app)
  register_teams_webhook_route(app)
end

.teams_adapterObject



138
139
140
141
142
143
144
145
146
# File 'lib/legion/gaia/routes.rb', line 138

def self.teams_adapter
  return nil unless defined?(Legion::Gaia) && Legion::Gaia.respond_to?(:channel_registry)
  return nil unless Legion::Gaia.channel_registry

  Legion::Gaia.channel_registry.adapter_for(:teams)
rescue StandardError => e
  Legion::Logging.warn "Gaia#teams_adapter failed: #{e.message}" if defined?(Legion::Logging)
  nil
end