class TestsDoc::Interaction

Public Instance Methods

record() click to toggle source
# File lib/tests_doc/interaction.rb, line 7
def record
  return if response.code.to_s == "500"

  TestsDoc::File.new(file_path, output, options).write_if_changed
end

Private Instance Methods

_path() click to toggle source
# File lib/tests_doc/interaction.rb, line 61
def _path
  _path = options.path ? options.path : request_path_without_format
  _path.gsub(/^\//, '') # remove initial / if present
end
description_content() click to toggle source
# File lib/tests_doc/interaction.rb, line 23
def description_content
  [
    "# #{request.method} #{_path}\n",
    options.description,
    "Rspec description: #{example.full_description}\n",
    formatted_example_location,
  ].compact.join("\n")
end
file_base_path() click to toggle source
# File lib/tests_doc/interaction.rb, line 57
def file_base_path
  ::File.join(*[TestsDoc.configuration.root_folder, TestsDoc.configuration.doc_folder, _path].reject(&:blank?))
end
file_path() click to toggle source
# File lib/tests_doc/interaction.rb, line 53
def file_path
  [file_base_path, options.key.to_s].reject(&:blank?).compact.join(TestsDoc.configuration.key_separator) + '.md'
end
formatted_example_location() click to toggle source
# File lib/tests_doc/interaction.rb, line 70
def formatted_example_location
  path = if TestsDoc.configuration.add_spec_file_number
           example.location
         else
           example.file_path
         end
  path = path.gsub(/^.\//, "")

  "[#{path}](/#{path.gsub(":", '#L')})"
end
output() click to toggle source
# File lib/tests_doc/interaction.rb, line 15
def output
  [
    description_content,
    params_content,
    response_content
  ].compact.join("\n\n")
end
params_content() click to toggle source
# File lib/tests_doc/interaction.rb, line 32
def params_content
  [
    "# Parameters\n",
    "```json",
    JSON.pretty_generate(request.request_parameters.merge(request.query_parameters).symbolize_keys),
    "```"
  ].join("\n")
end
request_path_without_format() click to toggle source
# File lib/tests_doc/interaction.rb, line 66
def request_path_without_format
  request.path.gsub(/(\.[\w]+)?$/, '') # remove .json if present
end
response_content() click to toggle source
# File lib/tests_doc/interaction.rb, line 41
def response_content
  [
    "# Response\n",
    "```",
    "HTTP CODE = #{response.code}",
    "```\n",
    "```json",
    (response.body.present? ? JSON.pretty_generate(JSON[response.body]) : ""),
    "```"
  ].join("\n")
end