class Fictium::Postman::V2Exporter::RequestFormatter

Constants

PATH_VARIABLE

Public Instance Methods

format(example) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 7
def format(example)
  {}.tap do |result|
    result.merge!(
      url: format_url(example),
      method: example.action.method.to_s.downcase,
      description: example.action.description,
      header: header_formatter.format(example.request)
    )
    add_optional_values(example, result)
  end
end

Private Instance Methods

add_optional_values(example, result) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 80
def add_optional_values(example, result)
  return if example.request.blank?

  body = body_formatter.format(example.request)
  result[:body] = body if body.present?
end
api_url() click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 52
def api_url
  Fictium.configuration.postman.api_url
end
body_formatter() click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 87
def body_formatter
  @body_formatter ||= BodyFormatter.new
end
convert_path(action) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 37
def convert_path(action)
  action.full_path.gsub(PATH_VARIABLE, ':\k<var>')
end
format_path(example) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 41
def format_path(example)
  path = convert_path(example.action).split('/')
  path.shift
  path
end
format_query(example) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 56
def format_query(example)
  return [] if example.request.blank?

  example.request[:query_parameters].map do |key, value|
    result = { key: key, value: value.to_json }
    description = example.action[:query][key] && example.action[:query][key][:description]
    result[:description] = description if description.present?
    result
  end
end
format_url(example) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 21
def format_url(example)
  {
    raw: full_path(example),
    host: [api_url],
    path: format_path(example),
    query: format_query(example),
    variable: format_variable(example)
  }
end
format_variable(example) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 67
def format_variable(example)
  return [] if example.request.blank?

  [].tap do |result|
    example.action[:path].each do |name, _|
      data = { id: name, key: name }
      value = example.request[:path_parameters][name.to_sym]
      data[:value] = value if value.present?
      result << data
    end
  end
end
full_path(example) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 31
def full_path(example)
  return '' if example.request.blank?

  "#{api_url}#{convert_path(example.action)}#{query_params_for(example)}"
end
header_formatter() click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 91
def header_formatter
  @header_formatter ||= HeaderFormatter.new
end
query_params_for(example) click to toggle source
# File lib/fictium/exporters/postman/v2_exporter/request_formatter.rb, line 47
def query_params_for(example)
  params = example.request[:query_parameters]
  params.present? ? "?#{params.to_query}" : ''
end