class Apigen::Rest::Api
Api
is a self-contained definition of a REST API, includings its endpoints and data types.
Attributes
endpoints[R]
Public Class Methods
new()
click to toggle source
# File lib/apigen/rest/api.rb, line 27 def initialize @description = '' @endpoints = [] @model_registry = Apigen::ModelRegistry.new end
Public Instance Methods
endpoint(name, &block)
click to toggle source
Declares a specific endpoint.
# File lib/apigen/rest/api.rb, line 35 def endpoint(name, &block) error = if @endpoints.find { |e| e.name == name } "Endpoint :#{name} is declared twice." elsif !block_given? 'You must pass a block when calling `endpoint`.' end raise error unless error.nil? endpoint = Endpoint.new name @endpoints << endpoint endpoint.instance_eval(&block) end
migrate(*migration_classes)
click to toggle source
# File lib/apigen/rest/api.rb, line 64 def migrate(*migration_classes) migration_classes.each { |klass| klass.new(self).up } end
model(name, &block)
click to toggle source
Declares a data model.
# File lib/apigen/rest/api.rb, line 49 def model(name, &block) @model_registry.model name, &block end
models()
click to toggle source
# File lib/apigen/rest/api.rb, line 53 def models @model_registry.models end
to_s()
click to toggle source
# File lib/apigen/rest/api.rb, line 68 def to_s repr = "Endpoints:\n\n" repr += @endpoints.map(&:to_s).join "\n" repr += "\nTypes:\n\n" repr += @model_registry.to_s repr end
validate()
click to toggle source
# File lib/apigen/rest/api.rb, line 57 def validate @model_registry.validate @endpoints.each do |e| e.validate @model_registry end end