Class: Apigen::Rest::Endpoint
- Inherits:
-
Object
- Object
- Apigen::Rest::Endpoint
- Defined in:
- lib/apigen/rest/endpoint.rb
Overview
Endpoint is a definition of a specific endpoint in the API, e.g. /users with GET method.
Constant Summary collapse
- PATH_PARAMETER_REGEX =
/\{(\w+)\}/
Instance Attribute Summary collapse
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
-
#path_parameters ⇒ Object
readonly
Returns the value of attribute path_parameters.
-
#query_parameters ⇒ Object
readonly
Returns the value of attribute query_parameters.
Instance Method Summary collapse
-
#initialize(name) ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#input(&block) ⇒ Object
Declares the input type of an endpoint.
-
#method(method = nil) ⇒ Object
Declares the HTTP method.
-
#output(name, &block) ⇒ Object
Declares the output of an endpoint for a given status code.
-
#path(path = nil, &block) ⇒ Object
Declares the endpoint path relative to the host.
-
#query(&block) ⇒ Object
Declares query parameters.
- #to_s ⇒ Object
-
#update_output(name, &block) ⇒ Object
Updates an already-declared output.
- #validate(model_registry) ⇒ Object
Constructor Details
#initialize(name) ⇒ Endpoint
Returns a new instance of Endpoint.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/apigen/rest/endpoint.rb', line 19 def initialize(name) @name = name @method = nil @path = nil @path_parameters = Apigen::ObjectType.new @query_parameters = Apigen::ObjectType.new @input = nil @outputs = [] @description = nil end |
Instance Attribute Details
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
15 16 17 |
# File 'lib/apigen/rest/endpoint.rb', line 15 def outputs @outputs end |
#path_parameters ⇒ Object (readonly)
Returns the value of attribute path_parameters.
16 17 18 |
# File 'lib/apigen/rest/endpoint.rb', line 16 def path_parameters @path_parameters end |
#query_parameters ⇒ Object (readonly)
Returns the value of attribute query_parameters.
17 18 19 |
# File 'lib/apigen/rest/endpoint.rb', line 17 def query_parameters @query_parameters end |
Instance Method Details
#input(&block) ⇒ Object
Declares the input type of an endpoint.
63 64 65 66 67 |
# File 'lib/apigen/rest/endpoint.rb', line 63 def input(&block) return @input unless block_given? @input = Input.new @input.instance_eval(&block) end |
#method(method = nil) ⇒ Object
Declares the HTTP method.
32 33 34 35 36 37 38 39 40 |
# File 'lib/apigen/rest/endpoint.rb', line 32 def method(method = nil) return @method unless method case method when :get, :post, :put, :delete @method = method else raise "Unknown HTTP method :#{method}." end end |
#output(name, &block) ⇒ Object
Declares the output of an endpoint for a given status code.
71 72 73 74 75 76 77 78 |
# File 'lib/apigen/rest/endpoint.rb', line 71 def output(name, &block) raise "Endpoint :#{@name} declares the output :#{name} twice." if @outputs.find { |o| o.name == name } output = Output.new name @outputs << output raise 'You must pass a block when calling `output`.' unless block_given? output.instance_eval(&block) output end |
#path(path = nil, &block) ⇒ Object
Declares the endpoint path relative to the host.
44 45 46 47 48 49 50 51 52 |
# File 'lib/apigen/rest/endpoint.rb', line 44 def path(path = nil, &block) return @path unless path @path = path if PATH_PARAMETER_REGEX.match path set_path_parameters(path, &block) elsif block_given? raise 'A path block was provided but no URL parameter was found.' end end |
#query(&block) ⇒ Object
Declares query parameters.
56 57 58 59 |
# File 'lib/apigen/rest/endpoint.rb', line 56 def query(&block) raise 'You must pass a block when calling `query`.' unless block_given? @query_parameters.instance_eval(&block) end |
#to_s ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/apigen/rest/endpoint.rb', line 97 def to_s repr = "#{@name}:" input_str = @input.to_s repr += " #{input_str}" unless input_str.empty? @outputs.each do |output| repr += "\n-> #{output}" end repr += "\n" repr end |
#update_output(name, &block) ⇒ Object
Updates an already-declared output.
82 83 84 85 86 87 88 |
# File 'lib/apigen/rest/endpoint.rb', line 82 def update_output(name, &block) output = @outputs.find { |o| o.name == name } raise "Endpoint :#{@name} never declares the output :#{name} so it cannot be updated." unless output raise 'You must pass a block when calling `update_output`.' unless block_given? output.instance_eval(&block) output end |
#validate(model_registry) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/apigen/rest/endpoint.rb', line 90 def validate(model_registry) validate_properties validate_input(model_registry) validate_path_parameters(model_registry) validate_outputs(model_registry) end |