Class: DocsUI::RequestExample

Inherits:
Phlex::HTML
  • Object
show all
Defined in:
app/components/docs_ui/request_example.rb

Overview

One structured request declaration → one code tab per configured API client. Declare method/path/body once and every client (curl, javascript, ruby, python by default; plus whatever a site adds) renders its own snippet, in a DocsUI::Example so the sticky global language preference keeps working.

render DocsUI::RequestExample.new(
method: :post, path: "/v1/webhook_endpoints",
body: { url: "https://example.com/hook", events: ["payment.paid"] }
)

# only some tabs, in a chosen order:
render DocsUI::RequestExample.new(method: :get, path: "/v1/things", clients: %i[curl ruby])

The base URL and an example auth header come from config (DocsKit.configuration.api_base_url / #api_auth_header); the client set comes from #api_clients (defaults + site overrides). This replaces the per-client heredoc a docs page used to hand-write once per endpoint per language.

Instance Method Summary collapse

Constructor Details

#initialize(method:, path:, body: nil, query: nil, headers: {}, clients: nil) ⇒ RequestExample

Returns a new instance of RequestExample.



22
23
24
25
26
27
28
29
# File 'app/components/docs_ui/request_example.rb', line 22

def initialize(method:, path:, body: nil, query: nil, headers: {}, clients: nil)
  @method = method
  @path = path
  @body = body
  @query = query || {}
  @headers = headers || {}
  @clients = clients
end

Instance Method Details

#view_templateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/docs_ui/request_example.rb', line 31

def view_template
  request = build_request
  selected = selected_clients

  render DocsUI::Example.new do |ex|
    selected.each do |token, client|
      ex.code(
        token,
        lexer: client.lexer,
        label: client.label,
        filename: client.filename_for(request)
      ) { client.render(request) }
    end
  end
end