-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added cli command to validate all spec files * improved init and setup via CLI * added file generation from cli * rubocop offences * added ruby 3.3 into the matrix * README.md update * README.md update * README.md update * README.md update * README.md update
- Loading branch information
1 parent
82027ea
commit b5d9f0f
Showing
13 changed files
with
284 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ jobs: | |
- "3.0" | ||
- "3.1" | ||
- "3.2" | ||
- "3.3" | ||
- ruby-head | ||
|
||
runs-on: ${{ matrix.os }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :apicraft do | ||
desc "Generate an example spec file" | ||
|
||
task generate: :environment do |_t, _args| | ||
arguments = ARGV.reduce({}) do |final, current| | ||
key, val = current.split("=").map(&:strip) | ||
final.merge!({ | ||
key => val | ||
}) | ||
end | ||
|
||
filepath = arguments["file"] | ||
template = File.expand_path("../templates/openapi.example.yaml", __dir__) | ||
|
||
# root path of all contracts | ||
contracts_path = Apicraft.config.contracts_path | ||
|
||
# Split the filepath into parts to extract the directory structure and file name | ||
path_parts = filepath.split("/") | ||
dir_path = File.join(contracts_path, *path_parts[0..-2]) | ||
file_name = "#{path_parts[-1]}.yaml" | ||
|
||
# Create the directory if it doesn't exist | ||
FileUtils.mkdir_p(dir_path) unless Dir.exist?(dir_path) | ||
|
||
File.write(File.join(dir_path, file_name), File.read(template)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :apicraft do | ||
desc "Initialize apicraft" | ||
task init: :environment do | ||
# Setup the apicraft initializer | ||
destination = Rails.root.join("config", "initializers", "apicraft.rb") | ||
if File.exist?(destination) | ||
puts "File already exists: #{destination}" | ||
else | ||
template = File.expand_path("../templates/initializer.rb", __dir__) | ||
FileUtils.cp(template, destination) | ||
puts "Apicraft initializer created at config/initializers/apicraft.rb" | ||
end | ||
|
||
# Create the default contracts directory | ||
contracts_path = Apicraft.config.default_contracts_path | ||
FileUtils.mkdir_p(contracts_path) unless Dir.exist?(contracts_path) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :apicraft do | ||
desc "Validate all contracts" | ||
task validate: :environment do | ||
Apicraft::Validator.validate! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
Apicraft::Web::App.use do |user, password| | ||
[user, password] == %w[admin password] | ||
end | ||
|
||
Apicraft.configure do |config| | ||
config.contracts_path = Rails.root.join("app", "contracts") | ||
|
||
# Enables or disables the mocking features | ||
# Defaults to true | ||
config.mocks = true | ||
|
||
# Enables or disables the introspection features | ||
# Defaults to true | ||
config.introspection = true | ||
|
||
# allows you to enforce stricter validation of $ref | ||
# references in your OpenAPI specifications. | ||
# When this option is enabled, the parser will raise | ||
# an error if any $ref references in your OpenAPI | ||
# document are invalid, ensuring that all references | ||
# are correctly defined and resolved. | ||
# Defaults to true | ||
config.strict_reference_validation = true | ||
|
||
# When simulating delay using the mocks, the max | ||
# delay in seconds that can be simulated | ||
config.max_allowed_delay = 0 | ||
|
||
config.headers = { | ||
# The name of the header used to control | ||
# the response code of the mock | ||
# Defaults to Apicraft-Response-Code | ||
response_code: "Apicraft-Response-Code", | ||
|
||
# The name of the header to introspect the API. | ||
# Defaults to Apicraft-Introspect | ||
introspect: "Apicraft-Introspect", | ||
|
||
# The name of the header to mock the API. | ||
# Defaults to Apicraft-Mock | ||
mock: "Apicraft-Mock", | ||
|
||
# Delay simulation header name | ||
delay: "Apicraft-Delay" | ||
} | ||
|
||
config.request_validation = { | ||
enabled: true, | ||
|
||
# Return the http code for validation errors, defaults to 400 | ||
http_code: 400, | ||
|
||
# Return a custom response body, defaults to `{ message: "..." }` | ||
response_body: proc do |ex| | ||
{ | ||
message: ex.message | ||
} | ||
end | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: Sample API | ||
version: 1.0.0 | ||
description: API template to manage users. | ||
|
||
servers: | ||
- url: https://api.example.com | ||
|
||
paths: | ||
/users: | ||
get: | ||
summary: Get a list of users | ||
responses: | ||
'200': | ||
description: A list of users | ||
content: | ||
application/json: | ||
schema: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/User' | ||
post: | ||
summary: Create a new user | ||
requestBody: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/User' | ||
responses: | ||
'201': | ||
description: User created successfully | ||
|
||
/users/{id}: | ||
get: | ||
summary: Get a user by id | ||
parameters: | ||
- name: id | ||
in: path | ||
required: true | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: A user object | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/User' | ||
|
||
components: | ||
schemas: | ||
User: | ||
type: object | ||
properties: | ||
id: | ||
type: string | ||
name: | ||
type: string | ||
email: | ||
type: string | ||
required: | ||
- id | ||
- name | ||
Oops, something went wrong.