Class: RubstApi::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rubst_api/routing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, endpoint:, methods:, params: {}, response_model: nil, status_code: 200, tags: [], summary: nil, description: nil, operation_id: nil, deprecated: false, include_in_schema: true, responses: {}, dependencies: [], name: nil) ⇒ Route

Returns a new instance of Route.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rubst_api/routing.rb', line 9

def initialize(path, endpoint:, methods:, params: {}, response_model: nil, status_code: 200,
               tags: [], summary: nil, description: nil, operation_id: nil, deprecated: false,
               include_in_schema: true, responses: {}, dependencies: [], name: nil, **)
  @path, @endpoint, @methods, @params = normalize_path(path), endpoint, Array(methods).map { |m| m.to_s.upcase }, params
  @response_model, @status_code, @tags = response_model, status_code, tags
  @summary, @description, @operation_id = summary, description, operation_id
  @deprecated, @include_in_schema, @responses = deprecated, include_in_schema, responses
  @dependencies, @name = dependencies, name
  @regexp, @path_names = compile(@path)
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def dependencies
  @dependencies
end

#deprecatedObject (readonly)

Returns the value of attribute deprecated.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def deprecated
  @deprecated
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def description
  @description
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def endpoint
  @endpoint
end

#include_in_schemaObject (readonly)

Returns the value of attribute include_in_schema.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def include_in_schema
  @include_in_schema
end

#methodsObject (readonly)

Returns the value of attribute methods.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def methods
  @methods
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def name
  @name
end

#operation_idObject (readonly)

Returns the value of attribute operation_id.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def operation_id
  @operation_id
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def path
  @path
end

#response_modelObject (readonly)

Returns the value of attribute response_model.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def response_model
  @response_model
end

#responsesObject (readonly)

Returns the value of attribute responses.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def responses
  @responses
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def status_code
  @status_code
end

#summaryObject (readonly)

Returns the value of attribute summary.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def summary
  @summary
end

#tagsObject (readonly)

Returns the value of attribute tags.



5
6
7
# File 'lib/rubst_api/routing.rb', line 5

def tags
  @tags
end

Instance Method Details

#allowed?(method) ⇒ Boolean

Returns:

  • (Boolean)


25
# File 'lib/rubst_api/routing.rb', line 25

def allowed?(method) = methods.include?(method) || (method == "HEAD" && methods.include?("GET"))

#match(request_path) ⇒ Object



20
21
22
23
# File 'lib/rubst_api/routing.rb', line 20

def match(request_path)
  match = @regexp.match(request_path)
  match&.named_captures || nil
end

#unique_idObject



27
28
29
# File 'lib/rubst_api/routing.rb', line 27

def unique_id
  operation_id || "#{name || endpoint_name}_#{path.gsub(/\W+/, "_")}_#{methods.first.downcase}".gsub(/\A_|_\z/, "")
end