Class: AsciidoctorExtensions::KrokiClient

Inherits:
Object
  • Object
show all
Includes:
Asciidoctor::Logging
Defined in:
lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb

Overview

Kroki client

Constant Summary collapse

SUPPORTED_HTTP_METHODS =
%w[get post adaptive].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, logger = ::Asciidoctor::LoggerManager.logger) ⇒ KrokiClient

Returns a new instance of KrokiClient.



349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 349

def initialize(opts, logger = ::Asciidoctor::LoggerManager.logger)
  @server_url = opts[:server_url]
  @max_uri_length = opts.fetch(:max_uri_length, 4000)
  @http_client = opts[:http_client]
  method = opts.fetch(:http_method, 'adaptive').downcase
  if SUPPORTED_HTTP_METHODS.include?(method)
    @method = method
  else
    logger.warn message_with_context "Invalid value '#{method}' for kroki-http-method attribute. The value must be either: " \
                                     "'get', 'post' or 'adaptive'. Proceeding using: 'adaptive'.",
                                     source_location: opts[:source_location]
    @method = 'adaptive'
  end
end

Instance Attribute Details

#max_uri_lengthObject (readonly)

Returns the value of attribute max_uri_length.



345
346
347
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 345

def max_uri_length
  @max_uri_length
end

#methodObject (readonly)

Returns the value of attribute method.



345
346
347
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 345

def method
  @method
end

#server_urlObject (readonly)

Returns the value of attribute server_url.



345
346
347
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 345

def server_url
  @server_url
end

Instance Method Details

#get_image(kroki_diagram, encoding) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 368

def get_image(kroki_diagram, encoding)
  type = kroki_diagram.type
  format = kroki_diagram.format
  text = kroki_diagram.text
  opts = kroki_diagram.opts
  if @method == 'adaptive' || @method == 'get'
    uri = kroki_diagram.get_diagram_uri(server_url)
    if uri.length > @max_uri_length
      # The request URI is longer than the max URI length.
      if @method == 'get'
        # The request might be rejected by the server with a 414 Request-URI Too Large.
        # Consider using the attribute kroki-http-method with the value 'adaptive'.
        @http_client.get(uri, opts, encoding)
      else
        @http_client.post("#{@server_url}/#{type}/#{format}", text, opts, encoding)
      end
    else
      @http_client.get(uri, opts, encoding)
    end
  else
    @http_client.post("#{@server_url}/#{type}/#{format}", text, opts, encoding)
  end
end

#text_content(kroki_diagram) ⇒ Object



364
365
366
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 364

def text_content(kroki_diagram)
  get_image(kroki_diagram, 'utf-8')
end