Class: Shorui::Generator
- Inherits:
-
Object
- Object
- Shorui::Generator
- Defined in:
- lib/shorui/generator.rb
Class Method Summary collapse
- .fetch_routes(ignore_rails_paths = false) ⇒ Object
- .generate_readme(ignore_rails_paths = false) ⇒ Object
Class Method Details
.fetch_routes(ignore_rails_paths = false) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/shorui/generator.rb', line 3 def self.fetch_routes(ignore_rails_paths = false) Rails.application.routes.routes.map do |route| path = route.path.spec.to_s.gsub("(.:format)", "") if ignore_rails_paths && path.start_with?("/rails") next end controller = route.defaults.dig(:controller) action = route.defaults.dig(:action) if controller && action { path: path, verb: route.verb, controller_action: { controller: controller, action: action }, required_params: route.path.required_names, optional_params: route.defaults.keys - [:controller, :action] } end end.compact end |
.generate_readme(ignore_rails_paths = false) ⇒ Object
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 |
# File 'lib/shorui/generator.rb', line 26 def self.generate_readme(ignore_rails_paths = false) routes = fetch_routes(ignore_rails_paths) dir_name = File.basename(Dir.pwd) formatted_name = dir_name.gsub(/[-_\/|\\.]/, ' ').split.map(&:capitalize).join(' ') content = "# #{formatted_name} API Documentation\n\n" routes.each do |route| if route.nil? next end if ignore_rails_paths next if route[:verb].start_with?("/rails") end controller = route.dig(:controller_action, :controller) action = route.dig(:controller_action, :action) if controller && action required_params = route[:required_params].any? ? route[:required_params].join(', ') : nil optional_params = route[:optional_params].any? ? route[:optional_params].join(', ') : nil content << "#### [#{route[:verb]}] #{route[:path]}\n" content << "- **Controller#Action:** `#{controller}##{action}`\n" content << "- **Required Params:** `#{required_params}`\n" if required_params content << "- **Optional Params:** `#{optional_params}`\n\n" if optional_params end end File.write('README.md', content) puts "README.md successfully generated in #{Dir.pwd}!" end |