boto-rb-scalar 0.21.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: 7c5f7331d4f6a4637e48247a857874c16f6dc552e89c2cecbefeb9e2153c7c3e
4
+ data.tar.gz: b3bfe7fcac09d0bcfe8cbb037251e0e2a08dbda0082edf3198816829c37ca826
5
+ SHA512:
6
+ metadata.gz: 6d4919da41e98017d29c20c279f949cbec9c7f1589f4f2eaa3abfad6385568b1461de1bec1284f02e7e8731da315baec8a83c89ba5ca803b0ac96c50dffa8721
7
+ data.tar.gz: 5424b86e0efb3b5fb01bf1803ba10d3b1743f070395d0a4653a7c37d753109774be9ae24feb30be4e44a58a90dfb9a9bf4901ee26ae5b606eb1da2359da0f6a8
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Evo Tech Builder
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # boto-rb-scalar
2
+
3
+ Scalar API reference for Boto apps. The `:scalar` plugin serves the Scalar UI
4
+ next to the API, pointed at the document of the `:openapi` plugin. The UI is
5
+ embedded in the gem, so it works offline and under a strict Content Security
6
+ Policy: there is no CDN and no inline script or style.
7
+
8
+ ## Installation
9
+
10
+ Add it to the Gemfile of the solution (or of the project that uses it):
11
+
12
+ ```ruby
13
+ gem "boto-rb-scalar"
14
+ ```
15
+
16
+ Then run `bundle install`.
17
+
18
+ ## Plugin
19
+
20
+ Enable it in the project's `config/app.rb`, after `:openapi`:
21
+
22
+ ```ruby
23
+ Boto.configure do |app|
24
+ app.use :openapi, auth: false
25
+ app.use :scalar # GET /scalar
26
+ end
27
+ ```
28
+
29
+ Options:
30
+
31
+ - `path:` changes where the UI is served (default `/scalar`). It must start
32
+ with `/` and not end with `/`.
33
+
34
+ ```ruby
35
+ app.use :scalar, path: "/reference"
36
+ ```
37
+
38
+ ## Rules
39
+
40
+ Boot fails with a message when:
41
+
42
+ - `:openapi` is not enabled (the page needs the document it names);
43
+ - the document is protected by bearer or API key. A browser cannot send those
44
+ credentials, so use `app.use :openapi, auth: false` or `app.use :auth, strategy: :basic`;
45
+ - an endpoint uses `GET` on the same path.
46
+
47
+ The UI respects `SCRIPT_NAME`, so links work behind the gateway
48
+ (for example `/catalogo/scalar`).
49
+
50
+ ## Coexistence with Swagger UI
51
+
52
+ `:scalar` and `:swagger_ui` are independent plugins, each on its own path
53
+ (`/scalar` and `/docs`), so both can be enabled in the same project.
54
+
55
+ ## What stays off
56
+
57
+ The vendored Scalar is configured with its outside services disabled:
58
+ telemetry, the request proxy, the AI agent and MCP. The UI only calls the API
59
+ it documents (the "Try it" client runs in the user's browser, as in Swagger UI).
60
+
61
+ ## Vendored files
62
+
63
+ `vendor/scalar/` holds `@scalar/api-reference` (MIT), with its version in
64
+ `vendor/scalar/VERSION`. The asset URLs include that version and the gem
65
+ version, so they are cached as immutable.
data/assets/boto.css ADDED
@@ -0,0 +1,3 @@
1
+ /* Boto accent on the Scalar theme: Ruby red in both modes. The rules are unlayered, so they beat Scalar's cascade layers. */
2
+ .light-mode, .light-mode .scalar-app { --scalar-color-accent: #cc342d; }
3
+ .dark-mode, .dark-mode .scalar-app { --scalar-color-accent: #e0473f; }
data/assets/ruby.svg ADDED
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Boto">
2
+ <polygon points="16,10 48,10 60,24 4,24" fill="#e0473f"/>
3
+ <polygon points="4,24 60,24 32,56" fill="#cc342d"/>
4
+ <path d="M16 10 L24 24 L32 56 L40 24 L48 10" fill="none" stroke="#ffffff" stroke-opacity="0.45"
5
+ stroke-width="1.5" stroke-linecap="round"/>
6
+ </svg>
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Plugins
5
+ # Serves the Scalar API reference at a path, pointed at the document of the :openapi plugin. Assets are read at
6
+ # boot and pages are built once per mount prefix, so a request is a table lookup that never touches disk.
7
+ class Scalar < Boto::Plugin
8
+ scope :global
9
+
10
+ GET = "GET"
11
+ HEAD = "HEAD"
12
+ SERVED_METHODS = [GET, HEAD].freeze
13
+ OK = 200
14
+ PATH_PREFIX = "/"
15
+ DEFAULT_PATH = "/scalar"
16
+ ASSETS_SEGMENT = "/assets/"
17
+ OPENAPI_PLUGIN = :openapi
18
+ BASIC_CHALLENGE = "Basic"
19
+ HTML_TYPE = "text/html; charset=utf-8"
20
+ JS_TYPE = "text/javascript; charset=utf-8"
21
+ CONTENT_TYPE = "content-type"
22
+ CACHE_CONTROL = "cache-control"
23
+ # The page and initializer embed the document path, which can change without a new asset version.
24
+ NO_CACHE = "no-cache"
25
+ # Asset URLs carry the vendored version, so a new Scalar gets new URLs.
26
+ IMMUTABLE = "public, max-age=31536000, immutable"
27
+
28
+ # Where the page is served (default /scalar), for links such as the gateway panel's.
29
+ attr_reader :path
30
+
31
+ def attach(app)
32
+ @openapi = find_openapi!(app)
33
+ @guard = @openapi.guard
34
+ check_guard!
35
+ check_no_endpoint_on_path!(app)
36
+ check_no_plugin_on_path!(app)
37
+ @assets = Boto::Scalar::Assets.load
38
+ @table = build_table
39
+ # Few prefixes exist (one per gateway), so one cached table per prefix stays small.
40
+ @tables_by_prefix = {}
41
+ end
42
+
43
+ def call(env, app)
44
+ return app.call(env) unless serves?(env)
45
+ return answer(env) if @table.key?(env["PATH_INFO"])
46
+ return Boto::Error::NotFound.new.to_response if env["PATH_INFO"].start_with?(@assets_prefix)
47
+
48
+ app.call(env)
49
+ end
50
+
51
+ private
52
+
53
+ def configure(path: DEFAULT_PATH)
54
+ @path = validate_path(path)
55
+ @assets_prefix = "#{@path}#{ASSETS_SEGMENT}"
56
+ @responder = method(:respond)
57
+ end
58
+
59
+ def validate_path(path)
60
+ return path if path.is_a?(String) && path.start_with?(PATH_PREFIX) && !path.end_with?(PATH_PREFIX)
61
+
62
+ raise ArgumentError, "scalar: path must be a String starting with \"/\" and not ending with \"/\", " \
63
+ "got #{path.inspect}"
64
+ end
65
+
66
+ def serves?(env)
67
+ env["PATH_INFO"].start_with?(@path) && SERVED_METHODS.include?(env["REQUEST_METHOD"])
68
+ end
69
+
70
+ def answer(env)
71
+ @guard ? @guard.call(env, @responder) : @responder.call(env)
72
+ end
73
+
74
+ def respond(env)
75
+ content_type, body, cache_control = table_for(env).fetch(env["PATH_INFO"])
76
+ [OK, { CONTENT_TYPE => content_type, CACHE_CONTROL => cache_control }, [body]]
77
+ end
78
+
79
+ # Keys are always PATH_INFO-based, so every table has the same keys; only the URLs in the page change.
80
+ def table_for(env)
81
+ prefix = env["SCRIPT_NAME"].to_s
82
+ return @table if prefix.empty?
83
+
84
+ @tables_by_prefix[prefix] ||= build_table(prefix)
85
+ end
86
+
87
+ def find_openapi!(app)
88
+ openapi = app.plugins.find { |plugin| plugin.class.plugin_name == OPENAPI_PLUGIN }
89
+ return openapi if openapi
90
+
91
+ raise ArgumentError, "scalar: needs app.use :openapi, because the page loads the document it names"
92
+ end
93
+
94
+ def check_guard!
95
+ return if @guard.nil? || @guard.strategy.challenge.to_s.start_with?(BASIC_CHALLENGE)
96
+
97
+ raise ArgumentError, "scalar: a browser cannot send #{@guard.strategy.challenge.inspect} credentials, " \
98
+ "so the page could not load the document; " \
99
+ "use app.use :openapi, auth: false or app.use :auth, strategy: :basic"
100
+ end
101
+
102
+ # :swagger_ui lives in another gem and serves /docs by default; both on one path would shadow each other.
103
+ def check_no_plugin_on_path!(app)
104
+ other = app.plugins.find { |plugin| plugin != self && plugin.respond_to?(:path) && plugin.path == @path }
105
+ return if other.nil?
106
+
107
+ raise ArgumentError, "scalar: :#{other.class.plugin_name} already serves #{@path}; give one of them another path:"
108
+ end
109
+
110
+ def check_no_endpoint_on_path!(app)
111
+ conflict = app.endpoints.find { |endpoint| endpoint.route == [GET, @path] }
112
+ return if conflict.nil?
113
+
114
+ raise ArgumentError, "scalar: #{conflict.name} uses GET #{@path}, which the plugin already serves"
115
+ end
116
+
117
+ # The prefix only changes the URLs embedded in the page; the table keys and asset bodies stay the same.
118
+ def build_table(prefix = "")
119
+ base = asset_base
120
+ page = Boto::Scalar::Page.new(title: @openapi.title, document_url: "#{prefix}#{@openapi.path}",
121
+ assets_path: "#{prefix}#{base}")
122
+ table = { @path => [HTML_TYPE, page.html, NO_CACHE] }
123
+ table["#{base}/#{Boto::Scalar::Page::INITIALIZER_NAME}"] = [JS_TYPE, page.initializer_js, NO_CACHE]
124
+ Boto::Scalar::Assets::FILES.each_key { |name| table["#{base}/#{name}"] = [*@assets.fetch(name), IMMUTABLE] }
125
+ table.transform_values(&:freeze).freeze
126
+ end
127
+
128
+ # The gem version is in the URL too: boto.css and ruby.svg change with the gem, not with Scalar.
129
+ def asset_base
130
+ "#{@path}#{ASSETS_SEGMENT}#{@assets.version}-#{Boto::Scalar::VERSION}"
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Scalar
5
+ # Vendored Scalar and Boto brand files, read once at load time so requests never touch disk.
6
+ class Assets
7
+ VENDOR_DIR = File.expand_path("../../../vendor/scalar", __dir__)
8
+ BRAND_DIR = File.expand_path("../../../assets", __dir__)
9
+ VERSION_FILE = "VERSION"
10
+ CSS_TYPE = "text/css; charset=utf-8"
11
+ JS_TYPE = "text/javascript; charset=utf-8"
12
+ SVG_TYPE = "image/svg+xml"
13
+ # Maps every servable name to [directory key, content type]; the keys are the only names ever served.
14
+ FILES = {
15
+ "standalone.js" => [:vendor, JS_TYPE],
16
+ "boto.css" => [:brand, CSS_TYPE],
17
+ "ruby.svg" => [:brand, SVG_TYPE]
18
+ }.freeze
19
+
20
+ def self.load(vendor_dir: VENDOR_DIR, brand_dir: BRAND_DIR)
21
+ dirs = { vendor: vendor_dir, brand: brand_dir }
22
+ files = FILES.to_h do |name, (dir_key, content_type)|
23
+ [name, [content_type, File.binread(File.join(dirs.fetch(dir_key), name)).freeze].freeze]
24
+ end
25
+ new(version: File.read(File.join(vendor_dir, VERSION_FILE)).strip.freeze, files: files.freeze)
26
+ end
27
+
28
+ attr_reader :version
29
+
30
+ def initialize(version:, files:)
31
+ @version = version
32
+ @files = files
33
+ freeze
34
+ end
35
+
36
+ # Returns [content_type, body] for a known name, or nil; the name is only a hash key, never a path.
37
+ def fetch(name)
38
+ @files[name]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi/escape"
4
+ require "json"
5
+
6
+ module Boto
7
+ module Scalar
8
+ # No inline scripts or styles on purpose: the page works under a strict CSP.
9
+ class Page
10
+ INITIALIZER_NAME = "initializer.js"
11
+ # Scalar's telemetry, request proxy, AI agent, MCP and developer toolbar (Share, Deploy, Generate SDK on
12
+ # localhost) stay off: the UI only calls the API it documents.
13
+ CONFIG = {
14
+ telemetry: false,
15
+ proxyUrl: "",
16
+ agent: { disabled: true },
17
+ mcp: { disabled: true },
18
+ showDeveloperTools: "never",
19
+ showToolbar: "never"
20
+ }.freeze
21
+
22
+ def initialize(title:, document_url:, assets_path:)
23
+ @title = title
24
+ @document_url = document_url
25
+ @assets_path = assets_path
26
+ end
27
+
28
+ def html
29
+ <<~HTML.freeze
30
+ <!DOCTYPE html>
31
+ <html lang="en">
32
+ <head>
33
+ <meta charset="utf-8">
34
+ <title>#{escape(@title)}</title>
35
+ <link rel="icon" href="#{escape(asset("ruby.svg"))}">
36
+ <link rel="stylesheet" href="#{escape(asset("boto.css"))}">
37
+ </head>
38
+ <body>
39
+ <div id="app"></div>
40
+ <script src="#{escape(asset("standalone.js"))}"></script>
41
+ <script src="#{escape(asset(INITIALIZER_NAME))}"></script>
42
+ </body>
43
+ </html>
44
+ HTML
45
+ end
46
+
47
+ def initializer_js
48
+ <<~JS.freeze
49
+ window.Scalar.createApiReference("#app", #{config_literal});
50
+ JS
51
+ end
52
+
53
+ private
54
+
55
+ def asset(name)
56
+ "#{@assets_path}/#{name}"
57
+ end
58
+
59
+ def escape(value)
60
+ CGI.escapeHTML(value)
61
+ end
62
+
63
+ def config_literal
64
+ JSON.generate(CONFIG.merge(url: @document_url)).gsub("</") { '<\/' }
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Scalar
5
+ VERSION = "0.21.0"
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "boto"
4
+ require "zeitwerk"
5
+
6
+ loader = Zeitwerk::Loader.for_gem_extension(Boto)
7
+ loader.setup
8
+
9
+ module Boto
10
+ # Scalar static assets and the HTML page that loads them.
11
+ module Scalar
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ @scalar/api-reference 1.85.0, https://github.com/scalar/scalar
2
+
3
+ MIT License
4
+
5
+ Copyright (c) Scalar (https://github.com/scalar)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ 1.85.0