miniswag-api 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 04a59ae0610a53747cbfcc2bf71716c17bca7997c9a29c65768087a5a71d38cc
4
+ data.tar.gz: 392f14d55f8509742a96def1a9424a2f5bf65f44a877214f68e0708aa3a8b98d
5
+ SHA512:
6
+ metadata.gz: 8decebb8d4db1d5b2e340de9fabf530bd715cfb96418e448187278c78a3230fa78441edc47c201df9d41c61d0bcee0aa9c1da32b46e19650003531673ac62ebb
7
+ data.tar.gz: a8fe8ea54d822ec0a3a05eacc6e471896666cf269aae9efd029cfc7b0b45e5fc3e24e2eac1b97e3bdac5c6cb8d22d974ace08f872c13aaf3f4f0116e33cae1d7
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sika
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.
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Miniswag
6
+ module Api
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('templates', __dir__)
9
+
10
+ def add_initializer
11
+ template('miniswag_api.rb', 'config/initializers/miniswag_api.rb')
12
+ end
13
+
14
+ def add_routes
15
+ route("mount Miniswag::Api::Engine => '/api-docs'")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ Miniswag::Api.configure do |c|
4
+ # Specify a root folder where OpenAPI JSON/YAML files are located.
5
+ # This is used by the middleware to serve requests for API descriptions.
6
+ c.openapi_root = Rails.root.join('openapi').to_s
7
+
8
+ # Inject a lambda to alter the returned OpenAPI prior to serialization.
9
+ # The function will have access to the rack env for the current request.
10
+ # For example, you could leverage this to dynamically assign the "host" property:
11
+ #
12
+ # c.openapi_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] }
13
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Miniswag
4
+ module Api
5
+ class Configuration
6
+ attr_accessor :openapi_root, :openapi_filter, :openapi_headers
7
+
8
+ def resolve_openapi_root(env)
9
+ path_params = env['action_dispatch.request.path_parameters'] || {}
10
+ path_params[:openapi_root] || openapi_root
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'miniswag/api/middleware'
4
+
5
+ module Miniswag
6
+ module Api
7
+ class Engine < ::Rails::Engine
8
+ isolate_namespace Miniswag::Api
9
+
10
+ initializer 'miniswag-api.initialize' do |_app|
11
+ middleware.use Miniswag::Api::Middleware, Miniswag::Api.config
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'yaml'
5
+ require 'rack/mime'
6
+
7
+ module Miniswag
8
+ module Api
9
+ class Middleware
10
+ def initialize(app, config)
11
+ @app = app
12
+ @config = config
13
+ end
14
+
15
+ def call(env)
16
+ path = env['PATH_INFO']
17
+ # Sanitize the filename for directory traversal by expanding, and ensuring
18
+ # it starts with the root directory.
19
+ openapi_root = @config.resolve_openapi_root(env)
20
+ filename = File.expand_path(File.join(openapi_root, path))
21
+ return @app.call(env) unless filename.start_with?(openapi_root.to_s)
22
+
23
+ if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
24
+ openapi = parse_file(filename)
25
+ @config.openapi_filter&.call(openapi, env)
26
+ mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
27
+ headers = { 'Content-Type' => mime }.merge(@config.openapi_headers || {})
28
+ body = unload_openapi(filename, openapi)
29
+
30
+ return [200, headers, [body]]
31
+ end
32
+
33
+ @app.call(env)
34
+ end
35
+
36
+ private
37
+
38
+ def parse_file(filename)
39
+ if /\.ya?ml$/.match?(filename)
40
+ YAML.safe_load(File.read(filename))
41
+ else
42
+ JSON.parse(File.read(filename))
43
+ end
44
+ end
45
+
46
+ def unload_openapi(filename, openapi)
47
+ if /\.ya?ml$/.match?(filename)
48
+ YAML.dump(openapi)
49
+ else
50
+ JSON.dump(openapi)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'miniswag/api/configuration'
4
+ require 'miniswag/api/engine' if defined?(Rails::Engine)
5
+
6
+ module Miniswag
7
+ module Api
8
+ def self.configure
9
+ yield(config)
10
+ end
11
+
12
+ def self.config
13
+ @config ||= Configuration.new
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: miniswag-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Edem Kumodzi
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: activesupport
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '7.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: railties
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '7.0'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '9.0'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '7.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '9.0'
52
+ description: |
53
+ Open up your API to the OpenAPI ecosystem by exposing OpenAPI files,
54
+ that describe your service, as JSON or YAML endpoints.
55
+ email:
56
+ - edem@sika.io
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - MIT-LICENSE
62
+ - lib/generators/miniswag/api/install/install_generator.rb
63
+ - lib/generators/miniswag/api/install/templates/miniswag_api.rb
64
+ - lib/miniswag/api.rb
65
+ - lib/miniswag/api/configuration.rb
66
+ - lib/miniswag/api/engine.rb
67
+ - lib/miniswag/api/middleware.rb
68
+ homepage: https://github.com/edemkumodzi/miniswag
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ source_code_uri: https://github.com/edemkumodzi/miniswag/tree/main/miniswag-api
73
+ rubygems_mfa_required: 'true'
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '3.1'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.7.0
89
+ specification_version: 4
90
+ summary: A Rails Engine that exposes OpenAPI files as JSON/YAML endpoints
91
+ test_files: []