Module: Minitest::Apidoc::Methods

Includes:
Rack::Test::Methods
Defined in:
lib/minitest/apidoc/methods.rb

Constant Summary collapse

VERBS =
%w[get head post put patch delete options]

Instance Method Summary collapse

Instance Method Details

#_request(verb, uri, params = {}, env = {}, &block) ⇒ Object

Performs a rack-test request while also saving the metadata necessary for documentation. Detects if the response is JSON (naively by just trying to parse it as JSON). If it is, formats the response nicely and also yields the data as parsed JSON object instead of raw text.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/minitest/apidoc/methods.rb', line 26

def _request(verb, uri, params={}, env={}, &block)
  send("rack_test_#{verb}", uri, params, env)

  self.class.[:request] = "#{verb.upcase} #{last_request.fullpath}"
  self.class.[:request] << $/ + last_request.body.read if last_request.body

  begin
    response_data = JSON.load(last_response.body)
    formatted_response = JSON.pretty_generate(response_data)
  rescue JSON::ParserError
    response_data = last_response.body
    formatted_response = last_response.body
  end

  self.class.[:response] = formatted_response
  block.call(response_data)
end