Class: RailsApiDocs::Doc::CurlRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-api-docs/doc/curl_renderer.rb

Overview

Renders a copy-pasteable multi-line curl command for an endpoint.

Body precedence:

1. endpoint["request_example"] (verbatim — user has full control)
2. JSON.pretty_generate of inferred sample from endpoint["body"]
3. no --data (no body at all)

Path param substitution prefers param when present, otherwise falls back to a type-based sample (always “1” for integers, “example” for everything else).

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, base_url:) ⇒ CurlRenderer

Returns a new instance of CurlRenderer.



18
19
20
21
# File 'lib/rails-api-docs/doc/curl_renderer.rb', line 18

def initialize(endpoint, base_url:)
  @endpoint = endpoint
  @base_url = base_url.to_s
end

Instance Method Details

#callObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails-api-docs/doc/curl_renderer.rb', line 23

def call
  lines = ["curl --request #{method} \\", "  --url #{url}"]

  all_headers = curl_headers
  all_headers.each do |name, value|
    lines[-1] += " \\"
    lines << "  --header '#{shell_escape("#{name}: #{value}")}'"
  end

  if body_present?
    lines[-1] += " \\"
    lines << "  --header 'Content-Type: application/json' \\"
    lines << "  --data '#{shell_escape(body_json)}'"
  end
  lines.join("\n")
end