Module: Legion::API::Routes::Gaia

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

Defined Under Namespace

Modules: GaiaHelpers

Class Method Summary collapse

Class Method Details

.register_buffer_route(app) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/api/gaia.rb', line 54

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/api/gaia.rb', line 36

def self.register_channels_route(app)
  app.helpers GaiaHelpers

  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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/api/gaia.rb', line 67

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



26
27
28
29
30
31
32
33
34
# File 'lib/legion/api/gaia.rb', line 26

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/legion/api/gaia.rb', line 79

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

    adapter = Routes::Gaia.teams_adapter
    unless adapter
      Legion::Logging.warn 'API POST /api/channels/teams/webhook returned 503: teams adapter not available'
      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}"
    json_response({ status: 'accepted', frame_id: input_frame&.id })
  end
end

.register_ticks_route(app) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/legion/api/gaia.rb', line 16

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

    limit = (params[:limit] || 50).to_i.clamp(1, 200)
    events = defined?(Legion::Gaia) ? Legion::Gaia.tick_history&.recent(limit: limit) : []
    json_response({ events: events || [] })
  end
end

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/legion/api/gaia.rb', line 7

def self.registered(app)
  register_status_route(app)
  register_ticks_route(app)
  register_channels_route(app)
  register_buffer_route(app)
  register_sessions_route(app)
  register_teams_webhook_route(app)
end

.teams_adapterObject



98
99
100
101
102
103
104
105
106
# File 'lib/legion/api/gaia.rb', line 98

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