Class: DocsKit::ApiClient

Inherits:
Data
  • Object
show all
Defined in:
lib/docs_kit/api_client.rb

Overview

One API-example language tab: a label, a Rouge lexer, a filename (a String or a (request) -> String proc), and a template — a (DocsKit::ApiRequest) -> String callable that renders the snippet. DocsUI::RequestExample turns each configured client into one DocsUI::Example tab.

DocsKit::ApiClient.new(
label: "cURL", lexer: :curl, filename: "request.sh",
template: ->(req) { "curl -X #{req.http_method} '#{req.url}'" }
)

The gem ships four generic-HTTP defaults (DEFAULTS); a site overrides or extends them via DocsKit.configuration.api_clients (SDK-flavored snippets, a cli tab, ...).

Constant Summary collapse

DEFAULTS =

The four generic-HTTP clients every site starts from. Order is stable (curl → javascript → ruby → python) and preserved when a site merges its own.

{
  curl: ApiClient.new(
    label: "cURL", lexer: :curl, filename: "request.sh",
    template: ApiTemplates.method(:curl)
  ),
  javascript: ApiClient.new(
    label: "JavaScript", lexer: :javascript, filename: "request.js",
    template: ApiTemplates.method(:javascript)
  ),
  ruby: ApiClient.new(
    label: "Ruby", lexer: :ruby, filename: "request.rb",
    template: ApiTemplates.method(:ruby)
  ),
  python: ApiClient.new(
    label: "Python", lexer: :python, filename: "request.py",
    template: ApiTemplates.method(:python)
  )
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, lexer:, template:, filename: nil) ⇒ ApiClient

Returns a new instance of ApiClient.



18
19
20
# File 'lib/docs_kit/api_client.rb', line 18

def initialize(label:, lexer:, template:, filename: nil)
  super
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



17
18
19
# File 'lib/docs_kit/api_client.rb', line 17

def filename
  @filename
end

#labelObject (readonly)

Returns the value of attribute label

Returns:

  • (Object)

    the current value of label



17
18
19
# File 'lib/docs_kit/api_client.rb', line 17

def label
  @label
end

#lexerObject (readonly)

Returns the value of attribute lexer

Returns:

  • (Object)

    the current value of lexer



17
18
19
# File 'lib/docs_kit/api_client.rb', line 17

def lexer
  @lexer
end

#templateObject (readonly)

Returns the value of attribute template

Returns:

  • (Object)

    the current value of template



17
18
19
# File 'lib/docs_kit/api_client.rb', line 17

def template
  @template
end

Instance Method Details

#filename_for(request) ⇒ Object

The filename for this client's title bar: a static String, or the result of calling a proc filename with the request (so it can vary by verb/path).



24
25
26
# File 'lib/docs_kit/api_client.rb', line 24

def filename_for(request)
  filename.respond_to?(:call) ? filename.call(request) : filename
end

#render(request) ⇒ Object

Render the snippet for this client from the request struct.



29
# File 'lib/docs_kit/api_client.rb', line 29

def render(request) = template.call(request)