rapitapir 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +5 -0
  3. data/CHANGELOG.md +29 -1
  4. data/README.md +23 -4
  5. data/RUBY_WEEKLY_LAUNCH_POST.md +43 -62
  6. data/docs/adr/0001-base-class-strategy.md +33 -0
  7. data/docs/adr/0002-type-shortcut-and-type-system.md +30 -0
  8. data/docs/adr/0003-openapi-source-of-truth.md +32 -0
  9. data/docs/adr/0004-client-generation-typescript-first.md +34 -0
  10. data/docs/adr/0005-auth-architecture.md +32 -0
  11. data/docs/adr/0006-observability-defaults.md +30 -0
  12. data/docs/adr/0007-ai-scope-and-boundaries.md +30 -0
  13. data/docs/adr/README.md +46 -0
  14. data/docs/adr/template.md +24 -0
  15. data/docs/blueprint.md +92 -4
  16. data/docs/implementation-status.md +1 -1
  17. data/examples/auto_derivation_ruby_friendly.rb +44 -113
  18. data/examples/cli/user_api_endpoints.rb +63 -36
  19. data/examples/demo_api.rb +57 -18
  20. data/examples/enterprise_extension_demo.rb +13 -13
  21. data/examples/enterprise_rapitapir_api.rb +81 -93
  22. data/examples/nocodb/nocodb_scaffold/Gemfile +22 -0
  23. data/examples/nocodb/nocodb_scaffold/README.md +10 -0
  24. data/examples/nocodb/nocodb_scaffold/Rakefile +7 -0
  25. data/examples/nocodb/nocodb_scaffold/app/api/app.rb +1139 -0
  26. data/examples/nocodb/nocodb_scaffold/app/models/email_template.rb +6 -0
  27. data/examples/nocodb/nocodb_scaffold/app/models/faq.rb +6 -0
  28. data/examples/nocodb/nocodb_scaffold/app/models/feature.rb +6 -0
  29. data/examples/nocodb/nocodb_scaffold/app/models/idea.rb +6 -0
  30. data/examples/nocodb/nocodb_scaffold/app/models/itinerary_day.rb +6 -0
  31. data/examples/nocodb/nocodb_scaffold/app/models/route.rb +6 -0
  32. data/examples/nocodb/nocodb_scaffold/app/models/subscriber.rb +6 -0
  33. data/examples/nocodb/nocodb_scaffold/app/models/tour.rb +6 -0
  34. data/examples/nocodb/nocodb_scaffold/app/models/webhook.rb +6 -0
  35. data/examples/nocodb/nocodb_scaffold/app/models_loader.rb +3 -0
  36. data/examples/nocodb/nocodb_scaffold/config/database.yml +11 -0
  37. data/examples/nocodb/nocodb_scaffold/config.ru +9 -0
  38. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152012_create_features.rb +10 -0
  39. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152013_create_tours.rb +10 -0
  40. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152014_create_routes.rb +10 -0
  41. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152015_create_itinerary_days.rb +10 -0
  42. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152016_create_subscribers.rb +10 -0
  43. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152017_create_email_templates.rb +10 -0
  44. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152018_create_faqs.rb +10 -0
  45. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152019_create_webhooks.rb +10 -0
  46. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152020_create_ideas.rb +10 -0
  47. data/examples/nocodb/swagger.json +4378 -0
  48. data/examples/production_ready_example.rb +78 -58
  49. data/examples/simple_demo_api.rb +40 -7
  50. data/examples/user_api.rb +130 -132
  51. data/lib/rapitapir/auth/oauth2.rb +1 -1
  52. data/lib/rapitapir/cli/command.rb +30 -4
  53. data/lib/rapitapir/cli/scaffold_generator.rb +639 -0
  54. data/lib/rapitapir/cli/validator.rb +1 -1
  55. data/lib/rapitapir/core/enhanced_endpoint.rb +1 -1
  56. data/lib/rapitapir/dsl/enhanced_structures.rb +1 -1
  57. data/lib/rapitapir/openapi/schema_generator.rb +46 -2
  58. data/lib/rapitapir/server/enhanced_rack_adapter.rb +11 -8
  59. data/lib/rapitapir/server/middleware.rb +2 -2
  60. data/lib/rapitapir/types/auto_derivation.rb +2 -4
  61. data/lib/rapitapir/version.rb +1 -1
  62. data/rapitapir.gemspec +3 -2
  63. metadata +70 -8
@@ -13,6 +13,7 @@
13
13
 
14
14
  require 'sinatra/base'
15
15
  require 'json'
16
+ require 'rack'
16
17
  require_relative '../lib/rapitapir'
17
18
  require_relative '../lib/rapitapir/server/sinatra_adapter'
18
19
 
@@ -313,79 +314,43 @@ end
313
314
 
314
315
  # Main Sinatra Application
315
316
  class EnterpriseTaskAPI < Sinatra::Base
316
- def initialize
317
- super
318
-
319
- configure do
320
- set :show_exceptions, false
321
- set :raise_errors, false
322
- set :dump_errors, false
323
- end
324
-
325
- # Setup authentication scheme
326
- bearer_auth = RapiTapir::Auth.bearer_token(:bearer, {
327
- realm: 'Enterprise Task Management API',
328
- token_validator: proc do |token|
329
- user = UserDatabase.find_by_token(token)
330
- next nil unless user
331
-
332
- {
333
- user: user,
334
- scopes: user[:scopes]
335
- }
336
- end
337
- })
338
-
339
- auth_schemes = { bearer: bearer_auth }
340
-
341
- # Setup middleware stack
342
- use RapiTapir::Auth::Middleware::SecurityHeadersMiddleware
343
- use RapiTapir::Auth::Middleware::CorsMiddleware, {
344
- allowed_origins: ['http://localhost:3000', 'https://app.example.com'],
345
- allowed_methods: %w[GET POST PUT DELETE PATCH OPTIONS],
346
- allowed_headers: %w[Authorization Content-Type Accept],
347
- allow_credentials: true
348
- }
349
- use RapiTapir::Auth::Middleware::RateLimitingMiddleware, {
350
- requests_per_minute: 100,
351
- requests_per_hour: 2000
352
- }
353
- use RapiTapir::Auth::Middleware::AuthenticationMiddleware, auth_schemes
354
-
355
- # Setup RapiTapir adapter and register endpoints
356
- setup_rapitapir_endpoints
357
- end
358
-
359
- # Helper methods
360
- def json_response(status, data)
361
- content_type :json
362
- halt status, JSON.generate(data)
363
- end
364
-
365
- def require_scope(scope)
366
- return if RapiTapir::Auth.has_scope?(scope)
367
-
368
- json_response(403, { error: "#{scope.capitalize} permission required" })
369
- end
370
-
371
- def require_authenticated
372
- return if RapiTapir::Auth.authenticated?
373
-
374
- json_response(401, { error: 'Authentication required' })
375
- end
376
-
377
- def parse_json_body
378
- if request.content_type&.include?('application/json') && request.body.read.length.positive?
379
- request.body.rewind
380
- JSON.parse(request.body.read, symbolize_names: true)
381
- else
382
- {}
383
- end
384
- rescue JSON::ParserError
385
- json_response(400, { error: 'Invalid JSON' })
386
- end
317
+ set :show_exceptions, false
318
+ set :raise_errors, false
319
+ set :dump_errors, false
320
+
321
+ # Setup authentication scheme
322
+ bearer_auth = RapiTapir::Auth.bearer_token(:bearer, {
323
+ realm: 'Enterprise Task Management API',
324
+ token_validator: proc do |token|
325
+ user = UserDatabase.find_by_token(token)
326
+ next nil unless user
327
+
328
+ {
329
+ user: user,
330
+ scopes: user[:scopes]
331
+ }
332
+ end
333
+ })
387
334
 
388
- def format_task(task)
335
+ auth_schemes = { bearer: bearer_auth }
336
+
337
+ # Setup middleware stack
338
+ use RapiTapir::Auth::Middleware::SecurityHeadersMiddleware
339
+ use RapiTapir::Auth::Middleware::CorsMiddleware, {
340
+ allowed_origins: ['http://localhost:3000', 'https://app.example.com'],
341
+ allowed_methods: %w[GET POST PUT DELETE PATCH OPTIONS],
342
+ allowed_headers: %w[Authorization Content-Type Accept],
343
+ allow_credentials: true
344
+ }
345
+ use RapiTapir::Auth::Middleware::RateLimitingMiddleware, {
346
+ requests_per_minute: 100,
347
+ requests_per_hour: 2000
348
+ }
349
+ use RapiTapir::Auth::Middleware::AuthenticationMiddleware, auth_schemes
350
+
351
+ # --- helpers removed: see class-level json_response/enforce!/format_task ---
352
+
353
+ def self.format_task(task)
389
354
  task_copy = task.dup
390
355
  task_copy[:created_at] = task_copy[:created_at].iso8601 if task_copy[:created_at]
391
356
  task_copy[:updated_at] = task_copy[:updated_at].iso8601 if task_copy[:updated_at]
@@ -394,16 +359,36 @@ class EnterpriseTaskAPI < Sinatra::Base
394
359
 
395
360
  private
396
361
 
397
- def setup_rapitapir_endpoints
362
+ def self.setup_rapitapir_endpoints
363
+ # Bind the adapter to the app class: route DSL (get/post/...) is a class
364
+ # method, and handlers instance_exec'd against the class can use the
365
+ # class-level helpers below (json_response, enforce!, format_task).
398
366
  adapter = RapiTapir::Server::SinatraAdapter.new(self)
399
367
 
400
368
  # Register all endpoints using the adapter
401
369
  TaskAPI.endpoints.each do |endpoint|
402
370
  adapter.register_endpoint(endpoint, get_endpoint_handler(endpoint))
403
371
  end
372
+
373
+ adapter
374
+ end
375
+
376
+ # Helper methods (class-level; the adapter runs handlers in class context).
377
+ # Handlers return either a plain data hash (200 + JSON) or a
378
+ # [status, headers, body] triple, which the adapter translates directly.
379
+ def self.json_response(status, data)
380
+ [status, { 'Content-Type' => 'application/json' }, JSON.generate(data)]
404
381
  end
405
382
 
406
- def get_endpoint_handler(endpoint)
383
+ # Returns nil when the scope is satisfied, otherwise a 401/403 response triple.
384
+ def self.enforce!(scope)
385
+ return json_response(401, { error: 'Authentication required' }) unless RapiTapir::Auth.authenticated?
386
+ return json_response(403, { error: "#{scope.capitalize} permission required" }) unless RapiTapir::Auth.scope?(scope)
387
+
388
+ nil
389
+ end
390
+
391
+ def self.get_endpoint_handler(endpoint)
407
392
  case endpoint.path
408
393
  when '/health'
409
394
  proc do |_inputs|
@@ -420,8 +405,8 @@ class EnterpriseTaskAPI < Sinatra::Base
420
405
  when '/api/v1/tasks'
421
406
  if endpoint.method == :get
422
407
  proc do |inputs|
423
- require_authenticated
424
- require_scope('read')
408
+ guard = enforce!('read')
409
+ return guard if guard
425
410
 
426
411
  tasks = TaskDatabase.all
427
412
 
@@ -440,8 +425,8 @@ class EnterpriseTaskAPI < Sinatra::Base
440
425
  end
441
426
  else # POST
442
427
  proc do |inputs|
443
- require_authenticated
444
- require_scope('write')
428
+ guard = enforce!('write')
429
+ return guard if guard
445
430
 
446
431
  body = inputs[:body] || {}
447
432
 
@@ -463,11 +448,11 @@ class EnterpriseTaskAPI < Sinatra::Base
463
448
  case endpoint.method
464
449
  when :get
465
450
  proc do |inputs|
466
- require_authenticated
467
- require_scope('read')
451
+ guard = enforce!('read')
452
+ return guard if guard
468
453
 
469
454
  task = TaskDatabase.find(inputs[:id])
470
- halt 404, { error: 'Task not found' }.to_json unless task
455
+ return json_response(404, { error: 'Task not found' }) unless task
471
456
 
472
457
  # Enrich with assignee details
473
458
  assignee = UserDatabase.find_by_id(task[:assignee_id])
@@ -484,11 +469,11 @@ class EnterpriseTaskAPI < Sinatra::Base
484
469
  end
485
470
  when :put
486
471
  proc do |inputs|
487
- require_authenticated
488
- require_scope('write')
472
+ guard = enforce!('write')
473
+ return guard if guard
489
474
 
490
475
  task = TaskDatabase.find(inputs[:id])
491
- halt 404, { error: 'Task not found' }.to_json unless task
476
+ return json_response(404, { error: 'Task not found' }) unless task
492
477
 
493
478
  body = inputs[:body] || {}
494
479
  update_data = {}
@@ -505,21 +490,21 @@ class EnterpriseTaskAPI < Sinatra::Base
505
490
  end
506
491
  when :delete
507
492
  proc do |inputs|
508
- require_authenticated
509
- require_scope('admin')
493
+ guard = enforce!('admin')
494
+ return guard if guard
510
495
 
511
496
  task = TaskDatabase.find(inputs[:id])
512
- halt 404, { error: 'Task not found' }.to_json unless task
497
+ return json_response(404, { error: 'Task not found' }) unless task
513
498
 
514
499
  TaskDatabase.delete(inputs[:id])
515
- status 204
516
- nil # Return nothing for 204 No Content
500
+ [204, {}, '']
517
501
  end
518
502
  end
519
503
 
520
504
  when '/api/v1/profile'
521
505
  proc do |_inputs|
522
- require_authenticated
506
+ guard = enforce!('read')
507
+ return guard if guard
523
508
 
524
509
  current_user = RapiTapir::Auth.current_user
525
510
 
@@ -539,15 +524,15 @@ class EnterpriseTaskAPI < Sinatra::Base
539
524
 
540
525
  when '/api/v1/admin/users'
541
526
  proc do |_inputs|
542
- require_authenticated
543
- require_scope('admin')
527
+ guard = enforce!('admin')
528
+ return guard if guard
544
529
 
545
530
  UserDatabase.all_users
546
531
  end
547
532
 
548
533
  else
549
534
  proc do |_inputs|
550
- halt 404, { error: 'Endpoint not implemented' }.to_json
535
+ json_response(404, { error: 'Endpoint not implemented' })
551
536
  end
552
537
  end
553
538
  end
@@ -656,6 +641,9 @@ class EnterpriseTaskAPI < Sinatra::Base
656
641
  puts '🔧 Architecture: Routes handled by SinatraAdapter with automatic input/output validation'
657
642
  puts ''
658
643
  end
644
+
645
+ # Now that get_endpoint_handler is defined, wire up the adapter + routes
646
+ setup_rapitapir_endpoints
659
647
  end
660
648
 
661
649
  # Start the server if this file is run directly
@@ -0,0 +1,22 @@
1
+ source 'https://rubygems.org'
2
+
3
+ ruby '>= 3.2.0'
4
+
5
+ gem 'sinatra'
6
+ gem 'activerecord', require: 'active_record'
7
+ gem 'sinatra-activerecord'
8
+ gem 'sqlite3'
9
+ gem 'rackup'
10
+ gem 'rake'
11
+
12
+ # API layer
13
+ # If you're using this scaffold from inside the rapitapir repo, this local path works.
14
+ # Otherwise, replace with: gem 'rapitapir', '~> 2.0.1'
15
+ gem 'rapitapir', path: File.expand_path('../../..', __dir__)
16
+
17
+ group :development, :test do
18
+ gem 'rspec'
19
+ gem 'rack-test'
20
+ gem 'dotenv'
21
+ gem 'pry'
22
+ end
@@ -0,0 +1,10 @@
1
+ # nocodb
2
+
3
+ Generated by RapiTapir scaffold from OpenAPI spec.
4
+
5
+ ## Run
6
+ ```bash
7
+ bundle install
8
+ bundle exec rake db:create db:migrate
9
+ bundle exec rackup
10
+ ```
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['RACK_ENV'] ||= 'development'
4
+
5
+ require 'dotenv/load'
6
+ require_relative 'app/api/app'
7
+ require 'sinatra/activerecord/rake'