Class: RailsAiBridge::Services::AppIntrospectionService

Inherits:
RailsAiBridge::Service show all
Defined in:
lib/rails_ai_bridge/services/app_introspection_service.rb

Overview

Application service for running the app Introspector behind RailsAiBridge::Service.

Treats a top-level :error key or any nested value that is a Hash containing :error as failure (per-introspector errors). Otherwise returns success with the introspection Hash as data.

Examples:

Basic usage

result = Services::AppIntrospectionService.call(Rails.application)
if result.success?
  puts "Introspection complete: #{result.data.keys}"
else
  puts "Error: #{result.errors.first}"
end

With specific introspectors

result = Services::AppIntrospectionService.call(Rails.application, only: [:models, :routes])

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, introspector_class: Introspector) ⇒ AppIntrospectionService

Returns a new instance of AppIntrospectionService.

Parameters:

  • app (Rails::Application)

    the Rails application to introspect

  • introspector_class (Class) (defaults to: Introspector)

    introspector class to use (defaults to Introspector)



31
32
33
34
35
# File 'lib/rails_ai_bridge/services/app_introspection_service.rb', line 31

def initialize(app, introspector_class: Introspector)
  super()
  @app = app
  @introspector_class = introspector_class
end

Class Method Details

.call(app, only: nil, introspector_class: Introspector) ⇒ RailsAiBridge::Service::Result

Returns success with introspection data or failure with errors.

Parameters:

  • app (Rails::Application)

    Rails application to introspect

  • only (Array<Symbol>, nil) (defaults to: nil)

    forwarded to Introspector#call

  • introspector_class (Class) (defaults to: Introspector)

    class that responds to #new(app) and #call(only:)

Returns:



25
26
27
# File 'lib/rails_ai_bridge/services/app_introspection_service.rb', line 25

def self.call(app, only: nil, introspector_class: Introspector)
  new(app, introspector_class: introspector_class).call(only: only)
end

Instance Method Details

#call(only: nil) ⇒ RailsAiBridge::Service::Result

Performs introspection and wraps the outcome in a RailsAiBridge::Service::Result.

Fails when the introspector returns a non-Hash, a top-level :error entry, or when any entry's value is a Hash with an :error key (nested per-introspector failure). In the nested case, errors contains strings "<key>: <message>" for each failing entry.

Parameters:

  • only (Array<Symbol>, nil) (defaults to: nil)

    optional list of introspector keys to run

Returns:

  • (RailsAiBridge::Service::Result)

    success with introspection Hash as data, or failure with messages; StandardError from the introspector is captured (not raised)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails_ai_bridge/services/app_introspection_service.rb', line 46

def call(only: nil)
  introspector = @introspector_class.new(@app)
  introspection_result = introspector.call(only: only)

  unless introspection_result.is_a?(Hash)
    return Service::Result.new(false,
                               errors: ['Introspector must return a Hash'])
  end
  if introspection_result.key?(:error)
    return Service::Result.new(false,
                               errors: ["Introspector returned error: #{introspection_result[:error]}"])
  end

  nested_errors = introspection_result.filter_map do |name, payload|
    next unless payload.is_a?(Hash) && payload.key?(:error)

    "#{name}: #{payload[:error]}"
  end
  return Service::Result.new(false, errors: nested_errors) if nested_errors.any?

  Service::Result.new(true, data: introspection_result)
rescue StandardError => error
  Service::Result.new(false, errors: [error.message])
end