class RspecApiDocumentation::ClientBase

Base client class that documents all requests that go through it.

client.get("/orders", { :page => 2 }, { "Accept" => "application/json" })

Public Instance Methods

delete(*args) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 23
def delete(*args)
  process :delete, *args
end
get(*args) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 11
def get(*args)
  process :get, *args
end
head(*args) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 27
def head(*args)
  process :head, *args
end
patch(*args) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 31
def patch(*args)
  process :patch, *args
end
post(*args) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 15
def post(*args)
  process :post, *args
end
put(*args) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 19
def put(*args)
  process :put, *args
end
response_status() click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 35
def response_status
  status
end

Private Instance Methods

clean_out_uploaded_data(params, request_body) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 98
def clean_out_uploaded_data(params, request_body)
  params.each do |value|
    if [Hash, Array].member? value.class
      request_body = if value.respond_to?(:has_key?) && value.has_key?(:tempfile)
                       data = value[:tempfile].read
                       request_body.gsub(data, "[uploaded data]")
                     else
                       clean_out_uploaded_data(value, request_body)
                     end
    end
  end
  request_body
end
document_example(method, path) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 52
def document_example(method, path)
  return unless metadata[:document]

  request_body = read_request_body

  request_metadata = {}

  if request_content_type =~ /multipart\/form-data/ && respond_to?(:handle_multipart_body, true)
    request_body = handle_multipart_body(request_headers, request_body)
  end

  request_metadata[:request_method] = method
  request_metadata[:request_path] = path
  request_metadata[:request_body] = request_body.empty? ? nil : request_body.force_encoding("UTF-8")
  request_metadata[:request_headers] = request_headers
  request_metadata[:request_query_parameters] = query_hash
  request_metadata[:request_content_type] = request_content_type
  request_metadata[:response_status] = status
  request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[status]
  request_metadata[:response_body] = record_response_body(response_content_type, response_body)
  request_metadata[:response_headers] = response_headers
  request_metadata[:response_content_type] = response_content_type
  request_metadata[:curl] = Curl.new(method, path, request_body, request_headers)

  metadata[:requests] ||= []
  metadata[:requests] << request_metadata
end
headers(method, path, params, request_headers) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 84
def headers(method, path, params, request_headers)
  request_headers || {}
end
process(method, path, params = {}, headers ={}) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 41
def process(method, path, params = {}, headers ={})
  do_request(method, path, params, headers)
  document_example(method.to_s.upcase, path)
end
query_hash() click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 80
def query_hash
  Rack::Utils.parse_nested_query(query_string)
end
read_request_body() click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 46
def read_request_body
  input = last_request.env["rack.input"]
  input.rewind
  input.read
end
record_response_body(response_content_type, response_body) click to toggle source
# File lib/rspec_api_documentation/client_base.rb, line 88
def record_response_body(response_content_type, response_body)
  return nil if response_body.empty?
  if response_body.encoding == Encoding::ASCII_8BIT
    "[binary data]"
  else
    formatter = RspecApiDocumentation.configuration.response_body_formatter
    return formatter.call(response_content_type, response_body)
  end
end