Class: LabSystem::KrokiClient

Inherits:
NetSystem::Client show all
Defined in:
lib/lab_system/clients/kroki_client.rb

Direct Known Subclasses

LizaKrokiClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Liza::Controller

color, inherited, on_connected

Methods inherited from Liza::Unit

const_missing, division, part, system, #system, test_class

Constructor Details

#initialize(name, action, format, &block) ⇒ KrokiClient

Returns a new instance of KrokiClient.



39
40
41
42
43
44
45
46
# File 'lib/lab_system/clients/kroki_client.rb', line 39

def initialize(name, action, format, &block)
  @name = name
  @render_format = name
  @action = action
  @format = format
  @result = nil
  instance_exec(&block) if block_given?
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



37
38
39
# File 'lib/lab_system/clients/kroki_client.rb', line 37

def action
  @action
end

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



37
38
39
# File 'lib/lab_system/clients/kroki_client.rb', line 37

def cache_key
  @cache_key
end

#codeObject (readonly)

Returns the value of attribute code.



37
38
39
# File 'lib/lab_system/clients/kroki_client.rb', line 37

def code
  @code
end

#formatObject (readonly)

Returns the value of attribute format.



37
38
39
# File 'lib/lab_system/clients/kroki_client.rb', line 37

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/lab_system/clients/kroki_client.rb', line 37

def name
  @name
end

#resultObject (readonly)

Returns the value of attribute result.



37
38
39
# File 'lib/lab_system/clients/kroki_client.rb', line 37

def result
  @result
end

Class Method Details

.call(name, action, output_format, &block) ⇒ Object



5
6
7
# File 'lib/lab_system/clients/kroki_client.rb', line 5

def self.call(name, action, output_format, &block)
  new(name, action, output_format, &block).call
end

.diagram(name, endpoint, *formats) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lab_system/clients/kroki_client.rb', line 15

def self.diagram name, endpoint, *formats
  diagrams[name] = {endpoint:, formats:}

  define_singleton_method name do |action, format, &block|
    call(name, action, format, &block)
  end

  define_singleton_method "new_#{name}" do |action, format, &block|
    new(name, action, format, &block)
  end
end

.diagramsObject

diagrams



11
12
13
# File 'lib/lab_system/clients/kroki_client.rb', line 11

def self.diagrams
  fetch(:diagrams) { {} }
end

Instance Method Details

#callObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lab_system/clients/kroki_client.rb', line 50

def call
  t = Time.now
  log "Calling #{endpoint} with #{action}.#{name}"

  render_code!
  read_cache!

  return @result if @result

  http_request!
  write_cache!

  @result
ensure
  log "#{t.diff}s to render #{action.inspect}"
  @result
end

#endpointObject



70
71
72
# File 'lib/lab_system/clients/kroki_client.rb', line 70

def endpoint
  self.class.diagrams[name][:endpoint]
end

#http_request!Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/lab_system/clients/kroki_client.rb', line 99

def http_request!
  require "net/http"
  require "json"
  t = Time.now
  
  request_uri = URI(url)
  request_headers = {'Content-Type' => 'application/json'}
  request_body = {diagram_source: code, output_format: format}.to_json
  log "POST #{request_uri} #{request_headers.count} headers and #{request_body.size} bytes of data."
  @response = Net::HTTP.post(request_uri, request_body, request_headers)

  if @response.code == '200'
    @result = @response.body
  else
    puts "---- HTTP #{@response.code} ----"
    puts @response.body
    puts "----"
    raise "Error calling Kroki API. HTTP Status Code: #{@response.code}"
  end
ensure
  log "#{t.diff}s to request #{request_uri}"
end

#read_cache!Object



86
87
88
89
90
91
# File 'lib/lab_system/clients/kroki_client.rb', line 86

def read_cache!
  raise "must be called after render_code!" unless @code_md5
  @cache_key = "tmp/#{App.mode}/clients/kokri/#{action}_#{@code_md5}.#{format}"
  log "cache_key = #{@cache_key.inspect}"
  @result = TextShell.read @cache_key if TextShell.exist? @cache_key
end

#render_code!Object



80
81
82
83
84
# File 'lib/lab_system/clients/kroki_client.rb', line 80

def render_code!
  @code = render! @action
  require "digest/md5"
  @code_md5 = Digest::MD5.hexdigest(@code)
end

#urlObject



74
75
76
# File 'lib/lab_system/clients/kroki_client.rb', line 74

def url
  "#{KrokiDockerShell.url}#{endpoint}"
end

#write_cache!Object



93
94
95
96
97
# File 'lib/lab_system/clients/kroki_client.rb', line 93

def write_cache!
  TextShell.write @cache_key, result
  cache_key = "tmp/#{App.mode}/clients/kokri/#{action}_#{@code_md5}.#{name}"
  TextShell.write cache_key, @code
end