Class: Asciidoctor::Diagram::HttpConverter

Inherits:
Object
  • Object
show all
Includes:
DiagramConverter
Defined in:
lib/asciidoctor-diagram/http/converter.rb

Constant Summary collapse

DEFAULT_MAX_GET_SIZE =
1024

Instance Method Summary collapse

Methods included from DiagramConverter

#native_scaling?, #wrap_source

Constructor Details

#initialize(base_uri, type, converter) ⇒ HttpConverter

Returns a new instance of HttpConverter.



18
19
20
21
22
# File 'lib/asciidoctor-diagram/http/converter.rb', line 18

def initialize(base_uri, type, converter)
  @base_uri = base_uri
  @type = type
  @converter = converter
end

Instance Method Details

#collect_options(source) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/asciidoctor-diagram/http/converter.rb', line 28

def collect_options(source)
  options = {}

  options[:max_get_size] = source.global_attr('max-get-size', DEFAULT_MAX_GET_SIZE).to_i

  options
end

#convert(source, format, options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/asciidoctor-diagram/http/converter.rb', line 36

def convert(source, format, options)
  code = source.code

  uri = URI(@base_uri)

  case @type
  when :plantuml
    deflate = Zlib::Deflate.new(Zlib::BEST_COMPRESSION,
                                -Zlib::MAX_WBITS,
                                Zlib::MAX_MEM_LEVEL,
                                Zlib::DEFAULT_STRATEGY)

    compressed = deflate.deflate(code, Zlib::FINISH)
    deflate.close

    encoded = Base64.urlsafe_encode64(compressed)
    data = '0A' + encoded

    path = uri.path.dup
    path << '/' unless path.end_with? '/'
    path << format.to_s
  when :kroki_io
    compressed = Zlib.deflate(code, Zlib::BEST_COMPRESSION)
    data = Base64.urlsafe_encode64(compressed)

    path = uri.path.dup
    path << '/' unless path.end_with? '/'
    path << source.diagram_type.to_s
    path << '/' << format.to_s
  else
    raise "Unsupported server type: " + @type
  end

  get_path = path.dup << '/' << data

  if get_path.length > options[:max_get_size]
    uri.path = path
    get_uri(uri, code, 'text/plain; charset=utf-8')
  else
    uri.path = get_path
    get_uri(uri)
  end
end

#supported_formatsObject



24
25
26
# File 'lib/asciidoctor-diagram/http/converter.rb', line 24

def supported_formats
  @converter.supported_formats
end