Skip to content

Instantly share code, notes, and snippets.

@minimum2scp
Created February 20, 2014 11:46
Show Gist options
  • Select an option

  • Save minimum2scp/9111835 to your computer and use it in GitHub Desktop.

Select an option

Save minimum2scp/9111835 to your computer and use it in GitHub Desktop.
graphviz-web.rb の抜粋(Sinatra Application)
post '/graphviz' do
data = params[:data]
if data.respond_to?(:has_key?) && data.has_key?(:tempfile) && data[:tempfile].respond_to?(:read)
data = data[:tempfile].read
end
format = params[:format]
layout = params[:layout]
hash = Digest::SHA1.hexdigest(data)
src_file, img_file, err_file = *filenames(hash, layout, format)
if ! validate_layout(layout)
return json :result => 'failed', :message => "Invalid layout \"#{layout}\". Available formats: #{settings.layouts.join(', ')}"
end
if ! validate_format(format)
return json :result => 'failed', :message => "Invalid format \"#{format}\". Available formats: #{settings.formats.join(', ')}"
end
if !File.exist?(src_file) || !File.exist?(img_file)
File.open(src_file, 'w') { |fh| fh << data }
end
if !File.exist?(img_file)
cmd = "#{layout} -o #{img_file} -T #{format} #{src_file} 2> #{err_file} && rm #{err_file}"
system(cmd)
end
if File.exist?(err_file)
json :result => 'failed', :message => 'Command execution failed.', :message_detail => File.read(err_file)
else
json :result => 'ok', :hash => hash, :image_url => url("/graphviz/#{layout}/#{hash}.#{format}"), :image_view_url => url("/graphviz/#{layout}/#{hash}.#{format}.html")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment