Class: Apigen::Rest::Api
- Inherits:
-
Object
- Object
- Apigen::Rest::Api
- 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
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
Instance Method Summary collapse
-
#endpoint(name, &block) ⇒ Object
Declares a specific endpoint.
-
#initialize ⇒ Api
constructor
A new instance of Api.
- #migrate(*migration_classes) ⇒ Object
-
#model(name, &block) ⇒ Object
Declares a data model.
- #models ⇒ Object
- #to_s ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize ⇒ Api
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
#endpoints ⇒ Object (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 42 43 44 45 |
# 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) ⇒ Object
64 65 66 |
# File 'lib/apigen/rest/api.rb', line 64 def migrate(*migration_classes) migration_classes.each { |klass| klass.new(self).up } end |
#model(name, &block) ⇒ Object
Declares a data model.
49 50 51 |
# File 'lib/apigen/rest/api.rb', line 49 def model(name, &block) @model_registry.model name, &block end |
#models ⇒ Object
53 54 55 |
# File 'lib/apigen/rest/api.rb', line 53 def models @model_registry.models end |
#to_s ⇒ Object
68 69 70 71 72 73 74 |
# 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 ⇒ Object
57 58 59 60 61 62 |
# File 'lib/apigen/rest/api.rb', line 57 def validate @model_registry.validate @endpoints.each do |e| e.validate @model_registry end end |