Class: AsciidoctorExtensions::KrokiClient
- Inherits:
-
Object
- Object
- AsciidoctorExtensions::KrokiClient
- 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
-
#max_uri_length ⇒ Object
readonly
Returns the value of attribute max_uri_length.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#server_url ⇒ Object
readonly
Returns the value of attribute server_url.
Instance Method Summary collapse
- #get_image(kroki_diagram, encoding) ⇒ Object
-
#initialize(opts, logger = ::Asciidoctor::LoggerManager.logger) ⇒ KrokiClient
constructor
A new instance of KrokiClient.
- #text_content(kroki_diagram) ⇒ Object
Constructor Details
#initialize(opts, logger = ::Asciidoctor::LoggerManager.logger) ⇒ KrokiClient
Returns a new instance of KrokiClient.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 472 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 "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_length ⇒ Object (readonly)
Returns the value of attribute max_uri_length.
468 469 470 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 468 def max_uri_length @max_uri_length end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
468 469 470 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 468 def method @method end |
#server_url ⇒ Object (readonly)
Returns the value of attribute server_url.
468 469 470 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 468 def server_url @server_url end |
Instance Method Details
#get_image(kroki_diagram, encoding) ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 491 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
487 488 489 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 487 def text_content(kroki_diagram) get_image(kroki_diagram, 'utf-8') end |