oas_hanami 0.1.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.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OasHanami
4
+ module Web
5
+ class View
6
+ def self.call(env)
7
+ new.call(env)
8
+ end
9
+
10
+ def call(env)
11
+ @request = Rack::Request.new(env)
12
+ oas = OasHanami.build.to_json
13
+
14
+ if @request.path.end_with?(".json")
15
+ [200, { "Content-Type" => "application/json" }, [oas]]
16
+ elsif @request.path.end_with?(".js")
17
+ [200, { "Content-Type" => "application/javascript" }, [render_js]]
18
+ else
19
+ [200, { "Content-Type" => "text/html" }, [render_view]]
20
+ end
21
+ end
22
+
23
+ def render_view
24
+ template_path = File.expand_path("views/index.html.erb", __dir__)
25
+ template = File.read(template_path)
26
+ ERB.new(template).result(binding)
27
+ end
28
+
29
+ def render_js
30
+ template_path = File.expand_path("assets/rapidoc-min.js", __dir__)
31
+ File.read(template_path)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,154 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= OasHanami.config.info.title %></title>
5
+ <meta charset="utf-8">
6
+ <!-- Important: rapi-doc uses utf8 characters -->
7
+ <script type="module" src="<%= @request.env['SCRIPT_NAME'] %>/rapidoc-min.js"></script>
8
+ <script>
9
+ function applyTheme() {
10
+ const theme = "<%= OasHanami.config.rapidoc_theme %>";
11
+ const docEl = document.querySelector('rapi-doc');
12
+
13
+ // Reset all theme-related attributes
14
+ docEl.removeAttribute('theme');
15
+ docEl.removeAttribute('bg-color');
16
+ docEl.removeAttribute('text-color');
17
+ docEl.removeAttribute('nav-bg-color');
18
+ docEl.removeAttribute('nav-text-color');
19
+ docEl.removeAttribute('nav-hover-bg-color');
20
+ docEl.removeAttribute('nav-hover-text-color');
21
+ docEl.removeAttribute('nav-accent-color');
22
+ docEl.removeAttribute('primary-color');
23
+
24
+ console.log(theme === 'dark')
25
+
26
+ if (theme === 'dark'){
27
+ docEl.setAttribute('theme','dark');
28
+ docEl.setAttribute('bg-color','#333');
29
+ docEl.setAttribute('text-color','#BBB');
30
+ } else if (theme === 'light'){
31
+ docEl.setAttribute('theme','light');
32
+ docEl.setAttribute('bg-color','#FFF');
33
+ docEl.setAttribute('text-color','#444');
34
+ } else if (theme === 'night') {
35
+ docEl.setAttribute('theme','dark');
36
+ docEl.setAttribute('bg-color','#14191f');
37
+ docEl.setAttribute('text-color','#aec2e0');
38
+ } else if (theme === 'mud') {
39
+ docEl.setAttribute('theme','dark');
40
+ docEl.setAttribute('bg-color','#403635');
41
+ docEl.setAttribute('text-color','#c3b8b7');
42
+ } else if (theme === 'cofee') {
43
+ docEl.setAttribute('theme','dark');
44
+ docEl.setAttribute('bg-color','#36312C');
45
+ docEl.setAttribute('text-color','#ceb8a0');
46
+ } else if (theme === 'forest') {
47
+ docEl.setAttribute('theme','dark');
48
+ docEl.setAttribute('bg-color','#384244');
49
+ docEl.setAttribute('text-color','#BDD6DB');
50
+ } else if (theme === 'olive') {
51
+ docEl.setAttribute('theme','dark');
52
+ docEl.setAttribute('bg-color','#2a2f31');
53
+ docEl.setAttribute('text-color','#acc7c8');
54
+ } else if (theme === 'outerspace') {
55
+ docEl.setAttribute('theme','dark');
56
+ docEl.setAttribute('bg-color','#2D3133');
57
+ docEl.setAttribute('text-color','#CAD9E3');
58
+ } else if (theme === 'ebony') {
59
+ docEl.setAttribute('theme','dark');
60
+ docEl.setAttribute('bg-color','#2B303B');
61
+ docEl.setAttribute('text-color','#dee3ec');
62
+ } else if (theme === 'snow') {
63
+ docEl.setAttribute('theme','light');
64
+ docEl.setAttribute('bg-color','#FAFAFA');
65
+ docEl.setAttribute('text-color','#555');
66
+ } else if (theme === 'green') {
67
+ docEl.setAttribute('theme','light');
68
+ docEl.setAttribute('bg-color','#f9fdf7');
69
+ docEl.setAttribute('text-color','#375F1B');
70
+ } else if (theme === 'blue') {
71
+ docEl.setAttribute('theme','light');
72
+ docEl.setAttribute('bg-color','#ecf1f7');
73
+ docEl.setAttribute('text-color','#133863');
74
+ } else if (theme === 'beige') {
75
+ docEl.setAttribute('show-header','true');
76
+ docEl.setAttribute('theme','light');
77
+ docEl.setAttribute('bg-color','#fdf8ed');
78
+ docEl.setAttribute('text-color','#342809');
79
+ } else if (theme === 'graynav') {
80
+ docEl.setAttribute('show-header','false');
81
+ docEl.setAttribute('theme','light');
82
+ docEl.setAttribute('nav-bg-color','#3e4b54');
83
+ docEl.setAttribute('nav-accent-color','#fd6964');
84
+ docEl.setAttribute('primary-color','#ea526f');
85
+ } else if (theme === 'purplenav') {
86
+ docEl.setAttribute('show-header','false');
87
+ docEl.setAttribute('theme','light');
88
+ docEl.setAttribute('nav-accent-color','#ffd8e7');
89
+ docEl.setAttribute('nav-bg-color','#666699');
90
+ docEl.setAttribute('primary-color','#ea526f');
91
+ docEl.setAttribute('bg-color','#fff9fb');
92
+ } else if (theme === 'lightgraynav') {
93
+ docEl.setAttribute('show-header','false');
94
+ docEl.setAttribute('theme','light');
95
+ docEl.setAttribute('nav-bg-color','#fafafa');
96
+ docEl.setAttribute('nav-hover-text-color','#9b0700');
97
+ docEl.setAttribute('nav-hover-bg-color','#ffebea');
98
+ docEl.setAttribute('primary-color','#F63C41');
99
+ docEl.setAttribute('bg-color','#ffffff');
100
+ } else if (theme === 'darkbluenav') {
101
+ docEl.setAttribute('show-header','false');
102
+ docEl.setAttribute('theme','light');
103
+ docEl.setAttribute('bg-color','#f9f9fa');
104
+ docEl.setAttribute('nav-bg-color','#3f4d67');
105
+ docEl.setAttribute('nav-text-color','#a9b7d0');
106
+ docEl.setAttribute('nav-hover-bg-color','#333f54');
107
+ docEl.setAttribute('nav-hover-text-color','#fff');
108
+ docEl.setAttribute('nav-accent-color','#f87070');
109
+ docEl.setAttribute('primary-color','#5c7096');
110
+ } else if (theme === 'rails') {
111
+ docEl.setAttribute('theme','light');
112
+ docEl.setAttribute('bg-color','#FFFFFF');
113
+ docEl.setAttribute('nav-bg-color','#101828');
114
+ docEl.setAttribute('nav-text-color','#fff');
115
+ docEl.setAttribute('nav-hover-bg-color','#261B23');
116
+ docEl.setAttribute('nav-hover-text-color','#fff');
117
+ docEl.setAttribute('nav-accent-color','#D30001');
118
+ docEl.setAttribute('primary-color','#D30001');
119
+ } else if (theme === 'hanami') {
120
+ docEl.setAttribute('theme','light');
121
+ docEl.setAttribute('bg-color','#FFFFFF');
122
+ docEl.setAttribute('nav-bg-color','#685D9F');
123
+ docEl.setAttribute('nav-text-color','#fff');
124
+ docEl.setAttribute('nav-hover-bg-color','#8e80d1');
125
+ docEl.setAttribute('nav-hover-text-color','#fff');
126
+ docEl.setAttribute('nav-accent-color','#FCE4EC');
127
+ docEl.setAttribute('primary-color','#685D9F');
128
+ }
129
+ }
130
+
131
+ document.addEventListener('DOMContentLoaded', applyTheme);
132
+ </script>
133
+ </head>
134
+ <body>
135
+ <rapi-doc
136
+ spec-url="<%= @request.env['SCRIPT_NAME'] %>/.json"
137
+ show-header="false"
138
+ font-size="largest"
139
+ show-method-in-nav-bar="as-colored-text"
140
+ nav-item-spacing="relaxed"
141
+ allow-spec-file-download="true"
142
+ schema-style="table"
143
+ sort-tags="true"
144
+ >
145
+ </rapi-doc>
146
+
147
+ <style>
148
+ rapi-doc::part(btn btn-outline) {
149
+ width: 220px;
150
+ min-width: 170px;
151
+ }
152
+ </style>
153
+ </body>
154
+ </html>
data/lib/oas_hanami.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack"
4
+ require "oas_core"
5
+ require "debug"
6
+
7
+ OasCore.configure_yard!
8
+
9
+ module OasHanami
10
+ autoload :VERSION, "oas_hanami/version"
11
+ autoload :Configuration, "oas_hanami/configuration"
12
+ autoload :RouteExtractor, "oas_hanami/route_extractor"
13
+ autoload :OasRouteBuilder, "oas_hanami/oas_route_builder"
14
+ autoload :HanamiRouteFormatter, "oas_hanami/hanami_route_formatter"
15
+ autoload :Inspector, "oas_hanami/inspector"
16
+
17
+ module Web
18
+ autoload :View, "oas_hanami/web/view"
19
+ end
20
+
21
+ class << self
22
+ def build
23
+ clear_cache
24
+ OasCore.config = config
25
+
26
+ host_routes = RouteExtractor.host_routes
27
+ oas = OasCore::Builders::SpecificationBuilder.new.with_oas_routes(host_routes).build
28
+
29
+ oas.to_spec
30
+ end
31
+
32
+ def configure
33
+ yield config
34
+ end
35
+
36
+ def config
37
+ @config ||= Configuration.new
38
+ end
39
+
40
+ def clear_cache
41
+ return if Hanami.env?(:production)
42
+
43
+ MethodSource.clear_cache
44
+ RouteExtractor.clear_cache
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oas_hanami
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - achacon
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: oas_core
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 0.5.3
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.5.3
26
+ description: OasHanami simplifies API documentation by automatically generating OpenAPI
27
+ Specification (OAS 3.1) documents from your Hanami application routes. It eliminates
28
+ the need for manual documentation, ensuring accuracy and consistency.
29
+ email:
30
+ - andres.ch@protonmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".release-please-config.json"
36
+ - ".release-please-manifest.json"
37
+ - ".rspec"
38
+ - ".rspec_status"
39
+ - ".rubocop.yml"
40
+ - ".rubocop_todo.yml"
41
+ - ".ruby-version"
42
+ - ".ruby.version"
43
+ - CHANGELOG.md
44
+ - README.md
45
+ - Rakefile
46
+ - lib/oas_hanami.rb
47
+ - lib/oas_hanami/configuration.rb
48
+ - lib/oas_hanami/hanami_route_formatter.rb
49
+ - lib/oas_hanami/inspector.rb
50
+ - lib/oas_hanami/oas_route_builder.rb
51
+ - lib/oas_hanami/route_extractor.rb
52
+ - lib/oas_hanami/version.rb
53
+ - lib/oas_hanami/web/assets/rapidoc-min.js
54
+ - lib/oas_hanami/web/view.rb
55
+ - lib/oas_hanami/web/views/index.html.erb
56
+ homepage: https://github.com/a-chacon/oas_hanami
57
+ licenses:
58
+ - GPL-3.0-only
59
+ metadata:
60
+ homepage_uri: https://github.com/a-chacon/oas_hanami
61
+ source_code_uri: https://github.com/a-chacon/oas_hanami
62
+ changelog_uri: https://github.com/a-chacon/oas_hanami/blob/main/CHANGELOG.md
63
+ rubygems_mfa_required: 'true'
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 3.1.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.6.7
79
+ specification_version: 4
80
+ summary: Generates OpenAPI Specification (OAS) documents by analyzing and extracting
81
+ routes from Hanami applications.
82
+ test_files: []