Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions example/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
APICraft.specification do
openapi_version "3.1.0"

info do
title "Example API"
summary "Summary"
description do
<<~TEXT
This schema is used for testing purposes.
TEXT
end
terms_of_service "Terms of Service"
contact do
name "LassoID"
url "https://lassoid.ru/"
email "kirill@lassoid.ru"
end
license do
name "License"
identifier "License ID"
url "url"
end
version "1.0.0"
end

json_schema_dialect "dialect"

servers do
element do
url { "url" }
description { "description" }
variables do
var_name_1 do
enum { ["value11", "value12"] }
default { "value11" }
description { "description1" }
end
var_name_2 do
enum { ["value21", "value22"] }
default { "value21" }
description { "description2" }
end
end
end
end

paths do
route "/users" do
get do
reference { "endpoints/users_list" }
end
post do
reference { "endpoints/user_create" }
end
options do
# NO
end
head do
# NO
end
trace do
# NO
end
end
route "/users/{id}" do
parameters do
element do
name { "id" }
in { "path" }
required { true }
deprecated { false }
description { "User ID" }
schema do
type { "string" }
end
end
end

servers do
element do
url { "url2" }
end
end

get do
reference { "endpoints/user_get" }
end
put do
reference { "endpoints/user_update" }
end
patch do
reference { "endpoints/user_update" }
end
delete do
reference { "endpoints/user_delete" }
end
options do
# NO
end
head do
# NO
end
trace do
# NO
end
end
end

# components do
# schemas do
# ref { "./components/schemas.yml" }
# end
# parameters do
#
# end
# securitySchemes do

# end
# end

# security do
# [
# jwt_token do

# end,
# ]
# end

# tags do
# "users"
# end
end
8 changes: 7 additions & 1 deletion lib/apicraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@

module APICraft
class Error < StandardError; end
# Your code goes here...

class << self
def specification(&block)
@specification = Specification.new
@specification.instance_exec(&block)
end
end
end
17 changes: 17 additions & 0 deletions lib/apicraft/array_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module APICraft
class ArrayNode < Node
def element_node
raise NotImplementedError
end

def element(&block)
new_element = element_node.new
new_element.instance_exec(&block)
@elements << new_element

new_element
end
end
end
35 changes: 35 additions & 0 deletions lib/apicraft/info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module APICraft
class Info < Node
def title(value, &block)
@title = block ? instance_exec(&block).to_s : value.to_s
end

def summary(value, &block)
@summary = block ? instance_exec(&block).to_s : value.to_s
end

def description(value, &block)
@description = block ? instance_exec(&block).to_s : value.to_s
end

def terms_of_service(value, &block)
@terms_of_service = block ? instance_exec(&block).to_s : value.to_s
end

def contact(value, &block)
@contact = Contact.new
@contact.instance_exec(&block)
end

def license(value, &block)
@license = License.new
@license.instance_exec(&block)
end

def version(value, &block)
@version = block ? instance_exec(&block).to_s : value.to_s
end
end
end
19 changes: 19 additions & 0 deletions lib/apicraft/info/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module APICraft
class Info
class Contact < Node
def name(value, &block)
@name = block ? instance_exec(&block).to_s : value.to_s
end

def url(value, &block)
@url = block ? instance_exec(&block).to_s : value.to_s
end

def email(value, &block)
@email = block ? instance_exec(&block).to_s : value.to_s
end
end
end
end
19 changes: 19 additions & 0 deletions lib/apicraft/info/license.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module APICraft
class Info
class License < Node
def name(value, &block)
@name = block ? instance_exec(&block).to_s : value.to_s
end

def identifier(value, &block)
@identifier = block ? instance_exec(&block).to_s : value.to_s
end

def url(value, &block)
@url = block ? instance_exec(&block).to_s : value.to_s
end
end
end
end
13 changes: 13 additions & 0 deletions lib/apicraft/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module APICraft
class Node < BasicObject
class << self
def define_plain_field(method, *args, &block)
define_method(method) do |value, &block|
instance_variable_set("@#{method}", block ? instance_exec(&block) : value)
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/apicraft/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module APICraft
class Servers < Node

end
end
7 changes: 7 additions & 0 deletions lib/apicraft/servers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module APICraft
class Servers < ArrayNode
def element_node = Server
end
end
23 changes: 23 additions & 0 deletions lib/apicraft/specification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module APICraft
class Specification < Node
def openapi_version(value, &block)
@openapi_version = block ? instance_exec(&block).to_s : value.to_s
end

def info(&block)
@info = Info.new
@info.instance_exec(&block)
end

def json_schema_dialect(value, &block)
@json_schema_dialect = block ? instance_exec(&block).to_s : value.to_s
end

def servers(&block)
@servers = Server.new
@servers.instance_exec(&block)
end
end
end
Loading