Class: AgentCode::Commands::ExportPostmanCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/agentcode/commands/export_postman_command.rb

Overview

Generate a Postman Collection v2.1 for all registered models. Mirrors Laravel php artisan agentcode:export-postman exactly.

Usage: rails agentcode:export_postman [--output=postman_collection.json] [--base-url=http://localhost:3000/api]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExportPostmanCommand

Returns a new instance of ExportPostmanCommand.



15
16
17
18
19
20
21
22
# File 'lib/agentcode/commands/export_postman_command.rb', line 15

def initialize
  super
  @options = {
    output: "postman_collection.json",
    base_url: "http://localhost:3000/api",
    project_name: nil
  }
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/agentcode/commands/export_postman_command.rb', line 13

def options
  @options
end

Instance Method Details

#performObject



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
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
# File 'lib/agentcode/commands/export_postman_command.rb', line 24

def perform
  output_path = options[:output]
  base_url = options[:base_url].chomp("/")
  project_name = options[:project_name] || Rails.application.class.module_parent_name rescue "API"

  config = AgentCode.config
  route_groups = config.route_groups
  all_models = config.models

  has_multiple_groups = route_groups.size > 1
  needs_org_variable = any_group_has_org_prefix?(route_groups)

  variables = build_collection_variables(base_url, needs_org_variable)
  items = []

  items << build_auth_folder

  if has_multiple_groups
    # Multiple groups: Project -> Auth, GroupName -> resources
    route_groups.each do |group_name, group_config|
      group_models = resolve_models_for_group(all_models, config, group_name)
      next if group_models.empty?

      group_prefix = group_config[:prefix] || ""

      group_items = []
      group_models.each do |slug|
        model_class_name = all_models[slug]
        model_class = begin
          model_class_name.constantize
        rescue NameError
          say "Model class does not exist: #{model_class_name}", :red
          next
        end

        model_meta = introspect_model(model_class, slug)
        group_items << {
          name: slug.to_s,
          item: build_action_folders(slug, model_meta, group_prefix)
        }
      end

      if group_items.any?
        items << {
          name: group_name.to_s,
          item: group_items
        }
      end
    end
  else
    # Single group: Project -> Auth, resources (flat, backward compatible)
    group_config = route_groups.values.first || {}
    group_prefix = group_config[:prefix] || ""
    group_name = route_groups.keys.first

    group_models = resolve_models_for_group(all_models, config, group_name)
    group_models.each do |slug|
      model_class_name = all_models[slug]
      model_class = begin
        model_class_name.constantize
      rescue NameError
        say "Model class does not exist: #{model_class_name}", :red
        next
      end

      model_meta = introspect_model(model_class, slug)
      items << {
        name: slug.to_s,
        item: build_action_folders(slug, model_meta, group_prefix)
      }
    end
  end

  collection = {
    info: {
      name: project_name,
      schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    variable: variables,
    item: items
  }

  json = JSON.pretty_generate(collection)
  File.write(output_path, json)

  say "Postman collection written to #{output_path}", :green
end