Class: DocsUI::JsonResponse

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

Overview

A pretty-printed JSON response block. Give it a Ruby Hash (deep-stringified and JSON.pretty_generate'd) or a pre-formatted String; it renders a DocsUI::Code with the json lexer and a filename title bar. Kills the hand-rolled deep_stringify + JSON.pretty_generate every API page was copy-pasting.

render DocsUI::JsonResponse.new({ id: "obj_1", status: "active" })
render DocsUI::JsonResponse.new(raw_json_string, filename: "webhook.json")

A Hash with symbol keys renders as real JSON (string keys, no :symbol / => leaking through). A String is passed through verbatim (already formatted).

Instance Method Summary collapse

Constructor Details

#initialize(body, filename: "response.json") ⇒ JsonResponse

Returns a new instance of JsonResponse.



17
18
19
20
# File 'app/components/docs_ui/json_response.rb', line 17

def initialize(body, filename: "response.json")
  @body = body
  @filename = filename
end

Instance Method Details

#view_templateObject



22
23
24
# File 'app/components/docs_ui/json_response.rb', line 22

def view_template
  render DocsUI::Code.new(json_source, lexer: :json, filename: @filename)
end