Last active
December 18, 2024 12:16
-
-
Save gabrielbentog/4e1d95c8d8d90bb72d18500602996873 to your computer and use it in GitHub Desktop.
Generate a ruby on rails insomnia collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'securerandom' | |
require 'json' | |
require 'time' | |
# Gera o arquivo de rotas a partir do Rails | |
routes_json = `rails routes > routes.txt` | |
routes = [] | |
crud_methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'] | |
File.open("routes.txt").each do |line| | |
next if line.strip.empty? || line.start_with?('Prefix', 'Routes') | |
find_method = crud_methods.find { |method| line.include?(method) } | |
if find_method | |
parts = line.split | |
routes << { | |
method: find_method, | |
path: parts[(parts.index(find_method) + 1)].gsub('(.:format)', ''), | |
controller_action: parts[-1].split('#').last.upcase | |
} | |
end | |
end | |
insomnia_collection = { | |
"_type": "export", | |
"__export_format": 4, | |
"__export_date": Time.now.iso8601, | |
"__export_source": "ruby_script", | |
"resources": [ | |
{ | |
"_id": SecureRandom.uuid, | |
"_type": "workspace", | |
"name": "Rails Routes", | |
"description": "", | |
"scope": "collection", | |
"_workspaceId": "__WORKSPACE_ID__" | |
} | |
] | |
} | |
folders = {} | |
routes.each do |route| | |
parent_id = insomnia_collection[:resources][0][:_id] | |
if route[:path] =~ %r{^/api/v1/([^/]+)} | |
folder_name = $1 | |
folder_id = folders[folder_name] || SecureRandom.uuid | |
unless folders[folder_name] | |
folders[folder_name] = folder_id | |
insomnia_collection[:resources] << { | |
"_id": folder_id, | |
"_type": "request_group", | |
"name": folder_name, | |
"parentId": insomnia_collection[:resources][0][:_id], | |
"description": "" | |
} | |
end | |
parent_id = folder_id | |
end | |
# Corpo padrão para POST/PUT/PATCH | |
default_body = if %w[POST PUT PATCH].include?(route[:method]) | |
{ | |
"data": { | |
"attributes": {} | |
} | |
} | |
else | |
nil | |
end | |
resource = { | |
"_id": SecureRandom.uuid, | |
"_type": "request", | |
"parentId": parent_id, | |
"method": route[:method].downcase, | |
"url": "{{BASE_URL}}#{route[:path]}", | |
"name": "#{route[:controller_action]} #{route[:method]} #{route[:path]}", | |
"headers": [ | |
{ "name": "Content-Type", "value": "application/json" }, | |
{ "name": "Authorization", "value": "Bearer {{TOKEN}}" } | |
], | |
"authentication": {}, | |
"parameters": [], | |
"body": { | |
"mimeType": "application/json", | |
"text": default_body ? JSON.pretty_generate(default_body) : nil | |
}, | |
"description": "" | |
} | |
insomnia_collection[:resources] << resource | |
end | |
File.open('insomnia_collection.json', 'w') do |file| | |
file.write(JSON.pretty_generate(insomnia_collection)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment