class Brainstem::ApiDocs::Formatters::OpenApiSpecification::Version2::EndpointFormatter

Attributes

endpoint[RW]
endpoint_key[RW]
http_method[RW]
output[RW]
presenter[RW]

Public Class Methods

new(endpoint, options = {}) click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 26
def initialize(endpoint, options = {})
  self.endpoint     = endpoint
  self.presenter    = endpoint.presenter
  self.endpoint_key = formatted_url
  self.http_method  = format_http_method(endpoint)
  self.output       = { endpoint_key => { http_method => {} } }.with_indifferent_access

  super options
end

Public Instance Methods

call() click to toggle source
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 36
def call
  return {} if endpoint.nodoc?

  format_summary!
  format_optional_info!
  format_security!
  format_tags!
  format_parameters!
  format_response!

  output
end

Private Instance Methods

description() click to toggle source

Format the endpoint description

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 69
def description
  endpoint.description
end
format_optional_info!() click to toggle source

Adds the following properties.

- description
- operation_id
- consumes
- produces
- schemes
- external_docs
- deprecated
# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 105
def format_optional_info!
  info = {
    description:    format_sentence(description),
    operation_id:   endpoint.operation_id,
    consumes:       endpoint.consumes,
    produces:       endpoint.produces,
    schemes:        endpoint.schemes,
    external_docs:  endpoint.external_docs,
    deprecated:     endpoint.deprecated,
  }.reject { |_,v| v.blank? }

  output[endpoint_key][http_method].merge!(info)
end
format_parameters!() click to toggle source

Formats each parameter.

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 140
def format_parameters!
  output[endpoint_key][http_method].merge!(
    parameters: ::Brainstem::ApiDocs::FORMATTERS[:parameters][:oas_v2].call(endpoint)
  )
end
format_response!() click to toggle source

Formats the response.

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 149
def format_response!
  output[endpoint_key][http_method].merge!(
    responses: ::Brainstem::ApiDocs::FORMATTERS[:response][:oas_v2].call(endpoint)
  )
end
format_security!() click to toggle source

Adds the security schemes for the given endpoint.

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 122
def format_security!
  return if endpoint.security.nil?

  output[endpoint_key][http_method].merge! security: endpoint.security
end
format_summary!() click to toggle source

Formats the summary as given, falling back to the humanized action name.

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 91
def format_summary!
  output[endpoint_key][http_method].merge! summary: summary.to_s.strip
end
format_tags!() click to toggle source

Adds the tags for the given endpoint.

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 131
def format_tags!
  tag_name = endpoint.controller.tag || format_tag_name(endpoint.controller.name)

  output[endpoint_key][http_method].merge! tags: [tag_name]
end
formatted_url() click to toggle source

Formats the actual URI

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 76
def formatted_url
  endpoint.path
    .gsub('(.:format)', '')
    .gsub(/(:(?<param>\w+))/, '{\k<param>}')
    .gsub(Brainstem::ApiDocs.base_path, '')
end
summary() click to toggle source

Format the endpoint summary

# File lib/brainstem/api_docs/formatters/open_api_specification/version_2/endpoint_formatter.rb, line 62
def summary
  endpoint.title
end