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.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OasGrape
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 = OasGrape.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><%= OasGrape.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 = "<%= OasGrape.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_grape.rb ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "oas_core"
4
+
5
+ OasCore.configure_yard!
6
+
7
+ module OasGrape
8
+ autoload :VERSION, "oas_grape/version"
9
+ autoload :Configuration, "oas_grape/configuration"
10
+ autoload :RouteExtractor, "oas_grape/route_extractor"
11
+ autoload :OasRouteBuilder, "oas_grape/oas_route_builder"
12
+
13
+ module Web
14
+ autoload :View, "oas_grape/web/view"
15
+ end
16
+
17
+ class << self
18
+ def build
19
+ clear_cache
20
+ OasCore.config = config
21
+
22
+ host_routes = RouteExtractor.host_routes
23
+ oas_source = config.source_oas_path ? read_source_oas_file : {}
24
+
25
+ OasCore.build(host_routes, oas_source: oas_source)
26
+ end
27
+
28
+ def configure
29
+ yield config
30
+ end
31
+
32
+ def config
33
+ @config ||= Configuration.new
34
+ end
35
+
36
+ def clear_cache
37
+ RouteExtractor.clear_cache
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oas_grape
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - a-chacon
8
+ bindir: bin
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: 1.0.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.0.0
26
+ description: OasGrape is a Grape extension for generating automatic interactive documentation
27
+ for your Grape APIs. It generates an OAS 3.1 document and displays it using RapiDoc.
28
+ email:
29
+ - andres.ch@protonmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/oas_grape.rb
37
+ - lib/oas_grape/configuration.rb
38
+ - lib/oas_grape/oas_route_builder.rb
39
+ - lib/oas_grape/route_extractor.rb
40
+ - lib/oas_grape/version.rb
41
+ - lib/oas_grape/web/assets/rapidoc-min.js
42
+ - lib/oas_grape/web/view.rb
43
+ - lib/oas_grape/web/views/index.html.erb
44
+ homepage: https://github.com/a-chacon/oas_grape
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/a-chacon/oas_grape
49
+ source_code_uri: https://github.com/a-chacon/oas_grape
50
+ changelog_uri: https://github.com/a-chacon/oas_grape
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '3.1'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.6.7
66
+ specification_version: 4
67
+ summary: OasGrape is a Grape extension for generating automatic interactive documentation
68
+ for your Grape APIs.
69
+ test_files: []