Skip to content

Commit 300509c

Browse files
committed
initialize gem
1 parent bc73dc5 commit 300509c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2263
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
.DS_Store
10+
11+
# rspec failure tracking
12+
.rspec_status
13+
14+
# rubocop cache of remote rules file
15+
.rubocop-https---dev-k-io-rubocop-*
16+
17+
*.gem

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.rubocop.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
inherit_from:
2+
- https://dev.k.io/rubocop/rubocop.yml
3+
4+
AllCops:
5+
TargetRubyVersion: 2.6
6+
Exclude:
7+
- Rakefile
8+
- "bin/**/*"
9+
10+
# Always use double quotes
11+
Style/StringLiterals:
12+
EnforcedStyle: double_quotes
13+
14+
Style/Documentation:
15+
Enabled: false
16+
17+
Metrics/CyclomaticComplexity:
18+
Enabled: false
19+
20+
Metrics/PerceivedComplexity:
21+
Enabled: false
22+
23+
Metrics/AbcSize:
24+
Enabled: false
25+
26+
Metrics/MethodLength:
27+
Max: 50

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.2.2

Gemfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
# Specify your gem's dependencies in apia-open_api.gemspec
6+
gemspec
7+
8+
gem "rake", "~> 13.0"
9+
10+
gem "rspec", "~> 3.0"
11+
12+
gem "rubocop", "~> 1.21"
13+
14+
gem "apia", "~> 3.5"

Gemfile.lock

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
PATH
2+
remote: .
3+
specs:
4+
apia-open_api (0.1.0)
5+
activesupport (>= 6)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
activesupport (7.0.6)
11+
concurrent-ruby (~> 1.0, >= 1.0.2)
12+
i18n (>= 1.6, < 2)
13+
minitest (>= 5.1)
14+
tzinfo (~> 2.0)
15+
apia (3.5.0)
16+
json
17+
rack
18+
ast (2.4.2)
19+
concurrent-ruby (1.2.2)
20+
diff-lcs (1.5.0)
21+
i18n (1.14.1)
22+
concurrent-ruby (~> 1.0)
23+
json (2.6.3)
24+
minitest (5.20.0)
25+
parallel (1.23.0)
26+
parser (3.2.2.3)
27+
ast (~> 2.4.1)
28+
racc
29+
racc (1.7.1)
30+
rack (3.0.8)
31+
rainbow (3.1.1)
32+
rake (13.0.6)
33+
regexp_parser (2.8.1)
34+
rexml (3.2.6)
35+
rspec (3.12.0)
36+
rspec-core (~> 3.12.0)
37+
rspec-expectations (~> 3.12.0)
38+
rspec-mocks (~> 3.12.0)
39+
rspec-core (3.12.2)
40+
rspec-support (~> 3.12.0)
41+
rspec-expectations (3.12.3)
42+
diff-lcs (>= 1.2.0, < 2.0)
43+
rspec-support (~> 3.12.0)
44+
rspec-mocks (3.12.6)
45+
diff-lcs (>= 1.2.0, < 2.0)
46+
rspec-support (~> 3.12.0)
47+
rspec-support (3.12.1)
48+
rubocop (1.52.0)
49+
json (~> 2.3)
50+
parallel (~> 1.10)
51+
parser (>= 3.2.0.0)
52+
rainbow (>= 2.2.2, < 4.0)
53+
regexp_parser (>= 1.8, < 3.0)
54+
rexml (>= 3.2.5, < 4.0)
55+
rubocop-ast (>= 1.28.0, < 2.0)
56+
ruby-progressbar (~> 1.7)
57+
unicode-display_width (>= 2.4.0, < 3.0)
58+
rubocop-ast (1.29.0)
59+
parser (>= 3.2.1.0)
60+
ruby-progressbar (1.13.0)
61+
tzinfo (2.0.6)
62+
concurrent-ruby (~> 1.0)
63+
unicode-display_width (2.4.2)
64+
65+
PLATFORMS
66+
arm64-darwin-22
67+
68+
DEPENDENCIES
69+
apia (~> 3.5)
70+
apia-open_api!
71+
rake (~> 13.0)
72+
rspec (~> 3.0)
73+
rubocop (~> 1.21)
74+
75+
BUNDLED WITH
76+
2.4.19

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Krystal Hosting Ltd
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
13+
all 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
21+
THE SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,96 @@
11
# Apia OpenAPI Specification
22

33
This gem can generate an [OpenAPI](https://www.openapis.org/) compatible schema from an API implemented using [Apia](https://github.com/krystal/apia).
4+
5+
## Installation
6+
7+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8+
9+
Install the gem and add to the application's Gemfile by executing:
10+
11+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
12+
13+
If bundler is not being used to manage dependencies, install the gem by executing:
14+
15+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
16+
17+
## Usage
18+
19+
The schema can be mounted in much the same way as an [Apia API](https://github.com/krystal/apia) itself.
20+
21+
For example, for a Ruby on Rails application:
22+
23+
```ruby
24+
module MyApp
25+
class Application < Rails::Application
26+
27+
config.middleware.use Apia::OpenApi::Rack,
28+
api_class: "CoreAPI::Base",
29+
schema_path: "/core/v1/schema/openapi.json",
30+
base_url: "http://katapult-api.localhost/core/v1"
31+
32+
end
33+
end
34+
```
35+
36+
Where `CoreAPI::Base` is the name of the API class that inherits from `Apia::API`.
37+
38+
## Generating a client library from the spec
39+
40+
It's possible to generate a client library from the generated OpenAPI schema using [OpenAPI Generator](https://openapi-generator.tech/).
41+
42+
For example we can generate a Ruby client with the following:
43+
44+
```bash
45+
brew install openapi-generator
46+
openapi-generator generate -i openapi.json -g ruby -o openapi-client --additional-properties=gemName=myapp-openapi-client,moduleName=MyAppOpenAPIClient
47+
```
48+
49+
The generated client will be in the `openapi-client` directory and will contain a readme with instructions on how to use it.
50+
51+
## Development
52+
53+
After checking out the repo, run `bin/setup` to install dependencies.
54+
55+
In `/examples` there is an example Apia API application that can be used to try out the gem.
56+
57+
Run `rackup` from the root of `/examples` to start the [rack app](https://github.com/rack/rack) running the example API.
58+
To view the generated OpenAPI schema, visit: http://127.0.0.1:9292/core/v1/schema/openapi.json
59+
`/examples/config.ru` shows how to mount the schema endpoint.
60+
61+
The generated schema can be viewed, validated and tried out using the online [Swagger Editor](https://editor.swagger.io/). You'll need to add the bearer token to the swagger editor to authenticate the requests. After that, they should work as expected. The bearer token is defined in main_authenticator.rb.
62+
63+
Currently the online swagger-editor only allows the OpenAPI schema v3.0.0. But it's also possible to run the swagger-editor locally, which allows us to check against v3.1.0.
64+
65+
e.g with this docker-compose.yml file:
66+
67+
```yml
68+
version: "3.3"
69+
services:
70+
swagger-editor:
71+
image: swaggerapi/swagger-editor:next-v5
72+
container_name: "swagger-editor"
73+
ports:
74+
- "8081:80"
75+
```
76+
77+
Run `docker-compose up` and visit http://localhost:8081 to view the swagger editor.
78+
79+
### Tests and Linting
80+
81+
- `bin/rspec`
82+
- `bin/rubocop`
83+
84+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85+
86+
## Releasing a new version
87+
88+
TODO: Write instructions for releasing a new version of the gem.
89+
90+
## Contributing
91+
92+
Bug reports and pull requests are welcome on GitHub at https://github.com/krystal/apia-open_api.
93+
94+
## License
95+
96+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require "bundler/gem_tasks"
4+
require "rspec/core/rake_task"
5+
6+
RSpec::Core::RakeTask.new(:spec)
7+
8+
require "rubocop/rake_task"
9+
10+
RuboCop::RakeTask.new
11+
12+
task default: %i[spec rubocop]

apia-open_api.gemspec

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "lib/apia/open_api/version"
4+
5+
Gem::Specification.new do |spec|
6+
spec.name = "apia-open_api"
7+
spec.version = Apia::OpenApi::VERSION
8+
spec.authors = ["Paul Sturgess"]
9+
10+
spec.summary = "Apia OpenAPI spec generator"
11+
spec.homepage = "https://github.com/krystal/apia-openapi"
12+
spec.license = "MIT"
13+
spec.required_ruby_version = ">= 2.6.0"
14+
15+
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
16+
17+
spec.metadata["homepage_uri"] = spec.homepage
18+
spec.metadata["source_code_uri"] = "https://github.com/krystal/apia-openapi"
19+
spec.metadata["changelog_uri"] = "https://github.com/krystal/apia-openapi/changelog.md"
20+
21+
spec.metadata["rubygems_mfa_required"] = "true"
22+
23+
# Specify which files should be added to the gem when it is released.
24+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25+
spec.files = Dir.chdir(__dir__) do
26+
`git ls-files -z`.split("\x0").reject do |f|
27+
(File.expand_path(f) == __FILE__) ||
28+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
29+
end
30+
end
31+
spec.bindir = "exe"
32+
spec.executables = spec.files.grep(/\Aexe\//) { |f| File.basename(f) }
33+
spec.require_paths = ["lib"]
34+
35+
spec.add_dependency "activesupport", ">= 6"
36+
end

bin/console

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "apia/open_api"
6+
7+
# You can add fixtures and/or initialization code here to make experimenting
8+
# with your gem easier. You can also use a different console, if you like.
9+
10+
require "irb"
11+
IRB.start(__FILE__)

bin/rspec

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rspec' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
bundle_binstub = File.expand_path("bundle", __dir__)
14+
15+
if File.file?(bundle_binstub)
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17+
load(bundle_binstub)
18+
else
19+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21+
end
22+
end
23+
24+
require "rubygems"
25+
require "bundler/setup"
26+
27+
load Gem.bin_path("rspec-core", "rspec")

bin/rubocop

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rubocop' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
bundle_binstub = File.expand_path("bundle", __dir__)
14+
15+
if File.file?(bundle_binstub)
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17+
load(bundle_binstub)
18+
else
19+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21+
end
22+
end
23+
24+
require "rubygems"
25+
require "bundler/setup"
26+
27+
load Gem.bin_path("rubocop", "rubocop")

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

examples/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Apia Examples
2+
3+
This directory contains some example applications which might be useful for reference purposes.
4+
5+
These examples can actually be mounted by runing `rackup` in the root of the examples directory.

0 commit comments

Comments
 (0)