Module: Vortex::Sinatra

Defined in:
lib/vortex/sinatra.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra application integration for Vortex SDK

This module provides the same route structure as other SDKs (Express, Java, Python) to ensure complete compatibility with React providers and frontend frameworks.

Usage in Sinatra app:

require 'sinatra/base'
require 'vortex/sinatra'

class MyApp < Sinatra::Base
  register Vortex::Sinatra

  configure do
    set :vortex_api_key, ENV['VORTEX_API_KEY']
    set :vortex_base_url, ENV['VORTEX_BASE_URL'] # optional
  end

  # Implement authentication callbacks
  def authenticate_vortex_user
    # Return user hash or nil
  end

  def authorize_vortex_operation(operation, user)
    # Return true/false
  end
end


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/vortex/sinatra.rb', line 35

def self.registered(app)
  app.helpers Helpers

  # Ensure these routes match exactly with other SDKs for React provider compatibility

  # Generate JWT for authenticated user
  # POST /api/vortex/jwt
  app.post '/api/vortex/jwt' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('JWT', user)
        return render_forbidden('Not authorized to generate JWT')
      end

      jwt = vortex_client.generate_jwt(
        user_id: user[:user_id],
        identifiers: user[:identifiers],
        groups: user[:groups],
        role: user[:role]
      )

      render_json({ jwt: jwt })
    end
  end

  # Get invitations by target
  # GET /api/vortex/invitations?targetType=email&[email protected]
  app.get '/api/vortex/invitations' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('GET_INVITATIONS', user)
        return render_forbidden('Not authorized to get invitations')
      end

      target_type = params['targetType']
      target_value = params['targetValue']

      unless target_type && target_value
        return render_bad_request('Missing targetType or targetValue')
      end

      invitations = vortex_client.get_invitations_by_target(target_type, target_value)
      render_json({ invitations: invitations })
    end
  end

  # Get specific invitation by ID
  # GET /api/vortex/invitations/:invitation_id
  app.get '/api/vortex/invitations/:invitation_id' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('GET_INVITATION', user)
        return render_forbidden('Not authorized to get invitation')
      end

      invitation_id = params['invitation_id']
      invitation = vortex_client.get_invitation(invitation_id)
      render_json(invitation)
    end
  end

  # Revoke (delete) invitation
  # DELETE /api/vortex/invitations/:invitation_id
  app.delete '/api/vortex/invitations/:invitation_id' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('REVOKE_INVITATION', user)
        return render_forbidden('Not authorized to revoke invitation')
      end

      invitation_id = params['invitation_id']
      vortex_client.revoke_invitation(invitation_id)
      render_json({ success: true })
    end
  end

  # Accept invitations
  # POST /api/vortex/invitations/accept
  app.post '/api/vortex/invitations/accept' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('ACCEPT_INVITATIONS', user)
        return render_forbidden('Not authorized to accept invitations')
      end

      request_body = parse_json_body

      invitation_ids = request_body['invitationIds']
      target = request_body['target']

      unless invitation_ids && target
        return render_bad_request('Missing invitationIds or target')
      end

      result = vortex_client.accept_invitations(invitation_ids, target)
      render_json(result)
    end
  end

  # Get invitations by group
  # GET /api/vortex/invitations/by-group/:group_type/:group_id
  app.get '/api/vortex/invitations/by-group/:group_type/:group_id' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('GET_GROUP_INVITATIONS', user)
        return render_forbidden('Not authorized to get group invitations')
      end

      group_type = params['group_type']
      group_id = params['group_id']

      invitations = vortex_client.get_invitations_by_group(group_type, group_id)
      render_json({ invitations: invitations })
    end
  end

  # Delete invitations by group
  # DELETE /api/vortex/invitations/by-group/:group_type/:group_id
  app.delete '/api/vortex/invitations/by-group/:group_type/:group_id' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('DELETE_GROUP_INVITATIONS', user)
        return render_forbidden('Not authorized to delete group invitations')
      end

      group_type = params['group_type']
      group_id = params['group_id']

      vortex_client.delete_invitations_by_group(group_type, group_id)
      render_json({ success: true })
    end
  end

  # Reinvite user
  # POST /api/vortex/invitations/:invitation_id/reinvite
  app.post '/api/vortex/invitations/:invitation_id/reinvite' do
    with_vortex_error_handling do
      user = authenticate_vortex_user
      return render_unauthorized('Authentication required') unless user

      unless authorize_vortex_operation('REINVITE', user)
        return render_forbidden('Not authorized to reinvite')
      end

      invitation_id = params['invitation_id']
      result = vortex_client.reinvite(invitation_id)
      render_json(result)
    end
  end

  # Configure Vortex client
  app.configure do
    unless app.respond_to?(:vortex_client)
      app.set :vortex_client, nil
    end
  end
end