Class: LabSystem::KrokiClient
- Inherits:
-
NetSystem::Client
- Object
- Liza::Unit
- Liza::Controller
- NetSystem::Client
- LabSystem::KrokiClient
- Defined in:
- lib/lab_system/clients/kroki_client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#cache_key ⇒ Object
readonly
Returns the value of attribute cache_key.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Attributes inherited from Liza::Controller
Class Method Summary collapse
- .call(name, action, output_format, &block) ⇒ Object
- .diagram(name, endpoint, *formats) ⇒ Object
-
.diagrams ⇒ Object
diagrams.
Instance Method Summary collapse
- #call ⇒ Object
- #endpoint ⇒ Object
- #http_request! ⇒ Object
-
#initialize(name, action, format, &block) ⇒ KrokiClient
constructor
A new instance of KrokiClient.
- #read_cache! ⇒ Object
- #render_code! ⇒ Object
- #url ⇒ Object
- #write_cache! ⇒ Object
Methods inherited from Liza::Controller
#`, `, attr_accessor, attr_reader, attr_writer, #attrs, box, #box, color, division, division!, division?, inherited, menv_accessor, menv_reader, menv_writer, on_connected, panel, #panel, plural, require, requirements, sh, #sh, singular, subsystem, subsystem!, subsystem?, subsystem_token, token
Methods inherited from Liza::Unit
_erbs_for, #add, add, cl, #cl, class_methods_defined, const_added, const_missing, constants_defined, define_error, descendants_select, division, erbs_available, erbs_defined, erbs_for, errors, #fetch, fetch, get, #get, instance_methods_defined, log, #log, log?, #log?, #log_array, log_array, log_hash, #log_hash, #log_level, log_level, #log_level?, log_level?, log_levels, #log_levels, #log_render_convert, #log_render_format, #log_render_in, #log_render_out, method_added, methods_defined, namespace, part, raise_error, #raise_error, reload!, #reload!, #render, #render!, #render_stack, renderable_formats_for, renderable_names, section, sections, #set, set, #settings, settings, singleton_method_added, sleep, #sleep, stick, #stick, sticks, #sticks, subclasses_select, subunits, system, #system, system?, test_class, time_diff, #time_diff
Constructor Details
#initialize(name, action, format, &block) ⇒ KrokiClient
Returns a new instance of KrokiClient.
40 41 42 43 44 45 46 47 |
# File 'lib/lab_system/clients/kroki_client.rb', line 40 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
#action ⇒ Object (readonly)
Returns the value of attribute action.
38 39 40 |
# File 'lib/lab_system/clients/kroki_client.rb', line 38 def action @action end |
#cache_key ⇒ Object (readonly)
Returns the value of attribute cache_key.
38 39 40 |
# File 'lib/lab_system/clients/kroki_client.rb', line 38 def cache_key @cache_key end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
38 39 40 |
# File 'lib/lab_system/clients/kroki_client.rb', line 38 def code @code end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
38 39 40 |
# File 'lib/lab_system/clients/kroki_client.rb', line 38 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
38 39 40 |
# File 'lib/lab_system/clients/kroki_client.rb', line 38 def name @name end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
38 39 40 |
# File 'lib/lab_system/clients/kroki_client.rb', line 38 def result @result end |
Class Method Details
.call(name, action, output_format, &block) ⇒ Object
5 6 7 8 |
# File 'lib/lab_system/clients/kroki_client.rb', line 5 def self.call(name, action, output_format, &block) super({}) new(name, action, output_format, &block).call end |
.diagram(name, endpoint, *formats) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lab_system/clients/kroki_client.rb', line 16 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 |
.diagrams ⇒ Object
diagrams
12 13 14 |
# File 'lib/lab_system/clients/kroki_client.rb', line 12 def self.diagrams fetch(:diagrams) { {} } end |
Instance Method Details
#call ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lab_system/clients/kroki_client.rb', line 51 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 |
#endpoint ⇒ Object
71 72 73 |
# File 'lib/lab_system/clients/kroki_client.rb', line 71 def endpoint self.class.diagrams[name][:endpoint] end |
#http_request! ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/lab_system/clients/kroki_client.rb', line 100 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
87 88 89 90 91 92 |
# File 'lib/lab_system/clients/kroki_client.rb', line 87 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
81 82 83 84 85 |
# File 'lib/lab_system/clients/kroki_client.rb', line 81 def render_code! @code = render! @action require "digest/md5" @code_md5 = Digest::MD5.hexdigest(@code) end |
#url ⇒ Object
75 76 77 |
# File 'lib/lab_system/clients/kroki_client.rb', line 75 def url "#{KrokiDockerShell.url}#{endpoint}" end |