oas_grape 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7cee578b32b694847532fbf186e9edf2f0418381b912417faf8181c32732144a
4
+ data.tar.gz: 96c7a37f3914b4a6ba4200687d232e4f0397fb8708b6e072f308f59d55fc5764
5
+ SHA512:
6
+ metadata.gz: 301223cc381bdbaefdd210602b1a499df8c110537404c3a42811c6d69cf544583eaaae2b8b6b7661fec2ac57149cd776391af4d4a2659813f5cf60edff3acc78
7
+ data.tar.gz: 2a0a1ae22838e8d9c0c6a2f52e31a7165ac8f588668a7ee1a2b214616f2667a2243c67e732ecf8927654d70abb5b908e32c2bb790542eb70db0818829f6e96ae
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ ![Gem Version](https://img.shields.io/gem/v/oas_grape?color=E9573F)
2
+ ![GitHub License](https://img.shields.io/github/license/a-chacon/oas_grape?color=blue)
3
+ ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/a-chacon/oas_grape/.github%2Fworkflows%2Fruby.yml)
4
+ ![Gem Total Downloads](https://img.shields.io/gem/dt/oas_grape)
5
+ ![Static Badge](https://img.shields.io/badge/Ruby-%3E%3D3.1.0-%23E9573F)
6
+
7
+ # šŸ“ƒ Open API Specification For Grape
8
+
9
+ **OasGrape** is a tool for generating **automatic interactive documentation for your Grape APIs**. It generates an **OAS 3.1** document and displays it using **[RapiDoc](https://rapidocweb.com)**.
10
+
11
+ It relies on the [OasCore](https://github.com/a-chacon/oas_core).
12
+
13
+ ![Screenshot](https://a-chacon.com/oas_core/assets/grape_theme.png)
14
+
15
+ ## Documentation
16
+
17
+ For details on how to install, configure, and use OasGrape, please refer to the [OasCore MDBook](http://a-chacon.com/oas_core).
18
+
19
+ ## Contributing
20
+
21
+ Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star⭐! Thanks again!
22
+
23
+ If you plan a big feature, first open an issue to discuss it before any development.
24
+
25
+ 1. Fork the Project
26
+ 2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
27
+ 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
28
+ 4. Push to the Branch (`git push origin feature/AmazingFeature`)
29
+ 5. Open a Pull Request
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.en.html#license-text).
34
+
35
+ ## Star History
36
+
37
+ [![Star History Chart](https://api.star-history.com/svg?repos=a-chacon/oas_grape&type=Date)](https://www.star-history.com/#a-chacon/oas_grape&Date)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OasGrape
4
+ class Configuration < OasCore::Configuration
5
+ attr_accessor :rapidoc_theme, :source_oas_path
6
+ attr_reader :include_mode
7
+
8
+ def initialize
9
+ super(info: generate_info_object)
10
+
11
+ @include_mode = :all
12
+ @rapidoc_theme = :rails
13
+ @source_oas_path = nil
14
+ end
15
+
16
+ def include_mode=(value)
17
+ valid_modes = %i[all with_tags explicit]
18
+ raise ArgumentError, "include_mode must be one of #{valid_modes}" unless valid_modes.include?(value)
19
+
20
+ @include_mode = value
21
+ end
22
+
23
+ def generate_info_object
24
+ OasCore::Spec::Info.new(
25
+ title: title,
26
+ summary: summary,
27
+ description: description
28
+ )
29
+ end
30
+
31
+ def title
32
+ "OasGrape"
33
+ end
34
+
35
+ def summary
36
+ "OasGrape: Automatic Interactive API Documentation for Grape"
37
+ end
38
+
39
+ def description
40
+ <<~DESC
41
+ # Welcome to OasGrape
42
+
43
+ OasGrape automatically generates interactive documentation for your Grape APIs using the OpenAPI Specification 3.1 (OAS 3.1) and displays it with a sleek UI.
44
+
45
+ ## Getting Started
46
+
47
+ You've successfully mounted the OasGrape engine. This default documentation is based on your routes and automatically gathered information.
48
+
49
+ For more details, visit the official documentation: [OasCore Documentation](https://a-chacon.com/oas_core).
50
+
51
+ ## Features
52
+
53
+ - **Automatic OAS 3.1 Document Generation**: No manual specification required.
54
+ - **[RapiDoc](https://github.com/rapi-doc/RapiDoc) Integration**: Interactive API exploration.
55
+ - **Minimal Setup**: Basic documentation works out of the box.
56
+ - **Extensible**: Customize through configuration and YARD tags.
57
+
58
+ Explore your API documentation and enjoy the power of OasGrape!
59
+
60
+ Any questions visit the [OasGrape GitHub Repository](https://github.com/a-chacon/oas_grape).
61
+ DESC
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OasGrape
4
+ class OasRouteBuilder
5
+ def self.build_from_grape_route(grape_route)
6
+ new(grape_route).build
7
+ end
8
+
9
+ def initialize(grape_route)
10
+ @grape_route = grape_route
11
+ end
12
+
13
+ def build
14
+ OasCore::OasRoute.new(
15
+ controller: controller,
16
+ method_name: method_name,
17
+ verb: verb,
18
+ path: path,
19
+ docstring: docstring,
20
+ source_string: source_string,
21
+ tags: tags
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ def controller
28
+ @grape_route.options[:namespace] || "unknown"
29
+ end
30
+
31
+ def method_name
32
+ @grape_route.options[:description] || "unknown"
33
+ end
34
+
35
+ def verb
36
+ @grape_route.request_method.downcase
37
+ end
38
+
39
+ def path
40
+ @grape_route.pattern.origin
41
+ end
42
+
43
+ def source_string
44
+ "Source not available" # Not applicable in this context
45
+ end
46
+
47
+ def docstring
48
+ detail = @grape_route.options.dig(:settings, :description, :detail) || @grape_route.options[:detail]
49
+ return "Docstring not available" unless detail
50
+
51
+ processed_lines = detail.lines.map { |line| line.sub(/^# /, "") }
52
+
53
+ filtered_lines = processed_lines.reject do |line|
54
+ line.include?("rubocop") || line.include?("TODO")
55
+ end
56
+
57
+ ::YARD::Docstring.parser.parse(filtered_lines.join).to_docstring
58
+ rescue StandardError
59
+ "Docstring not available"
60
+ end
61
+
62
+ def tags
63
+ detail = @grape_route.options[:detail] || ""
64
+
65
+ detail += "\n# @summary #{@grape_route.options[:description]}" if detail.empty? || !detail.include?("@summary")
66
+
67
+ parse_tags(detail)
68
+ end
69
+
70
+ def parse_tags(comment)
71
+ lines = comment.lines.map { |line| line.sub(/^# /, "") }
72
+ ::YARD::Docstring.parser.parse(lines.join).tags
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OasGrape
4
+ class RouteExtractor
5
+ class << self
6
+ def host_routes
7
+ @host_routes ||= extract_grape_routes
8
+ end
9
+
10
+ def clear_cache
11
+ @host_routes = nil
12
+ end
13
+
14
+ private
15
+
16
+ def extract_grape_routes
17
+ grape_klasses = ObjectSpace.each_object(Class).select { |klass| klass < Grape::API }
18
+ routes = grape_klasses.flat_map(&:routes).uniq { |r| r.path + r.request_method.to_s }
19
+
20
+ routes = routes.map { |route| OasRouteBuilder.build_from_grape_route(route) }
21
+ filter_routes(routes)
22
+ end
23
+
24
+ def filter_routes(routes)
25
+ case OasGrape.config.include_mode
26
+ when :with_tags
27
+ routes.select { |route| route.tags.any? }
28
+ when :explicit
29
+ routes.select { |route| route.tags.any? { |t| t.tag_name == "oas_include" } }
30
+ else
31
+ routes
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OasGrape
4
+ VERSION = "1.0.0"
5
+ end