module Asciidoctor::Diagram::Cacoo

@private

Constants

VERSION

Public Class Methods

get(url) click to toggle source
# File lib/asciidoctor-diagram-cacoo/extension.rb, line 40
def self.get(url)
  https = Net::HTTP.new('cacoo.com', 443)
  https.use_ssl = true
  https.start do
    response = https.get(url)
    raise "Cacoo response status code was #{response.code}" if response.code != '200'
    response.body
  end
end
get_diagram(diagram_id, api_key) click to toggle source
# File lib/asciidoctor-diagram-cacoo/extension.rb, line 21
def self.get_diagram(diagram_id, api_key)
  # NOTE: See API document at https://cacoo.com/lang/en/api and
  # https://cacoo.com/lang/en/api_image
  get("/api/v1/diagrams/#{diagram_id}.png?apiKey=#{api_key}")
end
get_diagram_contents(diagram_id, api_key) click to toggle source
# File lib/asciidoctor-diagram-cacoo/extension.rb, line 27
def self.get_diagram_contents(diagram_id, api_key)
  # NOTE: See API document at https://cacoo.com/lang/en/api and
  # https://cacoo.com/lang/en/api_diagram_contents
  get("/api/v1/diagrams/#{diagram_id}/contents.xml?returnValues=textStyle,shapeStyle,uid,position,point&apiKey=#{api_key}")
end
get_diagram_metadata(diagram_id, api_key) click to toggle source
# File lib/asciidoctor-diagram-cacoo/extension.rb, line 10
def self.get_diagram_metadata(diagram_id, api_key)
  # NOTE: See API document at https://cacoo.com/lang/en/api and
  # https://cacoo.com/lang/en/api_image
  unless @diagrams
    diagrams = JSON.parse(get("/api/v1/diagrams.json?apiKey=#{api_key}"))
    @diagrams = diagrams['result'].each_with_object({}) { |d, h| h[d['diagramId']] = d }
  end

  @diagrams[diagram_id]
end
included(mod) click to toggle source
# File lib/asciidoctor-diagram-cacoo/extension.rb, line 50
def self.included(mod)
  mod.register_format(:png, :image) do |c, p|
    if c.options[:does_download_contents]
      contents = Cacoo.get_diagram_contents(c.diagram_id, c.api_key)
      Cacoo.save_diagram_contents("#{c.image_name}-contents.xml", contents, c.options[:contents_xml_indent])
    end
    Cacoo.get_diagram(c.diagram_id, c.api_key)
  end
end
save_diagram_contents(filename, contents, indent) click to toggle source
# File lib/asciidoctor-diagram-cacoo/extension.rb, line 33
def self.save_diagram_contents(filename, contents, indent)
  doc = Nokogiri.XML(contents)
  File.open(filename, 'w') do |f|
    f.write doc.to_xml(indent: indent)
  end
end