api-document-generator 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad66014537251556e8ba4e1679ef970301ba2b4bf248947026306fde0cc610b4
4
- data.tar.gz: 30eb5b6eca8b9ba0179743d8434557ef29ea21a2f283cb357c548698e896dbe9
3
+ metadata.gz: 26b6ef9067a2b475f2835404c819cbe06ef711f8509301dfd4d1caa827390b74
4
+ data.tar.gz: 9bd47e1c90d95799f63b237eee705698ae4d4f07ee058423c09dfa0f83f6a1cb
5
5
  SHA512:
6
- metadata.gz: b16dd5cafa25fbb2e3da5712f51cd93dc75c1514d83f3288f8d78f53b7635e4c0d74969ef0c65c23cd919be92deab84d582b191de0afc3d4c593b3aafedd094d
7
- data.tar.gz: 732fe4c155c29bf4f8179b5e95f88a866afc3dab2125ea2dfd5fc2f55cf97c24522dba5a9cfeea06315e57b9d4b5f96fafd237fde07cb5401b960744f2e1029e
6
+ metadata.gz: 719c740045c6fc2eb2e7d1979cd506969310cf45c3407c5050556c04d93b9947f2a96be971108fa46354001a7580716fdbc8c084d0507935601eb7feb948c0bc
7
+ data.tar.gz: d81cd212efd47f8006be0c98ceeb47888a85b959eb4fae672cdeff08edc536d847bfcf8445ac6f26c136aef698a323c256709c2c2bfced8a0e8c65aad3b0da1e
@@ -1,9 +1,49 @@
1
1
  class ApiDocumentGenerator::ApiGrabber
2
- def self.grab_apis
3
- controllers = Dir['app/controllers/**/*.rb']
4
-
5
- controllers.each do |c|
6
- pp c
7
- end
8
- end
9
- end
2
+ def self.grab_apis_params(controllers)
3
+ controllers.keys.each do |controller|
4
+ c_controller = controller.contantize.new
5
+ param_hash = c_controller.parameters_for_different_actions
6
+ controllers[controller][:resources] = c_controller.is_resources
7
+ controllers[controller][:exclude_actions] = c_controller.exclude_actions
8
+
9
+ controllers[controller][:actions].each do |action|
10
+ params = param_hash[action[:action].to_sym]
11
+
12
+ controllers[controller][:actions][:params] = params
13
+ end
14
+ end
15
+
16
+ add_routes(controllers)
17
+ end
18
+
19
+ def self.add_routes(controllers)
20
+ insert_lines = [
21
+ " namespace :api do\n",
22
+ " end \n"
23
+ ]
24
+
25
+ default_actions = ["index", "show", "new", "edit", "create", "delete", "update"]
26
+
27
+ controllers.each do |k, v|
28
+ path = v[:path]
29
+
30
+ insert_lines.insert(-1, " #{v[:resources] ? ('resources') : ('resource')} :#{path.split("/").last}, only : #{default_actions - v[:exclude_actions]} do\n")
31
+ insert_lines.insert(-1, " end\n")
32
+
33
+ v[:actions].each do |action|
34
+ if !default_actions.include?(action[:action])
35
+ insert_lines.insert(-2, " #{action[:method].downcase} :#{action[:action]}\n")
36
+ end
37
+ end
38
+ end
39
+
40
+ File.open(file_name, 'r+') do |f|
41
+ routes = f.each_line.to_a
42
+ end_line = routes.delete_at(-1)
43
+
44
+ routes = routes + insert_lines + [end_line]
45
+ f.rewind
46
+ f.write(routes.join)
47
+ end
48
+ end
49
+ end
@@ -1,2 +1,3 @@
1
1
  class ApiDocumentGenerator::PageWriter
2
- end
2
+
3
+ end
@@ -1,5 +1,24 @@
1
1
  class ApiDocumentGenerator
2
2
  class << self
3
+ def initialize
4
+ controllers = {}
5
+ Rails.application.routes.routes.each do |route|
6
+ controller_path = route.defaults[:controller]
7
+
8
+ if controller_path
9
+ controller_name = controller_path.split('/').map(&:humanize).join('::')
10
+
11
+ if controller_name.constantize.include?(Api::GeneratorHelper)
12
+ controllers[controller_name] ||= {path: controller_path, actions: []}
13
+
14
+ controllers[controller_name][:actions].push({action: route.defaults[:action], method: route.verb})
15
+ end
16
+ end
17
+ end
18
+
19
+ generate_api_document(controllers)
20
+ end
21
+
3
22
  def hi
4
23
  puts "Hello this is my api documentat generator gem!"
5
24
  end
@@ -8,12 +27,12 @@
8
27
  if Dir.exist?('public') == false
9
28
  `mkdir public`
10
29
  end
11
-
12
- generate_api_document
30
+
31
+ initialize
13
32
  end
14
33
 
15
- def generate_api_document
16
- apis = ApiDocumentGenerator::ApiGrabber.grab_apis
34
+ def generate_api_document(controllers)
35
+ apis = ApiDocumentGenerator::ApiGrabber.grab_apis(controllers)
17
36
  # api_document = ApiDocumentGenerator::PageWriter(apis)
18
37
 
19
38
  # File.open('public/api_document', 'w') do |f|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-document-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Koe(shushi)