Class: Apigen::Rest::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/apigen/rest/api.rb

Overview

Api is a self-contained definition of a REST API, includings its endpoints and data types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



27
28
29
30
31
# File 'lib/apigen/rest/api.rb', line 27

def initialize
  @description = ''
  @endpoints = []
  @model_registry = Apigen::ModelRegistry.new
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



24
25
26
# File 'lib/apigen/rest/api.rb', line 24

def endpoints
  @endpoints
end

Instance Method Details

#endpoint(name, &block) ⇒ Object

Declares a specific endpoint.



35
36
37
38
39
40
41
# File 'lib/apigen/rest/api.rb', line 35

def endpoint(name, &block)
  raise "Endpoint :#{name} is declared twice." if @endpoints.find { |e| e.name == name }
  endpoint = Endpoint.new name
  @endpoints << endpoint
  raise 'You must pass a block when calling `endpoint`.' unless block_given?
  endpoint.instance_eval(&block)
end

#migrate(*migration_classes) ⇒ Object



60
61
62
# File 'lib/apigen/rest/api.rb', line 60

def migrate(*migration_classes)
  migration_classes.each { |klass| klass.new(self).up }
end

#model(name, &block) ⇒ Object

Declares a data model.



45
46
47
# File 'lib/apigen/rest/api.rb', line 45

def model(name, &block)
  @model_registry.model name, &block
end

#modelsObject



49
50
51
# File 'lib/apigen/rest/api.rb', line 49

def models
  @model_registry.models
end

#to_sObject



64
65
66
67
68
69
70
# File 'lib/apigen/rest/api.rb', line 64

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

#validateObject



53
54
55
56
57
58
# File 'lib/apigen/rest/api.rb', line 53

def validate
  @model_registry.validate
  @endpoints.each do |e|
    e.validate @model_registry
  end
end